Module Not found during import in Jupyter Notebook - python

I have the following package (and working directory):
WorkingDirectory--
|--MyPackage--
| |--__init__.py
| |--module1.py
| |--module2.py
|
|--notebook.ipynb
In __init__.py I have:
import module1
import module2
If I try to import MyPackage into my notebook:
import MyPackage as mp
I will get ModuleNotFoundError: No module named 'module1'. But import works fine if I execute the script outside a notebook: if I create test.py in the same directory and do the same as in the notebook the import would work properly. It will work inside the notebook if I use fully qualified name in __init__.py (import MyPackage.module1).
What's the reason for different import behavior?
I have confirmed the working directory of the notebook is WorkingDirectory.
---Update---------
Exact error is:
C:\Users\Me\Documents\Working Directory\MyPackage\__init__.py in <module>()
---> 17 import module1
ModuleNotFoundError: No module named 'module1'
My problem differs from the possible duplicate:
The notebook was able to find the package, but only unable to load the module. This was inferred from substituting module1 with MyPackage.module1 worked well and suggests it may not be a problem related with PATH.
I cded into WorkingDirectory and started the server there. The working directory should be the folder containing my package.

I'm pretty sure this issue is related and the answer there will help you: https://stackoverflow.com/a/15622021/7458681
tl;dr the cwd of the notebook server is always the base path where you started the server, no matter was running import os os.getcwd() says. Use import sys sys.path.append("/path/to/your/module/folder").
I ran it with some dummy modules in the same structure as you had specified, and before modifying sys.path it wouldn't run and after it would

understand this two functions, your problem will be solved.
#list the current work dir
os.getcwd()
#change the current work dir
os.chdir()
change the path, and import module, have fun.
sometime it won't work.try this
import sys
# sys.path is a list of absolute path strings
sys.path.append('/path/to/application/app/folder')
import file
-, -

if you face module not found on jupyter environment you had to install it on jupyter environment instead of installing it on command prompt
by this command(for windows) on jupyter
!pip install module name
after that you can easily import and use it.
Whenever you want to tell jupyter that this is system command you should put ( ! ) before your command.

The best way to tackle this issue is to create a virtual env and point your kernel to that virtual environment:
Steps:
python -m venv venv
source venv/bin/activate
ipython kernel install --user --name=venv
jupyter lab
go to the jupyter lab ->kernel-->change kernel-->add the venv from the dropdown
Now if your venv has the package installed, jupyter lab can also see the package and will have no problem importing the package.

You can do that by installing the import_ipynb package.
pip install import_ipynb
Suppose you want to import B.ipynb in A.ipynb, you can do as follows:
In A.ipynb:
import import_ipynb
import B as b
Then you may use all the functions of B.ipynb in A.

My problem was that I used the wrong conda enviroment when using Vs Code.
Enter your conda enviroment
conda activate **enviroment_name**
To check where a module is installed you can enter python interactive mode by writing python or python3. Then importing cv2
import cv2
Then to see where this module is installed
print(cv2.__file__)
You will see the installed path of the module. My problem was that my vs code kernel was set to the wrong enviroment. This can be changed in the top right corner for vs code.
hope this helps

this happened to me when I moved my journal into a new directory while the Jupyter lab server was running. The import broke for that journal, but when I made a new journal in the same directory I just moved to and used the same import, it worked. To fix this I:
Went to the root dir for my project.
Searched for all folders labeled “pycache”
Deleted all “pycache” folders that were found in my root and subfolders.
Restarted Jupyter lab server
Once Jupyter lab restarts and compiles your code, the “pycache” folders will be regenerated. Also the pycache folders have two leading and trailing “_”, but stackoverflow is formatting the pycache’s without them

