Import module into Jupyter - python

I am running Python 3 via Anaconda on Windows 10. I am having trouble importing my own modules into Jupyter workbooks. E.g., if I try import fibo for a module named 'fibo.py' I get the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-4105c89a6fa8> in <module>()
----> 1 import fibo
ImportError: No module named 'fibo'
I have tried three things:
1) using sys.path.append("path\to\fibo_folder")
2) Changing the PATH and PYTHONPATH environment variables to include "path\to\fibo_folder"
3. Navigating the Jupyter Notebook to the same directory as 'fibo' is installed in.
Note that I have also included an empty .py file in the same directory as fibo called 'init.py'
Only 3) seems to work, but this is impractical if I am using modules stored in different folders (they need to be kept separate). I am sure 1) and 2) should work, but they don't seem to.
How may I import my own modules into Jupyter?

sys.path.append("/path/to/fibo file") should have solved the issue. (Note that in your question you have used "\" while giving the path, which is an escape character and is wrong and it should be "/". Not sure whether it is a typo in the question, but just mentioning it for the sake of completion.)
But like you mentioned in the question that sys.path.append() did not work, here is one common place where you might have gone wrong.
In the respective code (.py or .ipynb) where you need to import fibo, you should run the sys.path.append("/path/to/fibo file") every time you run the respective .py or .ipynb.
Meaning, it is not like you open the terminal, run python command, and type sys.path.append("/path/to/fibo file") in the terminal, close it and then run your .py or .ipynb file. This will not work and will throw the above-said error in your question.
sys.path.append() is a session variable and should be run every time you run your respective code in that particular .py or .ipynb file.
This should get
1) and 2)
working!
(Also, (I know, trivial) re-check the path to the "fibo" file is correct)

Related

VS code cannot import local python modules

All of my project files in VS code suddenly gives an error saying that it cannot import modules (even tho the modules are local i.e same directory and they used to work pretty well before).
The code works fine in pycharm but not in VS code, any idea whats going on?
Code:
from backend.util.crypto_hash import crypto_hash
from backend.config import MINE_RATE
error:
env DEBUGPY_LAUNCHER_PORT=34625 /home/nikhil/python-blockchain/blockchain-env/bin/python /home/nikhil/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher /home/nikhil/python-blockchain/backend/app/__init__.py
Traceback (most recent call last):
File "/home/nikhil/python-blockchain/backend/app/__init__.py", line 2, in <module>
from backend.blockchain.blockchain import Blockchain
ModuleNotFoundError: No module named 'backend'
Close VS Code,
start it again, Go to File > open folder (open your project folder in vs code),
if it gives a prompt to select a existing virtual environment, select that.
Then you should be good to go.
More unrelated information:
I assume the issue here is that there's some problem with VS Code not recognizing the virtual environment properly. This has happened to me several times and I cannot point out why that happens. But the above solution is a quick fix and always works for me.
I'm not sure if this will resolve OP's (ten-month old) question, but I've been struggling with the same error using debugpy while trying to configure VSCode's debugger.
Step 4 of this resource resolved the issue for me. In particular, I was using the -m flag when running debugpy, but the module to be run was not in the current working directory. Once I had changed this, the debugger worked as expected. As an example, if the command was originally:
python -m debugpy --listen 0.0.0.0:5678 ./some/directory/my_script.py
then the following two commands would rectify it:
cd ./some/directory
python -m debugpy --listen 0.0.0.0:5678 ./my_script.py
After doing this, I no longer received the "No module found" error.
The main reason is that VSCode does not automatically configure environment variables for you, but PyCharm does. The simplest method is adding your module file to system path.
import sys
sys.path.append(module_file_path)
The better solution is to config your environmental path named PYTHONPATH, adding related module path to it, and after that you will no longer need import system path manually.
Hope it works!

Custom modules are not found when calling python script in console VS pycharm

