Python PIP package installed not importing - python

I have created a package via Github's pip instance, it is successfully installed:
Running setup.py install for lsm-shared ... done
Successfully installed lsm-shared-0.1.0
% pip list | grep shared
lsm-shared 0.1.0
If I start python and manually import it then it works fine:
% python
Python 3.9.6 (default, Jul 5 2021, 01:04:38)
>>> import lsm_shared
If I try and use the package in a project I get:
% quart run -p 3030
File "Development/lsm/servie/service/application.py", line 8, in <module>
import lsm_shared
ModuleNotFoundError: No module named 'lsm_shared'
The only difference between running manually and in a project is that I set PYTHONPATH, I don't know if that makes any difference.
I have looked at other solutions, including this StackOverflow question, and done the pip install with --user to no avail.

Related

Python3 import error: No module named 'rpm'

On a CentOS 7 I have installed python3 with the following command:
yum install -y python3
However, when I call import rpm, I get this error:
File "\<stdin>", line 1, in \<module>
ModuleNotFoundError: No module named 'rpm'
So how do I import the python3 rpm module?
Running rpm --version returns
RPM version 4.11.3
Accessing python (2.7.5) and running the import command works. So I could call a python2 script from python 3, as described here but this just feels wrong.
There are similar questions to this but they all relate to the rpm module being unavailable from Python2 and I don't have that problem. None deal with making the rpm module available in Python3.
Did you install the rpm python package for the python3 distribution?
pip3 install rpm-py-installer
Per the homepage of RPM this seems to be a required step.

import jwt ImportError: No module named jwt

