ModuleNotFoundError after successful package installation - python

I am trying to install and use new Python packages e.g. mysql:
pip install mysql
Collecting mysqlNote: you may need to restart the kernel to use updated packages.
Using cached mysql-0.0.3-py3-none-any.whl (1.2 kB)
Collecting mysqlclient
Using cached mysqlclient-2.1.0-cp38-cp38-win_amd64.whl (180 kB)
Installing collected packages: mysqlclient, mysql
Successfully installed mysql-0.0.3 mysqlclient-2.1.0
However, if I run the code I receive the following error:
runfile('untitled1.py', wdir='')
Traceback (most recent call last):
File "untitled1.py", line 1, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
Also restarting the Kernel does not solve the problem:
Python 3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 7.29.0 -- An enhanced Interactive Python.
Restarting kernel...
I am using Spyder 5.0.0 on a fresh Anaconda 2.0.3 installation.

First, I'm not familiar with Anaconda, although from a quick search I don't think that should change anything about my answer.
From the PyPI description for mysql:
This package is a ‘virtual package’, which requires MySQL-python
(Python 2) or mysqlclient (Python 3) to install. In effect, this means
‘pip install mysql’ will actually install MySQL-python.
Since you're on Python 3, what it installs is mysqlclient. This is confirmed by the pip output Installing collected packages: mysqlclient, mysql. From the docs for that package, you can see that you have to import MySQLdb rather than mysql.

Related

Trying to test my python library, it's installed but not found

When I try to install my library locally and test it, it shows up as installed:
PS C:\Projects\pypi\potatoutils> pip install -e .
Obtaining file:///C:/Projects/pypi/potatoutils
Installing collected packages: potatoutils
Attempting uninstall: potatoutils
Found existing installation: potatoutils 0.0.1
Uninstalling potatoutils-0.0.1:
Successfully uninstalled potatoutils-0.0.1
Running setup.py develop for potatoutils
Successfully installed potatoutils
But when I try to test it
PS C:\Projects\pypi\potatoutils> python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import potatoutils
I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'potatoutils'
What am I doing wrong?
EDIT: pip and python have the same version (3.7) and their paths match.
And the library shows up when I use pip list.
Often this is the issue if you pip install a package with pip for python2.x
but then when you attempt to import your library for python3.x
Make sure you're running installing pip packages for the correct python version.
use pip3 using pip sometimes point to python 2.x versions, that's why maybe it can help!
Okay, so I managed to get the library up and running by starting over and following these instructions:
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f
I still don't know what went wrong the first time (maybe outdated instructions?) but it no longer matters to me.

No module named 'dbus.mainloop.pyqt5' even if all dependencies seem to be installed

I am trying to use DBUS as the main loop of PyQt5.
System-wide
I installed, system-wide (with apt, LinuxMint 19, amd64), the following dependencies:
python3-pyqt5 Version: 5.10.1+dfsg-1ubuntu2
python3-dbus Version: 1.2.6-1
python3-dbus.mainloop.pyqt5 Version: 5.10.1+dfsg-1ubuntu2
Trying it:
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dbus.mainloop.pyqt5 import DBusQtMainLoop
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dbus.mainloop.pyqt5'
In a virtual env
I tried in a venv too. However, I had to accept system-wide packages, because I can't find a pip equivalent of python3-dbus.mainloop.pyqt5.
virtualenv --python=/usr/bin/python3 ~/.venvs/python3/testqtdbus5 --system-site-packages
source ~/.venvs/python3/testqtdbus5/bin/activate
$ pip install "pyqt5==5.10" dbus-python
Collecting pyqt5==5.10
Using cached https://files.pythonhosted.org/packages/ae/4b/c7315ba7a266d493ee50c4597b1b4dea2348896a49115b5192b21adf1a47/PyQt5-5.10-5.10.0-cp35.cp36.cp37-abi3-manylinux1_x86_64.whl
Requirement already satisfied: dbus-python in /usr/local/lib/python3.6/dist-packages (1.2.8)
Collecting sip<4.20,>=4.19.4 (from pyqt5==5.10)
Using cached https://files.pythonhosted.org/packages/8a/ea/d317ce5696dda4df7c156cd60447cda22833b38106c98250eae1451f03ec/sip-4.19.8-cp36-cp36m-manylinux1_x86_64.whl
ERROR: pyqtwebengine 5.12.1 has requirement PyQt5>=5.12, but you'll have pyqt5 5.10 which is incompatible.
Installing collected packages: sip, pyqt5
Found existing installation: PyQt5 5.12
Not uninstalling pyqt5 at /usr/local/lib/python3.6/dist-packages, outside environment /home/vince/.venvs/python3/testqtdbus5
Can't uninstall 'PyQt5'. No files were found to uninstall.
Successfully installed pyqt5-5.10 sip-4.19.8
Trying:
$ ipython
/home/vince/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:925: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from dbus.mainloop.pyqt5 import DBusQtMainLoop
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-ad501d00de5f> in <module>
----> 1 from dbus.mainloop.pyqt5 import DBusQtMainLoop
ModuleNotFoundError: No module named 'dbus.mainloop.pyqt5'
Actually I want pyqt v5.12, for the WebEngine.
$ pip install "pyqt5==5.12.2" dbus-python
Collecting pyqt5==5.12.2
Downloading https://files.pythonhosted.org/packages/6a/f4/6a63aafcee3efd2b156dc835d9c85ca99b24e80f8af89b6da5c46054fe43/PyQt5-5.12.2-5.12.3-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl (61.5MB)
|████████████████████████████████| 61.5MB 362kB/s
Requirement already satisfied: dbus-python in /usr/local/lib/python3.6/dist-packages (1.2.8)
Requirement already satisfied: PyQt5_sip<13,>=4.19.14 in /home/vince/.local/lib/python3.6/site-packages (from pyqt5==5.12.2) (4.19.15)
Installing collected packages: pyqt5
Found existing installation: PyQt5 5.10
Uninstalling PyQt5-5.10:
Successfully uninstalled PyQt5-5.10
Successfully installed pyqt5-5.12.2
$ ipython
/home/vince/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:925: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from dbus.mainloop.pyqt5 import DBusQtMainLoop
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-ad501d00de5f> in <module>
----> 1 from dbus.mainloop.pyqt5 import DBusQtMainLoop
ModuleNotFoundError: No module named 'dbus.mainloop.pyqt5'
Version of dbus.mainloop.init.py
In [2]: import dbus.mainloop
In [3]: help(dbus.mainloop)
# File location is: /usr/local/lib/python3.6/dist-packages/dbus/mainloop/__init__.py
So it seems the system version is used. Ok.
What am I missing ? Is there a pip package for the dbus mainloop ? Thanks.
edit: So far, I can work with the default
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
But if Gtk is now a dependency, that isn't satisfactory.
So far, I can work with the default glib mainloop:
EDIT: this won't work on other platforms than Linux (tested with MacOS).
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
glib is decoupled from Gtk, so it's alright I guess.