I have a python3 script that I am calling in terminal; I do not use Python prefix to run it, since I did add #!/usr/local/bin/python3 in my script (I have python3 from brew, on OSX).
The interesting thing is that if I run the script in terminal, I get an import error because one of my custom module hasn't been found. If I run the same script in pycharm, it works fine.
I assume Python launch and read all the various path that I use for modules in the same way, in both pycharm and terminal, but it seems not the case. How do I set up my scripts so the modules are found, independently from their path?
I may run the same script from other machines too, so I want to be prepared and do the right thing from the start.
EDIT
I am running pycharm on OSX; Python3 is installed via Brew, but the symlink is in /usr/local/bin.
My script is running from a folder inside my home directory, so
/Users/tester/git/python_test_app/main/base/app_main.py
The custom modules are in the same folder of the main py script, but one level above: /Users/tester/git/python_test_app/main/pyutils.py
The import statement from app_main.py is
import main.pyutils as utilities
This is the stack trace that I get when running the script:
Traceback (most recent call last):
File "main/base/app_main.py", line 13, in <module>
import main.pyutils as utilities
ModuleNotFoundError: No module named 'main'
EDIT 2 and solution
Thanks to The answers, I was able to figure out that the issue is related to how Pycharm handle projects. Basically it somehow save the path where the project is; so calling an import will result in the project folder being parsed, and that's why it works fine from Pycharm.
In Python, unless PYTHONPATH has the path to my project or other modules that I wrote, it won't be able to find them, hence, raise the error.
FIX:
in my main module that I use to run the application, I did retrieve the path of the file; which I know being one level below the modules I need; so I can explicitly add the folder to the current sys.path. This will end up making possible for me to call the import successfully.
import sys
current_dir = os.path.dirname(__file__)
sys.path.insert(0, , current_dir)
The only downside is that every file and resource that I use in my project, has to be directly referred by full path; so I have to pass the current_dir around the various files in the project.
PyCharm has project interpreter settings. Verify these are the same as your system Python. Go to:
File menu
Settings
Project: <project name>
Project Interpreter
View the path to the Python executable/binary being used by the project in PyCharm and verify it matches what your system is calling (e.g., which python3)
Alternatively, it may be that you declared your sources root within PyCharm and the system cannot properly run the module as it exists in the path you're running it from (especially if inside a package). You can get around this using the -m parameter and calling it from Python.
You can also try running it from the Terminal inside PyCharm and see what it adds to the path before initializing the shell session (you can sometimes see this in your Run configurations also). If you are referring to modules not installed via pip / into the Python path but rather loaded into your project path, then this may be the culprit.
On PyCharm, next to the green "RUN" arrow press the box and then press edit configurations (see image)
There you'll have Working Directory - that path is where PyCharm is running that script from (without errors).
Try running it from the terminal within that path - that should solve your import errors.

Python can't find module in the same folder

My python somehow can't find any modules in the same directory.
What am I doing wrong? (python2.7)
So I have one directory '2014_07_13_test', with two files in it:
test.py
hello.py
where hello.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
def hello1():
print 'HelloWorld!'
and test.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
from hello import hello1
hello1()
Still python gives me
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in <module>
ImportError: No module named hello
What's wrong?
Change your import in test.py to:
from .hello import hello1
Your code is fine, I suspect your problem is how you are launching it.
You need to launch python from your '2014_07_13_test' directory.
Open up a command prompt and 'cd' into your '2014_07_13_test' directory.
For instance:
$ cd /path/to/2014_07_13_test
$ python test.py
If you cannot 'cd' into the directory like this you can add it to sys.path
In test.py:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
Or set/edit the PYTHONPATH
And all should be well...
...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'
There is a better shebang you should use.
Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.
I had a similar problem, I solved it by explicitly adding the file's directory to the path list:
import os
import sys
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
After that, I had no problem importing from the same directory.
Here is the generic solution I use. It solves the problem for importing from modules in the same folder:
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
Put this at top of the module which gives the error "No module named xxxx"
In my case, Python was unable to find it because I'd put the code inside a module with hyphens, e.g. my-module. When I changed it to my_module it worked.
I ran into this issue. I had three folders in the same directory so I had to specify which folder.
Ex: from Folder import script
I had somewhat of a similar problem. I could not import modules even though they all were in the same directory (importError). I tried out the solutions above but none of them worked for me. I had to set up the path myself (manually). Also, the code was run on my university server, perhaps that's why I had to set the path manually.
import sys
sys.path.append(r'path_to_directory_where_all_modules_are')
I recommend reading The Module Search Path
The following doesn't solve the OP's problem, but the title and error is exactly what I faced.
If your project has a setup.py script in it, you can install that package you are in, with python3 -m pip install -e . or python3 setup.py install or python3 setup.py develop, and this package will be installed, but still editable (so changes to the code will be seen when importing the package). If it doesn't have a setup.py, make sense of it.
Anyway, the problem OP faces seems to not exist anymore?
file one.py:
def function():
print("output")
file two.py:
#!/usr/bin/env python3
import one
one.function()
chmod +x two.py # To allow execution of the python file
./two.py # Only works if you have a python shebang
Command line output: output
Other solutions seem 'dirty'
In the case of OP with 2 test files, modifying them to work is probably fine. However, in other real scenarios, the methods listed in the other answers is probably not recommended. They require you to modify the python code or restrict your flexibility (running the python file from a specific directory) and generally introduce annoyances. What if you've just cloned a project, and this happens? It probably already works for other people, and making code changes is unnecessary. The chosen answer also wants people to run a script from a specific folder to make it work. This can be a source of long term annoyance, which is never good. It also suggests adding your specific python folder to PATH (can be done through python or command line). Again, what happens if you rename or move the folder in a few months? You have to hunt down this page again, and eventually discover you need to set the path (and that you did exactly this a few months ago), and that you simply need to update a path (sure you could use sys.path and programmatically set it, but this can be flaky still). Many sources of great annoyance.
If you are sure that all the modules, files you're trying to import are in the same folder and they should be picked directly just by giving the name and not the reference path then your editor or terminal should have opened the main folder where all the files/modules are present.
Either, try running from Terminal, make sure first you go to the correct directory.
cd path to the root folder where all the modules are
python script.py
Or if running [F5] from the editor i.e VsCode then open the complete folder there and not the individual files.
After spending hours to get imports working like:
from business import Business
from .business import Business
import .Business
import business.Business
...
I got finally rid of my embedded python installation and installed python from the scratch by be the .exe file for all users like in
c:\Program Files\Python310
then I made sure my PATH Variable is up to date with the new installation (so what we want to see or make are entries like c:\Program Files\Python310 and c:\Program Files\Python310\Scripts and %USERPROFILE%\AppData\Roaming\Python\Python310\Scripts) and started a cmd with administrator privileges, downloaded the get-pip.py file and run it in the elivated cmd like python get-pip.py and finaly everything worked as expected... I don't know why or what I did wrong or so, but python really seems that it need to be integrated deeply into windows or it just do not work the easy way. It doesn't happen too often, but in this case it worked a lot better in linux ;)
Also recheck spelling of both the file and the module for typos.
For example
import passwords
When the file name has been saved as password missing an s.
It might sound obvious but it can sometimes be something as simple as this when all other advice above not working :)
This kind of problems happens when your project path is changed. You need to use:
cd path\to\the\files_path
simply
The same error, but I didn't find an answer to my case, maybe someone need my solution.
It appears that in PyCharm I've created .py file, but somehow in windows directory of my project file was blank and without .py. So I rename extension right in derictory and it worked.
Picture before changing an extension

