Trying to resolve ModuleNotFoundError: No module named - python

Trying to install third party modules on my mac using pip and I am getting this error (Using IDLE as the Python shell):
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
(Have tried multiple modules and all return the same error)
I have installed the modules using:
pip3 install pyperclip
I can see that it has installed into the directory:
/opt/homebrew/lib/python3.9/site-packages
Ive tried going through several of the posts posted on StackOverflow such as this and this but after trying their steps I cant seem to resolve it.
which -a pip3 gives:
/opt/homebrew/bin/pip3
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
/usr/local/bin/pip3
/usr/bin/pip3
which -a python3 gives:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/usr/local/bin/python3
/usr/bin/python3
print(sys.version) gives:
3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)]
pip --version gives:
pip 22.1.2 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
Running /usr/local/bin/python3 test1.py:
Traceback (most recent call last):
File "/Users/dxz/Downloads/test1.py", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Thanks

You can run this command for seeing pip3 uses which python:
pip3 --version
But i guess you are not choosing right python to run your .py file
if you run your .py like this:
python3 main.py
You must be sure pip3 installing libraries to 'python3' linked python or you can run your main.py file with one of pip3 linked python
I mean if pip3 --version gives you
/usr/bin/python3
this python path you can run your main.py like this
/usr/bin/python3 main.py
but if you want to use best practice i recommend to you using virtualenv

Related

netplan not working after change default python

in ubuntu 18.04, when i change default python from python 3.6 to other version by this command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
or when i remove python 3.6 and install other version netplan apply not working and result this error:
File "/usr/sbin/netplan", line 20, in <module>
from netplan import Netplan
File "/usr/share/netplan/netplan/__init__.py", line 18, in <module>
from netplan.cli.core import Netplan
File "/usr/share/netplan/netplan/cli/core.py", line 24, in <module>
import netplan.cli.utils as utils
File "/usr/share/netplan/netplan/cli/utils.py", line 25, in <module>
import netifaces
ModuleNotFoundError: No module named 'netifaces'
and command pip install netifaces has some errors.
I faced the same issue before with vagrant. If you use update-alternatives to make python3 alias points to another version of Python, vagrant will not work. You cannot use update-alternatives to change the alias of Python3.
For some reason I lost my python configuration after an update made by my ubuntu server,solved by:
Checking if I could import the module so on CLI:
python
import python
After getting the same message I realize my python environment was falling to use the modules even when it all showed up as installed I went ahead and "upgrade" the python modules:
python pip install --upgrade pip
python pip install --upgrade netifaces
python pip install --upgrade "any module you need to use for your script"
And just like that modules were recognized updated and properly install.
tl;dr: Create a link netifaces.so to netifaces.cpython-36m-x86_64-linux-gnu.so in /usr/lib/python3/
In /usr/lib/python3/dist-packages/ is only a module for python3.6:
vagrant#ubuntu18.04:~$ find /usr/lib/python3/dist-packages/ -type f -name netifaces\*
/usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gnu.so
That's why importing 'netifaces' failes for python3.8 while it works for python3.6:
vagrant#ubuntu18.04:~$ python3.6 -c 'import netifaces; print("works!")'
works!
vagrant#ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'netifaces'
One can link to it with a more unspecific name so python3.8 can find and use it:
vagrant#ubuntu18.04:~$ sudo ln -s /usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gn
u.so /usr/lib/python3/dist-packages/netifaces.so
vagrant#ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
works!
Hint: I had to do the same for apt_pkg.so → apt_pkg.cpython-36m-x86_64-linux-gnu.so

Installing openpyxl with python2.7 results in "ImportError : no module named util"

I have Python 2.7.14. I know that Python 2 is deprecated but I don't have choice for the moment.
I tried to install openpyxl library.
I downloaded and extracted the .tar.gz. The version downloaded is 3.0.3.
When I run python setup.py install, I'm facing the following error:
Traceback (most recent call last):
File "setup.py", line 28, in <module>
from importlib.util import module_from_spec, spec_from_file_location
ImportError: No module named util
How can I solve this issue for Python 2.7.14?
From the openpyxl release history, the last version compatible with Python2.7 is 2.6.4:
https://openpyxl.readthedocs.io/en/stable/changes.html#id16
2.6.4 (2019-09-25)
Final release for Python 2.7 ––––––––––––––––––––––––––––
Starting openpyxl 3.0.0, it is Python-3 only.
So, you need to install a specific older version.
Using pip:
$ python -V
Python 2.7.17
$ python -m pip install openpyxl==2.6.4
Using the downloadable tar.gz from PyPi:
$ wget https://files.pythonhosted.org/packages/d6/26/eb28e975b7a37aad38d7ec4f7a0f652bdee6ecf36e6bd06f473c5af9b87b/openpyxl-2.6.4.tar.gz
$ mkdir openpyxl-2.6.4
$ tar -xvzf openpyxl-2.6.4.tar.gz -C openpyxl-2.6.4
$ cd openpyxl-2.6.4
$ python setup.py install

ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import)