Python Modules installing properly but dont exist in editors

I want to start with I am new to the python language and am learning on my own. My issue is that I am installing the modules that I'm trying to use. The installs are successful but do not exist when I try to import them. I am using Visual Studio Code, Python 3.6, and Anaconda on Windows 10.
I am trying to use pyttsx3, SpeechRecognition and pocketsphinx.
$pip list
This gives me a very large list but does not contain either of my installed modules.
$python -m pip list
Gives me smaller output with them.
Package Version
----------------- ---------
beautifulsoup4 4.6.0
bs4 0.0.1
certifi 2018.4.16
chardet 3.0.4
click 6.7
gTTS 2.0.0
gTTS-token 1.1.1
idna 2.6
pip 10.0.1
pocketsphinx 0.1.3 #Here
pypiwin32 223
pyttsx 1.1
pyttsx3 2.7 #Here
pywin32 223
requests 2.18.4
setuptools 39.1.0
six 1.11.0
SpeechRecognition 3.8.1 #Here
urllib3 1.22
wheel 0.31.0
I know their installed but when I try to use any of the imports.
$python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyttsx3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pyttsx3'
I am fine with this being something small on my part due to my lack of knowledge with this language, but I have done a full days worth of research trying to solve this myself.
Edit:
$pip --version
pip 9.0.1 from C:\Users\Keiro\Anaconda3\lib\site-packages (python 3.6)
You have two versions of pip perhaps two different installations of python as well. One with Anaconda and other one by main site.
Stick to one version and if you install in that use that environment in Visual Studio(Or VS Code)
You have installed those in anaconda which the standard installtion of Python wont recognize..
Open Python prompt from Anaconda Navigator >> Choose the environment (Select base for default) >> Open with Terminal [Or Open with Python]
>>python
>>import pyttsx3
In Visual Studio code set the same environment
Ctrl+Shift+P >> Select Python interpreter
Choose the anaconda location.
Volla. Now it should work
I had the same problem and found the answer in an old forum.
Write "pip3" instead of "pip".
So the code is:
pip3 install pyttsx3
(Source: bash: pip: command not found)

Module import error in virtualenv but pip list --local shows it

I'm wondering about the following importError which I can't get ride off. I have a local virtualenv (venv) in which I've installed multiple modules:
(venv) stam#stam:~$ pip list --local
cryptography (1.5.2)
cvxopt (1.1.9)
cycler (0.10.0)
Cython (0.25.1)
matplotlib (1.5.3)
more-itertools (2.2)
ndg-httpsclient (0.4.2)
numpy (1.11.2)
pandas (0.19.0)
paramiko (1.15.2)
Pillow (2.8.1)
pip (8.1.2)
psycopg2 (2.6)
python-apt (0.9.3.12)
python-dateutil (2.5.3)
python-debian (0.1.27)
scipy (0.18.1)
setuptools (28.6.1)
simplejson (3.8.2)
urllib3 (1.9.1)
wheel (0.24.0)
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) stam#stam:~$
now starting a python interpreter and trying to import cvxopt (note it is in the list above) gives me a importError. Why is this? I'm using the right python version, I work on the venv so everything should work out of the box.
(venv) stam#stam:~$ python
Python 2.7.9 (default, Aug 13 2016, 16:41:35)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cvxopt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cvxopt
>>>
I'm a bit lost and don't know how exactly I can resolve this issue. It seems the right python version is started, at least the one I've attached to the virtualenv.
Additional outps asked for in the comments
(venv) stam#stam:~$ which python
/usr/bin/python
(venv) stam#stam:~$ which pip
/usr/local/bin/pip
Based on the output from which it seems clear that your python and pip belong to two different instances. So all libraries you install will be installed for some other python instance.
The acute fix is to force pip to run through your specified interpreter:
python -m pip list --local
python -m pip install cvxopt