I have been trying to run this project
https://github.com/udacity/FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS
I installed all the dependencies.
I still did not make any adjustments. I need to run it first
but I get this error when I type the command
python main.py
this is the error i get:
Traceback (most recent call last):
File "main.py", line 8, in <module>
import jwt
ImportError: No module named jwt
I worked with similar errors before and managed to solve them but not with this one I could not figure out the source of the problem
Check if PyJWTY is in the requirements file or if is installed in you system, using: pip3 install PyJWT
You could also face this error if you have running on your machine two versions of python. So the correct command will be python3 main.py
I have hit the same issue with pyjwt 2.1.0 which was clearly installed in my venv as well as globally. What helped was to downgrade it to version 1.7.1
pip install "PyJWT==1.7.1"
run the app and then to reinstall newest version 2.1.0
pip install "PyJWT==2.1.0"
And the issue disappeared.
This project has requirements that need to be installed for it to work. These can be installed via pip, pip install -r requirements.txt (I've linked to the requirements file in the project), which you can read more about here.
What worked for me was using import jwt instead of import PyJWT. I am using version PyJWT-2.3.0.
jwt image on vscode
As you can see no errors in the above screenshot. The app runs without import errors.
Image of terminal
Faced the same issue. Am using a guest VM running ubuntu 16.04.
I have multiple versions of python installed - both 3.5 and 3.7.
After repeated tries with and without using virtualenv what worked finally is:
Create a fresh virtual environment using :
priya:~$ virtualenv -p /usr/bin/python3.7 fenv
Activate the virutal environment :
priya:~$ source ./fenv/bin/activate
Note : You can find the path for python3.7 by using whereis python:
priya:~$ whereis python
python: /usr/bin/python /usr/bin/python3.5m /usr/bin/python3.5 /usr/bin/python3.7 /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python2.7 /usr/bin/python3.7m /usr/bin/python2.7-config /usr/lib/python3.5 /usr/lib/python3.7 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python3.7 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python3.7 /usr/local/lib/python2.7 /usr/include/python3.5m /usr/include/python3.5 /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
Referenced link is : https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv#:~:text=By%20default%2C%20that%20will%20be,%2Flocal%2Fbin%2Fpython3
For your project - FSWD Nanodegree -
After you have activated your virtualenv, run pip install -r requirements.txt
You can test by :
(fenv) priya:FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS :~$ python
Python 3.7.9 (default, Aug 18 2020, 06:24:24)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
import jwt
exit()
pip3 install flask_jwt_ex.. I was doing this without sudo. And then I was working on the program as sudo.
You have to have only PyJWT installed and not JWT. Make sure you uninstall JWT (pip uninstall JWT) and install PyJWT (pip install PyJWT)

Import runtime installed module using pip in python 3

I want to install and import Python 3 modules at runtime.
I'm using the following function to install modules at runtime using pip:
def installModules(modules):
for module in modules:
print("Installing module {}...".format(module))
subprocess.call([sys.executable, "-m", "pip", "install", "--user", module])
The module is installed successfully, but I'm not able to import it at runtime, after the installation finishes. So if I do:
modules = [ "wget", "zipfile2" ]
installModules(module)
import wget
I get a ModuleNotFoundError. If, after that, I start another Python 3 session, I am able to use the modules e.g. wget, which means that the modules have been installed, but they are not available for this current Python 3 session.
Is it possible in Python 3 to install and then import the installed modules in the same Python 3 session i.e. right after installation?
Thank you!
EDIT:
On a fresh Ubuntu 19.04 install inside VirtualBox, after a sudo apt-get install python3-pip, running the following script:
import os, sys
import subprocess
def installModules(modules):
for module in modules:
print("Installing module {}...".format(module))
subprocess.call([sys.executable, "-m", "pip", "install", "--user", module])
def process():
modulesToInstall = [ "wget", "zipfile2" ]
installModules(modulesToInstall)
process()
import wget
def main():
wget.download("http://192.168.2.234/test/configure.py")
if __name__ == "__main__":
main()
I get:
user#user-VirtualBox:~$ python3 script.py
Installing module wget...
Collecting wget
Installing collected packages: wget
Successfully installed wget-3.2
Installing module zipfile2...
Collecting zipfile2
Using cached https://files.pythonhosted.org/packages/60/ad/d6bc08f235b66c11bbb76df41b973ce93544a907cc0e23c726ea374eee79/zipfile2-0.0.12-py2.py3-none-any.whl
Installing collected packages: zipfile2
Successfully installed zipfile2-0.0.12
Traceback (most recent call last):
File "script.py", line 17, in <module>
import wget
ModuleNotFoundError: No module named 'wget'
The Python 3 version is:
user#user-VirtualBox:~$ python3 --version
Python 3.7.3
The pip3 version is:
user#user-VirtualBox:~$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
Other info:
user#user-VirtualBox:~$ whereis python3
python3: /usr/bin/python3.7m /usr/bin/python3.7-config /usr/bin/python3.7 /usr/bin/python3 /usr/bin/python3.7m-config /usr/lib/python3.7 /usr/lib/python3.8 /usr/lib/python3 /etc/python3.7 /etc/python3 /usr/local/lib/python3.7 /usr/include/python3.7m /usr/include/python3.7 /usr/share/python3 /usr/share/man/man1/python3.1.gz
Any ideas?
By default, at startup Python adds the user site-packages dir (I'm going to refer to it as USPD) in the module search paths. But this only happens if the directory exists on the file system (disk). I didn't find any official documentation to support this statement 1, so I spent some time debugging and wondering why things seem to be so weird.
The above behavior has a major impact on this particular scenario (pip install --user). Considering the state (at startup) of the Python process that will install modules:
USPD exists:
Things are straightforward, everything works OK
USPD doesn't exist:
Module installation will create it
But, since it's not in the module search paths, all the modules installed there won't be available for (simple) import statements
When another Python process is started, it will fall under #1.
To fix things, USPD should be manually added to module search paths. Here's how the (beginning of the) script should look like:
import os
import site
import subprocess
import sys
user_site = site.getusersitepackages()
if user_site not in sys.path:
sys.path.append(user_site)
# ...
Update #0
1 I just came across [Python]: PEP 370 - Per user site-packages directory - Implementation (emphasis is mine):
The site module gets a new method adduserpackage() which adds the appropriate directory to the search path. The directory is not added if it doesn't exist when Python is started.

R-markdown: the python-logging package seems to cause the problem

I am facing a strange problem which I could track down to the python logging package. Let me shortly explain what I want to do: the goal is to create html reports with python. I am using the R rmarkdown package which runs python code trough reticulate using a local virtualenv.
The Problem:
As soon as I install the python logging package rmarkdown runs into a problem when loading matplotlib. I have written a small test script to reproduce the example.
My system:
Ubuntu 18.04 bionic
Python 2.7.15rc1
Test "1" script (without logging):
Create a new virtualenv (venv).
Use venv/bin/pip to install matplotlib.
Run reticulate::import (at the end via rmarkdown::render).
Test "2" script (with logging):
Create a new virtualenv (venv).
In addition to the first test: install logging via venv/bin/pip.
Use venv/bin/pip to install matplotlib.
Run reticulate::import (at the end via rmarkdown::render).
The modules installed (virtualenv):
backports.functools-lru-cache 1.5
cycler 0.10.0
kiwisolver 1.0.1
logging 0.4.9.6 <- only for "test 2"
matplotlib 2.2.3
numpy 1.15.1
pip 18.0
pkg-resources 0.0.0
pyparsing 2.2.0
python-dateutil 2.7.3
pytz 2018.5
setuptools 40.2.0
six 1.11.0
subprocess32 3.5.2
wheel 0.31.1
The system site packages do have the same module version.
Results:
All tests from test 1 (without logging) work nicely.
The tests from test 2 (with loging) fail when using the virtualenv. When calling rmarkdown::render (see below), when using the system python installation (not virtualenv) they work nice as well.
There seem to be something strange with reticulate when logging is installed in a virtualenenv.
The output of the test script (see below):
The full output including the error:
----------- no logging package installed ------------
Module(matplotlib)
Module(matplotlib)
--------- with logging package installed ------------
Error in py_module_import(module, convert = convert) :
AttributeError: 'module' object has no attribute 'NullHandler'
Detailed traceback:
File "/home/retos/Downloads/venvtest/venv/lib/python2.7/site-packages/matplotlib/__init__.py", line 168, in <module>
_log.addHandler(logging.NullHandler())
Calls: <Anonymous> -> py_module_import -> .Call
Execution halted
Module(matplotlib)
The Module(matplotlib) output is the success message of loading the module via reticulate::import. As one can see only the one test fails where the virtualenv is used with installed logging python module.
Anyone having an idea what could case these problems? I spent quite some time to identify the source of the error, but I am kind of lost now ...
Test script to reproduce the error:
Here is a small bash/shell script to reproduce my tests.
#!/bin/bash
# New virtual environment and install matplotlib
echo " ----------- no logging package installed ------------"
if [ -d venv ] ; then rm -rf venv ; fi
virtualenv venv &>/dev/null > /dev/null
venv/bin/pip install matplotlib > /dev/null
# Print installed packages
Rscript -e "reticulate::use_python('venv/bin/python'); reticulate::import('matplotlib')"
Rscript -e "reticulate::import('matplotlib')"
# New virtual environment and install logging and matplotlib
echo " --------- with logging package installed ------------"
if [ -d venv ] ; then rm -rf venv ; fi
virtualenv venv > /dev/null
venv/bin/pip install logging > /dev/null
venv/bin/pip install matplotlib > /dev/null
# Print installed packages
Rscript -e "reticulate::use_python('venv/bin/python'); reticulate::import('matplotlib')"
Rscript -e "reticulate::import('matplotlib')"
I first thought it is related to the problem "ImportError: cannot import name cbook" but the solution there did not work.
May thanks in advance!
R
Logging became a standard module included in Python library in version 2.3. You must not install it from PyPI. Remove it ASAP:
pip uninstall logging

ImportError: cannot import name wraps

I'm using python 2.7.6 on Ubuntu 14.04.2 LTS. I'm using mock to mock some unittests and noticing when I import mock it fails importing wraps.
Not sure if there's a different version of mock or six I should be using for it's import to work? Couldn't find any relevant answers and I'm not using virtual environments.
mock module says it's compatible with python 2.7.x: https://pypi.python.org/pypi/mock
mock==1.1.3
six==1.9.0
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mock import Mock
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/mock/__init__.py", line 2, in <module>
import mock.mock as _mock
File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 68, in <module>
from six import wraps
ImportError: cannot import name wraps
also tried with sudo with no luck.
$ sudo python -c 'from six import wraps'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: cannot import name wraps
Installed mock==1.0.1 and that worked for some reason. (shrugs)
edit: The real fix for me was to updated setuptools to the latest and it allowed me to upgrade mock and six to the latest. I was on setuptools 3.3. In my case I also had to remove said modules by hand because they were owned by OS in '/usr/local/lib/python2.7/dist-packages/'
check versions of everything
pip freeze | grep -e six -e mock
easy_install --version
Update everything
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
pip install mock --upgrade
pip install six --upgrade
Thanks #lifeless
I encountered the same issue on my mac, which I was able to fix by realizing that my python's sys.path contained both
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/
and
/Library/Python/2.7/site-packages/
with the former earlier than the latter.
You can test if this is happening to you by running the following in the python console.
import six
six.__version__
my python was loading an outdated six.py from the former directory (which didn't have wrapper), even though pip had installed a newer version six in the second directory. (It seems mac's framework comes with a version of six by default.)
I was able to fix it by moving six.py and six.pyc out of the first directory (requires sudo access), so that python would find the newer version of six in the second directory. I'm sure you could also change the ordering of the paths in sys.path.
To find the older version of six that need to be deleted run this from the terminal console
find /System/Library/Frameworks/Python.framework/Versions -name six.py*
so mock 1.1.1 and above defines a versioned requirement on six 1.7 or above:
https://github.com/testing-cabal/mock/blob/master/requirements.txt#L6
This gets reflected into setuptools metadata by pbr, which there is a versioned setup_requires dependency on:
https://github.com/testing-cabal/mock/blob/master/setup.py#L17
So there are a couple of possibilities:
1) six 1.7 is not new enough
2) there's a distro six package claiming to be 1.9.0 that doesn't have wraps for some reason
3) the setuptools in use didn't integrate properly with pbr and deps are missing
4) the wheel metadata isn't being interrogated properly by your pip/setuptools combination.
We do have a hard requirement for setuptools 17.1, and that was only explicitly reported by setup.py more recently. I'd love it if you can figure which of these is the case and update https://github.com/testing-cabal/mock/issues/298 so that we can fix whatever interaction is leading to this silent failure of setup.py / wheels.
On Mac OSX, the previously installed version of six was blocking my upgraded version from being used. I verified this, as previously suggested by running the following in my interpreter:
import six
six.__version__
To fix this I moved the file:
mv/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py
/tmp/old_six.py
This is stated already in another answer on this site, but I wanted to provide a more streamlined response.
I originally had an issue with old "OS-owned" versions of and pip/setuptools. After I installed pip manually, like so:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo ln -s /usr/local/bin/pip /usr/bin/pip
And then installing the latest version of pip, mock and six, I still had the problem you've described above. Turns out that I had six installed twice in:
/usr/lib/python2.7/dist-packages/
and in
/usr/local/lib/python2.7/dist-packages/
After I removed the six from /usr/lib/ it worked fine:
rm /usr/lib/python2.7/dist-packages/*six*
I did a pip install of six==1.9.0 and it took the new version. It seems like mock==1.3.0 doesn't properly define the version of six that it needs to get wraps support.
Another solution is setting your PYTHONPATH environment variable to point to the installed packages.
Setting my environment variable in my bash config so that:
PYTHONPATH=/Library/Python/2.7/site-packages
Allowed me to run tests in terminal (without removing/renaming any libraries, etc).
However, when using PyCharm, it was less-than-helpfully not correctly importing this environment variable. Even though PyCharm was showing as including parent variables (with that listed in the ones it showed importing), it seems this import wasn't working correctly.
Manually setting the environment variable to the above in the PyCharm run configuration resolves this.
I am unsure if PyCharm overwrites the PYTHONPATH variable after importing it from system environment variables or some other trickery, but this did resolve the error for me.
Though you aren't using a virtual environment like virtualenv, it's certainly a great use case for it. By sandboxing your Python installation and all the dependencies for your project, you can avoid hacking away at the global/default python installation entirely, which is where a lot of the complexity/difficulty comes from.
This is what I used when I got the wraps error - requirements.txt contains mock==2.0.0 and six==1.10.0:
cd <my_project>
virtualenv venv
source venv/bin/activate
sudo pip install -r requirements.txt
Not only is this simpler to use in my opinion, it's also simpler to document for people who might want to run your code.
I found a interesting things!
There is a file named "functools.py" in my project root path, and while I run my project , pycharm will raise ImportError.
So I rename my file fix this problem~~

Categories

Resources