Weird issue with platypus (makes .app packages from scripts)

I wrote myself a small Python script that I want to use to automatically do things with certain types of files; as such, I want to create an .app out of it so that I can set certain files to be opened with it automatically.
So I looked around and found Platypus which seems to do what I need.
However, weirdly it doesn't work. Specifically, it does not seem to be finding the right python interpreter. I set it up as follows:
I.e., the script type is env so it should just read the top line of the file like the shell does.
In magic.py, the top line is #!/usr/bin/env python2.7.
Now, when I run the shell script on the command line (that is, ~/devel/magic.py whatever), everything works fine. But when I run the app, it errors with:
Traceback (most recent call last):
File "/Users/jan/Dropbox/devel/Magic.app/Contents/Resources/script", line 8, in <module>
from bencode import *
ImportError: No module named bencode
The same import works just fine when running it from the command line, so I'm thinking it's using the wrong interpreter somehow. How can I fix or debug this?
You are trying to import from bencode module but you didn't add it in the application's bundled resources. Either drag it to the list of included files and export again or just copy it to the resources folder in the package's contents.

hbase module not found in python - how to import?

I have installed hadoop with this tutorial, hbase with this one, and hbase.thrift with this one
Now I have a given python script, which is there to created some hbase tables. When I run the py file it gives me the error:
Traceback (most recent call last):
File "./createTables.py", line 9, in <module>
from hbase import Hbase
ImportError: No module named hbase
This question seemed to have the same trouble: How can I import hbase in python?
I tried the solution given there. I ran
thrift --gen py Hbase.thrift
in the /usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift folder, where the Hbase.thrift was located. Ot created the subfolder gen-py, as descibed in the tutorial linked in above similiar question.
Now, if I get the "Simply take that command and copy it to your default module folder (or in the folder where you run your program and it should work)." Part of given solution there right, I go to the folder in which my given py file is located (say /home/kumo/Downloads/createTables.py) and run
thrift --gen py /usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
...? But nothing happes with that. Copying the Hbase.thrift file into the Downloads folder next to the py file, gives only
[FAILURE:arguments:1] Could not open input file with realpath: ./Hbase.thrift
So obviously not helping either.
I also tried adding
import sys
sys.path.append('/usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/gen-py')
gave the same intial missing modules error again.
I also tried adding the 5.c. step of the thrift tutorial by adding the python path in the .bashrc:
export PYTHONPATH=$PYTHONPATH:/usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/gen-py
did not really work.
I tired the same with the path /usr/local/hadoop/src/contrib/thriftfs/gen-py, as that is another gen-py folder that somehow popped up, both as a sys import and pythonpath export, but it still gives me the same error.
I am still new to all of this, so I just followed the tutorials step by step. I have no clue what I may have missed or wasn't in the tutorials to begin with.
Thanks for any help!
Not sure what exactly is the problem in your case but I'm quite happy with HappyBase - you might want to give it a try.
The solution seems to be a link in the python folder to the generated gen-py folder. I moved the unzipped hbase folder to an own "software" folder in home, then I created the link:
cd /usr/local/lib/python2.7/dist-packages/
ln -s /home/kumo/software/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/gen-py/hbase/
and all was fine about that error. Not sure, what happens with multiple projects, through.

Categories

Resources