The best solution by far (for me) is to have a kernel for each environment you are working in. Then, with that kernel defined, all you have to do is to update this kernel's environment variables to look at your project folder where your modules are located.
Steps (using pip):
pip install ipykernel (if not installed already)
source activate <your environment name>
python -m ipykernel install --user --name <your environment name> --display-name "<a display name>" (where is the name you want to give to your kernel and is just a name used for display by jupyter.
Once you ran the command above, it will output the location of the kernel configuration files. E.g.: C:\Users\<your user name>\AppData\Roaming\jupyter\kernels\<selected environment name>. Go to this folder and open the kernel.json file.
Add the following entry to this file:
"env": {
"PYTHONPATH": "${PYTHONPATH};<the path to your project with your modules>
}
Good reference about the kernel install command here.

The reason is that your MyPackage/__init__.py is ran from the current working directory. E.g. from WorkingDirectory in this case. It means, that interpreter cannot find the module named module1 since it is not located in either current or global packages directory.
There are few workarounds for this. For example, you can temporarily override a current working directory like this
cwd = os.getcwd()
csd = __path__[0]
os.chdir(csd)
and then, after all a package initialization actions like import module1 are done, restore "caller's" working directory with os.chdir(cwd).
This is quite a bad approach as for me, since, for example, if an exception is raised on initialization actions, a working directory would not be restored. You'll need to play with try..except statements to fix this.
Another approach would be using relative imports. Refer to the documentation for more details.
Here is an example of MyPackage/__init__.py that will work for your example:
from .module1 import *
But it has few disadvantages that are found rather empirically then through the documentation. For example, you cannot write something like import .module1.
Upd:
I've found this exception to be raised even if import MyPackage is ran from usual python console. Not from IPython or Jupyter Notebook. So this seems to be not an IPython itself issue.

Related

ModuleNotFoundError - Python VSCode I can´t import modules:

I can´t import any module in VSCode.
The structure of my project is the next:
6.Python
-.vscode
-settings.json
-Ortiz
-init.py
-puntoentrada.py
-Paquetes
-Prueba1
-init.py
-prob.py
puntoentrada.py looks like this:
puntoentrada.py
Then, this is prob.py
I tried to run this but I have a problem called:
ModuleNotFoundError: No module named 'puntoentrada'
prob.py
Then, this is the directory of Python
Directory
Then, this is the variable environment
PythonPath
Then, this is my Python > Analysis Cache Folder Path in VSCode:
Configure VSCode
This is the same in "Usuario", "Area de trabajo" and "6.Python"
Then, this is my settings.json
settings.json
I only want to execute "prob.py" and the only task is "import puntoentrada", but I cant do it, because puntoentrada according to VSCode "does not exist"
So, what can I do?
This is not a VSCode, but a Python issue. Python looks in serveral places for packages and modules and you can inspect these places via the sys.path. If prob.py is your main script, Python finds all modules that are in the same folder as prob.py. If your script is in a subfolder, you have to tell that to Python:
from subfolder import mymodule
If the module is part of a package in a completely separate path, your best option is to install that package in editable mode:
pip install path/to/mypackage -e
But you have to have a setup.py in that second package.
A quick-and-dirty workaround would be to simply add that separate path to sys.path using the append method.
I have changed my Python version for the operating environment in these places.
On the top right corner, you can choose the Python Version.
On the left bottom corner, you can choose the Python version.

Debug python library

I have no idea how to debut a library without installing it, I want to enhance this library, but if I run PyCharm with this configuration:
Pycharm debug configuration
I never collaborated on github or similar, however I decided to join this Hacktoberfest. What configuration should I use? In case I have to install it, how can I put the breakpoints easily?
Edit for more info:
Package has 3 modules:
scdl
|
|-------- client.py
|--------------- scdl.py
|--------------------utils.py
When I run the scdl.py module which has a main it tries to import the client and utils modules, however It can't find them for some reason, I tried to add the directory where the scdl package is stored to the PYTHONPATH variable, and still got no result
Screenshot of PyCharm Directory
The error is:
from scdl import client, utils
ImportError: cannot import name 'client'
When working on something that's intended to be a module, I usually install the module in my interpretter then use the "module_name" run configuration in PyCharm instead of the "script_path" like you have set up.
Create a virtual environment: python -m venv venv
Activate the virtual environment: source venv/bin/activate
Follow the instructions in the projects readme to install (in this case python setup.py install)
Add that virtual environment to PyCharm (Preferences > Project Interpreter)
PyCharm will prompt you to install requirements, do it
Where it says "script_path" in your run configuration, click the little arrow and change to "module_name"
Set the module name to "scdl.scdl"
Now you should be able to run/debug as normal, hope that helps!

How to add another path to the Python Path on AWS SageMaker

I've been trying to add one of my folders where I hold my python modules and, so far, I haven't been able to do it through AWS's terminal. The folder with the .py files is inside the main SageMaker folder, so I'm trying (I've also tried it with SageMaker/zds, which is the folder that holds the modules):
export PYTHONPATH="${PYTHONPATH}:SageMaker/"
After printing the directories of the PYTHONPATH through the terminal with python -c "import sys; print('\n'.join(sys.path))", I get that indeed my new path is included in the PYTHONPATH. However, when I try to import any module from any notebook (with from zds.module import * or from module import *), I get the error that the module doesn't exist. If I print the paths from the PYTHONPATH directly inside the notebook I no longer see the previously added path in the list.
Am I missing something basic here or is it not possible to add paths to the PYTHONPATH inside AWS SageMaker? For now, I'm having to use import sys, os
sys.path.insert(0, os.path.abspath('..')) inside basically every notebook as a fix to the problem.
Adding this to the lifecycle script worked for me
sudo -i <<'EOF'
touch /etc/profile.d/jupyter-env.sh
echo export PYTHONPATH="$PYTHONPATH:/home/ec2-user/SageMaker/repo-name/src" >> /etc/profile.d/jupyter-env.sh
EOF
Thanks for using Amazon SageMaker!
Copying from the https://docs.aws.amazon.com/sagemaker/latest/dg/notebook-lifecycle-config.html
Amazon SageMaker notebook instances use conda environments to implement different kernels for Jupyter notebooks. If you want to install packages that are available to one or more notebook kernels, enclose the commands to install the packages with conda environment commands that activate the conda environment that contains the kernel where you want to install the package
For example, if you want to install a package only in for the python3 environment, use the following code:
# This will affect only the Jupyter kernel called "conda_python3".
source activate python3
# Replace myPackage with the name of the package you want to install.
pip install myPackage
# You can also perform "conda install" here as well.
source deactivate
If you do installation in above suggested way you should be able to import your package from the Notebook corresponding Kernel which you are using. Let us know if it doesn't help.

Python: I can import packages in python but not in the notebook

I have installed two packages such as mgwr and libpysal.
I can import them using a normal python script but when I try to import in my jupyter notebook I get the error that the modules don't exist
import libpysal as ps
ImportError: No module named libpysal
from mgwr.gwr import GWR, MGWR
ImportError: No module named mgwr
if I do from Terminal
! which python
/usr/local/opt/python/libexec/bin/python
from the notebook
! which python
/Users/myName/anaconda2/bin/python
You might need to get your system to look for the anaconda version of Python by telling it where it's located.
cd ~
nano .bashrc
Now add export PATH="/home/<myName>/anaconda2/bin:$PATH" to the end of the file.
You can now refresh the PATH variable by using source or .
. .bashrc
Hopefully which python now points to your anaconda folder. If it does, go ahead and conda install the packages that Jupyter isn't finding. If not, leave a comment.

Fail to import tensorflow from a program called within a script via ssh [duplicate]

When I try to run my program from the PyCharm IDE everything works fine but if I type in Fedora:
python myScript.py
in a shell prompt I get an import error from 1 of the module.
ImportError : No modue named myDependency
What does PyCharm do that allows the interpreter to find my dependencies when launched from the IDE? How can I get my script to find its dependencies so it can be launched with a singe command?
There are a few possible things that can be causing this:
The same python interpreter? Check with import sys; print(sys.executable)
Is it the same working directory? Check with import os; print(os.getcwd())
Discrepancies in sys.path, which is the list python searches sequentially for import locations, can possibly caused by environment variables. Check with import sys; print(sys.path).
Adding this worked for me:
from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
As a first step, in PyCharm go to project settings / python interpreter, and note the path. Then compare that to the result of which python -- do they line up? If not, you need to make them do so.
If that's ok, check what PyCharm defines as your project root in project settings / project structure. Is that the directory where your script is located? If not, you should run the script from that directory or append the directory to the $PYTHONPATH variable.
Almost definitely, it's one of those two things.
You might have set some project dependency in Pycharm for module myDependency.
You can access the same in Fedora by importing the module explicitly or by creating the egg of that module and installing it.
This will then go to python site-packages from where you can refer this dependency.

Categories