ModuleNotFoundError in Juypter notebook after installing local Python package - python

I tried to install my project as a package so I can import functions from my src folder. In my conda env in the root folder of my project in the terminal I tried pip install -e ./ and (after uninstalling my first attempt) python -m pip install -e ./, both of which resulted in
Installing collected packages: gliders
Running setup.py develop for gliders
Successfully installed gliders
and the package showing up when I did conda list
as
Name Version Build Channel
gliders 0.1.0 dev_0 <develop>
However when I run code with import gliders in my Jupyter notebook, I get the following error: ModuleNotFoundError: No module named 'gliders'
I am using Python 3.6.13.
How can I fix this error?

Related

Module import error only when run from command line

I can successfully run a python script from the IDE (PyCharm) but when I attempt to run this from the command line I get ModuleNotFoundError.
The module is PythonGit and is imported as follows after doing pip install:
from git import Repo
if __name__ == '__main__':
print('Imported git package')
I have a virtual environment set up as .venv/ and the package is present here: .venv/lib/python3.10/site-packages/git.
The output of pip list is as follows:
Package Version
------------------ -------
...
gitdb 4.0.9
GitPython 3.1.29
...
The virtual environment is activated when I attempt to run from the command line yet this is the error I am getting:
File "/Users/paul/Repos/dependencies/main.py", line 1, in <module>
from git import Repo
ModuleNotFoundError: No module named 'git'
There are no special settings in the run configuration in the IDE, the script path points to the same file (main.py) and the interpreter to the one in the virtual environment.
I have not been able to find any answer on these forums that successfully addresses this issue yet.
Try to install using setup.py from this https://github.com/gitpython-developers/GitPython
Just clone from git and run setup.py in ur directory
I just tested and successfully ran the python program.
Here are the steps I took:
Open a command prompt
Navigate to the folder that will host the virtual environment. In my case this was:
D:\Personal\Programming>_
Checked Python version using: py --version
D:\Personal\Programming>py --version
Python 3.10.8
Upgraded pip using: py -m pip install --upgrade pip
Checked pip version using: py -m pip --version
D:\Personal\Programming>py -m pip --version
pip 22.3.1 from ...\AppData\Local\Programs\Python\Python310\lib\site-packages\pip (python 3.10)
Ensured I had the latest virtualenv : py -m pip install --user virtualenv
Create a new virtual environment (env) using : py -m venv env
Activated the environment (env) using: .\env\Scripts\activate
The cursor now shows you are in the virtual enviroment:
(env) D:\Personal\Programming>_
Also you should see a new folder called env in the D:\Personal\Programming folder (or where ever you created the virtual environment.
Checked that python existed within the virtual environment: where python
(env) D:\Personal\Programming>where python
D:\Personal\Programming\env\Scripts\python.exe
Installed GitPython using: py -m pip install GitPython
(env) D:\Personal\Programming>py -m pip install GitPython
Collecting GitPython
Using cached GitPython-3.1.29-py3-none-any.whl (182 kB)
Collecting gitdb<5,>=4.0.1
Downloading gitdb-4.0.10-py3-none-any.whl (62 kB)
---------------------------------------- 62.7/62.7 kB 3.5 MB/s eta 0:00:00
Collecting smmap<6,>=3.0.1
Using cached smmap-5.0.0-py3-none-any.whl (24 kB)
Installing collected packages: smmap, gitdb, GitPython
Successfully installed GitPython-3.1.29 gitdb-4.0.10 smmap-5.0.0
Checked pip list using: pip list
(env) D:\Personal\Programming>pip list
Package Version
---------- -------
gitdb 4.0.10
GitPython 3.1.29
pip 22.2.2
setuptools 63.2.0
smmap 5.0.0
Created the main.py program in my D:\Personal\Programming folder
Ran the main.py program: py main.py
(env) D:\Personal\Programming>py main.py
Imported git package
This shows that the program ran successfully.

How to resolve "ModuleNotFoundError: No module named 'package'" in notebook?

I installed a package in my notebook by running in a cell the following command:
!pip install packagename
but when I try to import the package, I'm getting the following error:
import packagename as pn
--> ModuleNotFoundError: No module named 'packagename'
I don't change my kernel between these commands, and also it seems installed correctly since running the install command again detects the installed package. I also tried re-installing it without success. What am I missing here?
To install any package on windows
pip install packagename
MacOS
python3 -m pip install packagename
Linux
sudo apt-get install packagename

Unable to import package in virtual environment using anaconda

I have installed a package in my virtual environment. The package is also listed in the conda list but when I try to import the package it returns an Import Error.
I was following the tutorial here
i just followed the whole tutorial,
first after creating the structure (schema in the tutorial):
1 . in terminal open your env
2. run python -m pip install --user --upgrade setuptools wheel
3. python setup.py sdist
4. python setup.py install
then in terminal (virtual env activated )
run commands
import example_pkg
it will import it.

Python on anaconda cannot find azure.mgmt.datafactory

I am trying to run this tutorial
https://learn.microsoft.com/en-US/azure/data-factory/quickstart-create-data-factory-python
but I fail to install the packages. I tried several installations but I keep getting the error No module named 'azure.mgmt.datafactory' when trying to run from azure.mgmt.datafactory import DataFactoryManagementClient.
I am using anaconda and windows 10.
I tried running the recommended anaconda packages https://anaconda.org/anaconda/azure and https://anaconda.org/clinicalgraphics/azure-mgmt-resource under a python 3.5 environment and I also tried to manually install everything from github (https://github.com/Azure/azure-sdk-for-python) using
git clone git://github.com/Azure/azure-sdk-for-python.git
cd azure-sdk-for-python
python setup.py install
In both the normal (Python 3.6) and the new (Python 3.5, using Anaconda version with Python 3.5) environment. None of this worked.
What am I missing?
(Note that from azure.mgmt.resource import ResourceManagementClient worked fine with the anaconda installation)
EDIT
After the first response, I ran the following commands from the powershell
pip install azure-mgmt-resource
pip install azure-mgmt-datafactory
pip install azure-mgmt
which resulted in ModuleNotFoundError: No module named 'azure.mgmt'
Uninstalling the three packages and installing azure-mgmt as a first one did not solve the issue either. However, I don't know how to uninstall the manually installed package from python setup.py install, which still might be an issue.
Have you tried pip install in powershell/cmd?
pip install azure-mgmt-datafactory
Update (Jan's answer):
pip freeze > requirements.txt
pip uninstall -r requirements.txt
python -m pip install azure-common
python -m pip install azure-mgmt
python -m pip install azure-mgmt-datafactory (this might not be needed as it comes with azure-mgmt)
Ok, this is how I got the required azure libraries to work (thx to Saul Cruy, who gave me the idea)
Using this post What is the easiest way to remove all packages installed by pip?, I created a requirements file in PowerShell
pip freeze > requirements.txt
In this file, I manually kept only the entries with azure.
Then, I deleted all packages in the file
pip uninstall -r requirements.txt
The steps above were repeated twice, as upon first delete, some azure packages survived.
Then, I ran (all in PowerShell, in that order)
python -m pip install azure-common
python -m pip install azure-mgmt
python -m pip install azure-mgmt-datafactory
The reason might(!) be that installing packages in the anaconda console using the conda commands causes confusion in the dependencies (I tried a similar approach in a conda environment as it seemed like a good idea to seperate the azure packages from the other ones, but without success).

pip installing to wrong folder even though `which pip` is correct

I'm using Mac OS X 10.10. I want to use pip to install packages for my homebrew installed version of python (located in /usr/local/bin/python, which is an alias that points to /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin). It appears that site-packages for this version are here: /usr/local/lib/python2.7/site-packages/.
which python returns /usr/local/bin/python
which pip returns /usr/local/bin/pip
These seem correct to me.
Trying something like pip install pylzma returns:
Collecting pylzma
Installing collected packages: pylzma
Successfully installed pylzma
You are using pip version 8.0.2, however version 8.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
But then pip list does not show pylzma to be installed. It looks like pip installs the packages to /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (the python that ships with Mac OS X).
How can I get pip to install to my homebrewed python?
I've tried a number of suggestions from similar questions:
I've tried export PATH=/usr/local/bin/python:${PATH}.
I've tried pip install --install-option="--prefix=/usr/local/lib/python2.7" pylzma.
I've tried changing the first line of the pip executable script to #!/usr/local/bin/python
I've tried /usr/local/bin/python -m pip install pylzma.
But none of these work. I also tried upgrading pip to 8.1.1, but that made pip break entirely. People recommend using virtualenv, but as far as I know, I can't install that without pip.
When I type python -m pip, it says:
Usage:
/usr/local/opt/python/bin/python2.7 -m pip <command> [options]
Could that be a problem?
My issue was that my /Users/<username>/.pydistutils.cfg contained the following:
[easy_install]
# set the default location to install packages
install_dir = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
[install]
install_lib = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
install_scripts = ~/bin
I changed this to:
[easy_install]
# set the default location to install packages
install_dir = /usr/local/lib/python2.7/site-packages
[install]
install_lib = /usr/local/lib/python2.7/site-packages
install_scripts = ~/bin
That seemed to have worked. pip install now installs packages to the desired location /usr/local/lib/python2.7/site-packages.
However, I am have ongoing path issues.
import pylzma still gives me ImportError: No module named pylzma.
and running jupyter notebook in terminal gives -bash: jupyter: command not found. /Users/<username>/bin/jupyter notebook does execute, but I get ImportError: No module named markupsafe despite the fact that /usr/local/lib/python2.7/site-packages/MarkupSafe-0.23.dist-info exists.
EDIT: I got jupyter notebook working eventually. I had to install several packages from the source tarballs directly, including MarkupSafe, functools32, and jsonschema. Maybe Python is not looking in the correct folder or something.

Categories

Resources