similar to this issue and this topic related article, I have created my own
package within Jupyter Notebook. I can successfully access module content after the first import of a python file.
However, whenever I'd like to add a new function to the python file, I am unable to access it then within my notebook.
I've tried the following:
- adjust and save the python file online
- delete the old python version and upload a new one
The only thing working was to upload the python file with a different name. But this is not really what I want to achieve :D
Anyone here having an idea how to add new functions with direct access ability?
This is how I import my modules:
import os
import sys
sys.path.insert(0, os.path.abspath('/home/ubuntu/jupyter/src/..'))
from src.parsing import general
general. <-- function list popping up
When you are in a cell with the code/function you want to use, press Shift + Enter to be able to access that code in the next cell.
I solved the issue. It was rather obvious. You need to restart the kernel, so that the newly added methods can be accessed.
Just rerunning the import cell is not sufficient.
Related
When I import ( like below after doing pip install somewhere ) handle_uploaded_file from 'somewhere library on my linux
machine.)*
from somewhere import handle_uploaded_file
Getting following error when trying to run my django application.)-
Seems I scared previous person that provided a correct answer as it was deleted, but
I'm gonna guess you are following this example. As you can see, there is a clear comment that it is supposed to be imaginary function, i.e. one that you need to implement yourself if you want to use it in the actual code. It's not supposed to refer to somewhere module which does not offer any function of that name.
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file
I'm trying to get a little bit familiar with code obfuscating and package building. For this I made a simple file named "addCode.py" with a function that adds two numbers called "add". I published it to PyPI and to use it I just need to install the package and import the function:
from addCode import add
The next step I would like to make is to obfuscate the code in addCode.py using pyarmor so that someone who imports the package can use it but can't read the source code. I know this does not fully protect the code but I just want it to be good enough to hide it for the regular person. So I got the encrypted code and uploaded it to PyPI but the problem is that now I cannot import the "add" function because it is not possible to find the function in the file (eventhough the function is there, but encrypted).
To resume: is there anyway I can import a function from an encrypted with pyarmor? If not, is there any other encrypting tool that lets me both import functions and run the code without any errors?
![invoice][1]
![python script][2]
[1]:
[2]: https://i.stack.imgur.com/Y6Ebm.png
Hello! I would appreciate if someone could help me with a doubt I have about using Python libraries in Django. I will try to be as clear as possible so here it goes:
In my job I work with invoices that are all saved in a specific directory (PDF files) and they all have the same structure. In my job I am interested in only one specific value in a row which is a number. My job is to extract that value from all the invoices and sum them all. So, I made a python script in which I use the libraries Pandas, os and PDFplumber and it works great. In code.png, you can see the loop I use to extract the row and value that I want by using PDFplumber and then sum all these values. In invoice.png you can see how PDFplumber divides the invoice in rows and in columns.
So, here is the thing: I want to deploy a Django App so that other people in the enterprise can use the python script I use ( they don't know anything about Python programming). So, I would want to deploy a Django app in which they can upload the directory with all the PDF files and then use the python script I show in code.png. But I'm having problems with the logic behind it. My questions are:
Can we use any python libraries in Django ( such as PDFplumber, pandas, etc) ?
Would I put my python script in views.py? (Something like this)
app/views.py
import os
import pdfplumber
import pandas as pd
from django.shortcuts import render
def function(request):
#Use the directory with the images uploaded by the user.
#Python script shown in code.png
# data = response obtained from the python script.
return render(request, 'app/response.html',{"data":data})
Thank you beforehand!
Can we use any python libraries in Django ( such as PDFplumber, pandas, etc) ?
Yes, a Django app is just Python code. You can add whatever python code you want, including import pandas or whatever.
Would I put my python script in views.py? (Something like this)
While you can write the code in the way you describe, I suggest you organize it more logically. The view function should only handle the request and return a response. Any business logic, such as parsing PDF files, should be in a separate file. Organize your code in a way that makes sense for the task you are trying to complete.
Django is just another Python library so you can definitely use other libs together with Django. Adding your code in views.py is a very good start your project. The downside of views.py is that it will block the call until a response is received. If it takes too lonk you may get HTTP timeouts so other solutions can be explored but I would for sure start with the code in views.py.
I have a python script that is written in different files (one for importing, one for calculations, et cetera). These are all in the same folder, and when I need a function from another function I do something like
import file_import
file_import.do_something_usefull()
where, of course, in the file_import there is a function do_something_usefull() that, uhm, does something usefull. How can I accomplish the same in Azure?
I found it out myself. It is documenten on Microsoft's site here.
The steps, very short, are:
Include all the python you want in a .zip
Upload that zip as a dataset
Drag the dataset as the third option parameter in the 'execute python'-block (example below)
execute said function by importing import Hello (the name of the file, not the zip) and running Hello.do_something_usefull()
As reference, there is a similiar answered thread you can refer to, please see Access Azure blog storage from within an Azure ML experiment.
I have been writing some code and used it as a module. This was working properly, until this morning one of the modules would not load.
I have multiple programs in one direction. most of them load, except for two.
I have tried importing it using:
import company.a
from company import a
Both does not work.
However the following things both do work:
import company.b
from company import b
Both are in the same folder, containing __init__.py.
Can someone help me with this?
For some reason, it did work when I made an exact copy of the company folder, and named it company2.
the following does work:
from company2 import a
while it is the exact same file, in an folder containing exactly the same, with a different name.
Solved for now, although I am curious why changing the name of the folder made the difference