Unable to import praw module after install

Edit: Before you start deleting/modifying installs, please glance over StvnW's answer/summary to make sure you are applying the solution that is appropriate for you.
I've installed python 2.7.5 and pip [edit: mac OSX Mountain Lion.] I've run "pip install praw" in terminal. All good. When I run python and run "import praw" I get:
...$ python
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import praw
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named praw
...same ImportError from a script.
when I install praw I get this:
$ pip install praw
Downloading/unpacking praw
Downloading praw-2.1.10.tar.gz (83kB): 83kB downloaded
Running setup.py egg_info for package praw
Requirement already satisfied (use --upgrade to upgrade): requests>=1.2.0 in
/usr/local/lib/python2.7/site-packages (from praw)
Requirement already satisfied (use --upgrade to upgrade): six in
/usr/local/lib/python2.7/site-packages (from praw)
Requirement already satisfied (use --upgrade to upgrade): update-checker>=0.6 in
/usr/local/lib/python2.7/site-packages (from praw)
Requirement already satisfied (use --upgrade to upgrade): setuptools in
/usr/local/lib/python2.7/site-packages/setuptools-1.1.6-py2.7.egg (from update-
checker>=0.6->praw)
Installing collected packages: praw
Running setup.py install for praw
Installing praw-multiprocess script to /usr/local/bin
Successfully installed praw
Cleaning up...
In python if I run help('modules') it isn't there.
Relatively new to python and I haven't been able to sort this out with google search. Any hints would be much appreciated.
Edit:
SitRep:
I've uninstalled 2.7.2, uninstalled praw, and uninstalled (homebrew) pip. I ran python 2.7.5 and it couldn't find the module (as you would suspect.) I then reinstalled pip with easy_install and now 2.7.5 is finding praw but giving this error:
$ python
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import praw
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/praw/__init__.py", line 43, in <module>
from update_checker import update_check
File "/Library/Python/2.7/site-packages/update_checker.py", line 11, in <module>
from pkg_resources import parse_version as V
ImportError: No module named pkg_resources
>>>
Thanks for the help so far, the spurious install was the root of problem 1.
The solution to the final problem can be found here:
No module named pkg_resources
I wish there were some way to summarize this for future readers, but I've done so many things that I no longer recall what addressed what. I basically uninstalled everything (python 2.7.2, 2.7.5, praw, pip,) reinstalled 2.7.5 from http://python.org/download/, reinstalled pip with easy_install (and sudo command) instead of homebrew, reinstalled praw with sudo command, and followed the directions for the subsequent module error in the link above. Hope that helps. :)
The OS X system versions of Python live in /System/Library/Frameworks/Python.Framework/<verson>, each of which links to /Library/Python/<version>/site-packages. Seems like you have installed another version of python and/or pip to /usr/local, but when you invoke python you are still getting the system version.
Try this:
$ /usr/local/bin/python
>>> import praw
I'd also recommend looking into tools like pyenv and virtualenv if you are going to do any amount of work with Python. Pyenv lets you easily manage and switch between multiple versions (including micro versions x.x.3, x.x.5, etc). Virtualenv lets you create isolated Python environments where you can pin down versions of site-packages specific to a particular project.
Edit (summarizing):
sudo easy_install pip will install pip under /System/Library/Python/<version>. Calling that pip will install packages to /Library/Python/<version>/site-packages
brew install python will install a second version of python — including pip — under /usr/local/. That pip will put packages in /usr/local/lib/python<version>/site-packages/`.
One version may not see packages installed to the other.
which python and which pip are helpful for troubleshooting.
Dean's final solution above results in user packages being installed to the system site-packages.
Despite Dean's choice to update the system Python, many would advocate instead using brew, pyenv, and virtualenv to isolate oneself from the system Python.
It's strange. I am suspecting you have another version of python2.7 on your mac os
If you are using a mac. Check the version of python on your shell's path using which python. Then make sure that the shebang line in the script you are trying to run uses the same version of python.
In my terminal I typed
which python and got the output /usr/local/bin/python
I went to the script I was trying to run and added the following to the first line
#!/usr/local/bin/python
Then I went back to my terminal. Checked that I was in the same directory as the script I wanted to run and typed
./my_script.py
If you don't see any output at this point make sure that you have execution permission enabled for your script by typing chmod +x my_script.py in ther terminal. Then try running the script again using ./my_script.py

Categories