Looks like I have broken my python installation when I wanted to switch to python 3.8. Using Ubuntu 18.04. Trying to use the gi, gives the following error:
$ python
Python 3.8.1 (default, Dec 31 2019, 18:42:42)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import GLib, Gio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
Tried running update-alternatives for python, but it tells me there is only one python alternative configured (3.8).
Tried to reinstall python3-gi and python3.8. Still the same problem
I had the same issue. I linked python3 to python3.6, for me it was pointing to 3.8. That solved the issue.
cd /usr/bin/
rm python3
ln -s python3.6 python3
Thats all. Now my system started working fine.
Install gi for python 3.8: python3.8 -m pip install pgi
Then, instead of import gi use:
import pgi
pgi.install_as_gi()
from gi.repository import GLib, Gio
Alternatively, you can force install PyGObject for python 3.8:
sudo python3.8 -m pip install --ignore-installed PyGObject
which should allow one to from gi import ... as before.
For me the workaround was to create a symlink:
cd /usr/lib/python3/dist-packages/gi/
sudo ln -s _gi.so _gi.cpython-38-x86_64-linux-gnu.so
and it solved the problem for me.
I had the same problem on ubuntu 18 as python3 was referring to python3.9.
In order to solve it, I changed the alternative for python3:
sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.9 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/bin/python3.9 2 manual mode
By choosing number 1, now python3 points to python3.6 and everything works fine again
Found answer here https://bugzilla.redhat.com/show_bug.cgi?id=1709787:
The cause is - /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so has incorrect name:
sh-5.0# python3 -c 'from gi.repository import GLib'
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python3.8/site-packages/gi/init.py", line 42, in
from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib64/python3.8/site-packages/gi/init.py)
sh-5.0# mv /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
sh-5.0# python3 -c 'from gi.repository import GLib'
Note that since 3.8.0a4, the "m" is not supposed to be there. Is it somehow hardcoded?
sh-5.0# python3-config --extension-suffix
.cpython-38-x86_64-linux-gnu.so
in my case it was
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-38-x86_64-linux-gnu.so
The reason of this error is that this app can't find the matched Python version of _gi_cairo.cpython-(version)-x86_64-linux-gnu.so.
Normally, this unmatched situation is caused by some wrong mixed usage of different versions of Python.
So basically, you can try to switch your Python version ( to the default version of your OS). Or you can go to '/usr/lib/python3/dist-packages/gi' and create a new .so library file:
cp _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi_cairo.cpython-(new version)-x86_64-linux-gnu.so
or
ln -s _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi.so
Have same issue, can't load module from script folder. In my case work if i changing workdir for example (but only for inline command):
cd /tmp; python -c 'from gocd import Server'
echo $?
0
After copy script to /tmp, it's does't work
cp gocd.py /tmp
cd /tmp; python -c 'from gocd import Server'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/gocd.py", line 3, in <module>
from gocd import Server
ImportError: cannot import name 'Server' from partially initialized module 'gocd' (most likely due to a circular import) (/tmp/gocd.py)

Pip install is not working with python but working with python2

I have an issue with Python on my Mac with High Sierra.
After running pip install pysal, I have a successful installation. However, the module is not found when I run in python:
> import pysal
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named pysal
I do not face such errors when running the same command in python2. However, most of my python scripts call the interpreter at the beginning of the file with #!/usr/bin/env python, so I would prefer not having to rely on python2 (I don't know how to do it either).
I figured out that this is python's executable is not on the same location where pip installs the modules:
macbook-pro-3:~ ME$ which -a python
/usr/bin/python
macbook-pro-3:~ ME$ which -a pip
/usr/local/bin/pip
I tried to follow the solution proposed by #J0ANMM in Modules are installed using pip on OSX but not found when importing . It recommeds downloading the script get-pip.py and executing it with sudo /usr/bin/python get-pip.py. I tried, but it did not change anything (the previous which commands still yield the same thing).
How do I manage to make pip work with my version of python?

Pip3.5 points to old Python 3.4.2 after Python 3.5 installation

I have installed Python 3.5.4 alongside python 3.4.2.
Calling pip with python 3.4 works fine, that is:
>>pip3.4
>>py -3.4 -m pip
Trying to use the python 3.5 version of pip (which I have ticked to install during the installation) with:
>>pip
>>python -m pip
>>py -3.5 -m pip
All lead to this error:
Could not import runpy module
Traceback (most recent call last):
File "C:\Python34\Lib\runpy.py", line 14, in <module>
import importlib.machinery # importlib first so we can test #15386 via -m
File "C:\Python34\Lib\importlib\__init__.py", line 34, in <module>
_w_long = _bootstrap._w_long
AttributeError: module 'importlib._bootstrap' has no attribute '_w_long'
This is on a Windows 7 64bit PC. I have no idea why specifically calling the python 3.5 version of pip makes it to access python 3.4's directory as in "C:\Python3.4". Help?
have you try to configure the environment variable properly?
See this link:
python and pip windows configuration

Categories