can not import MySQLdb after I installed it in a virtualenv - python

system:ubuntu14.04, mysql:5.5, python:2.7.4
I have installed mysql-connector-python outside of a virtualenv using this command:
pip install mysql-connector-python --allow-external mysql-connector-python
I can use mysql-connector correctly outside of the virtualenv:
think#think-Inspiron-N4050:~$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
>>>
then I activated a virtualenv and installed the mysql-connector. I used this method(I downloaded this install package from the offical website):
think#think-Inspiron-N4050:~/myzone/MindFly$ source venv/bin/activate
(venv)think#think-Inspiron-N4050:~/myzone/MindFly$ cd ~/download/
(venv)think#think-Inspiron-N4050:~/download$ dpkg -i mysql-connector-python_2.0.4-1ubuntu14.04_all.deb
After installed it correctly, I can not import it:
(venv)think#think-Inspiron-N4050:~/download$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
I don't know why, then I had to install MySQLdb inside the virtualenv:
(venv)think#think-Inspiron-N4050:~/myzone/MindFly$ sudo apt-get install python-mysqldb
after finished it, I can not import it too
(venv)think#think-Inspiron-N4050:~/myzone/MindFly$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
then I installed MySQL-python and libmysqlclient-dev, it alse didn't work.
then I looked over the venv/lib/python2.7/site-packages:
(venv)think#think-Inspiron-N4050:~/myzone/MindFly/venv/lib/python2.7/site-packages$ ls
easy_install.py pip-7.1.2.dist-info wheel
easy_install.pyc pkg_resources wheel-0.24.0.dist-info
_markerlib setuptools
pip setuptools-18.2.dist-info
there are not mysql-connector or mysqldb, I find mysqlconnector in /usr/lib/python2.7/dist-packages when I tried to reinstall mysqlconnector,
(venv)think#think-Inspiron-N4050:~/myzone/MindFly$ sudo pip install mysql-connector-python --allow-external mysql-connector-python
[sudo] password for think:
Requirement already satisfied (use --upgrade to upgrade): mysql-connector-python in /usr/lib/python2.7/dist-packages
Cleaning up...
I really don't know how to do after I install mysql-connector-python,mysqldb,mysql-python and libmysqlclient-dev. I can use them outside of the virtualenv, but I still can not import mysql-connector nor mysqldb in virtualenv.
Please help me: how can I connect mysql in virtualenv? thanks very much. (if the expression is not clear, tell me and I will update it,thanks)

then I activated a virtualenv and installed the mysql-connector (...)
dpkg -i mysql-connector-python_2.0.4-1ubuntu14.04_all.deb
(...)
then I had to install MySQLdb inside the virtualenv:
sudo apt-get install python-mysqldb
Err... apt-get and dpkg are your distrib's package manager, this has nothing to do with pip and virtualenv. Your distrib's package manager installs system packages (.deb in your case) and install them system-wide. The point of virtualenv is to install python packages (with pip) in an isolated "virtual" environnement. What you want is:
# source path/to/your/venv/bin/activate
# pip install your-python-package
Note that all this is documented in virtualenv and pip...

Related

How to install pyaudio module in the current version of python while having multiple versions of python installed?

I have installed multiple versions of python like 3.8.5 and 3.9.2 in my Linux Ubuntu system. I want to install pyaudio module in 3.9.2 version.
For this, I have used a command: pip3 install PyAudio and I am getting this error - error: Microsoft Visual C++ 14.0 is required.
I have also used python unofficial libraries and tried to install pyaudio but it shows this error -
ERROR: PyAudio-0.2.11-cp39-cp39-win_amd64.whl is not a supported wheel on this platform.
Then I used these commands for the same -
sudo apt-get install libasound-dev portaudio19-dev libportaudio2
libportaudiocpp0
sudo apt-get install ffmpeg libav-tools
sudo pip install pyaudio
After this when I import pyaudio module, it shows module not found error. But when I tried to re-install the module, it shows requirement already satisfied.
Then I executed the command:
sudo -s
python3
import pyaudio
Now it shows no error. It seems that pyaudio module has been installed in python 3.8.5 version and not in python 3.9.2 version. However, I used virtual environment of older python version and installed speech recognition, pyaudio which worked well.
I want to use pyaudio module without using virtual environment without any errors. Please help.
Here is the screenshot of the issueimage_pyaudio_issue
Here is the code -
maaz#maaz-HP-Notebook:~$ python
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import pyaudio
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named pyaudio
import PyAudio
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named PyAudio
exit()
maaz#maaz-HP-Notebook:~$ sudo pip install pyaudio
[sudo] password for maaz:
Requirement already satisfied: pyaudio in /usr/lib/python3/dist-packages (0.2.11)
maaz#maaz-HP-Notebook:~$ sudo -s
[sudo] password for maaz:
root#maaz-HP-Notebook:/home/maaz# python3
Python 3.8.5 (default, May 27 2021, 13:30:53)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import pyaudio
exit()
root#maaz-HP-Notebook:/home/maaz# exit
exit
Check in the usr/bin and usr/local/bin for all python instances.
The installation of python3 is probably linked to python 3.8.5. But there might be a python3.9.
You could check for other python instances in the above folders by running the below commands:
ls -ls /usr/bin/python*
ls -ls /usr/local/bin/python*
If so, you can run the commands with python3.9, but i would recommend cleaning up the unused installations.

ModuleNotFoundError: No module named 'jpype'

I am using Anaconda Python and used the following to install jpype:
conda install -c conda-forge jpype1
I have GCC installed:
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
and I did not get any error during installation.
conda install -c conda-forge jpype1
Collecting package metadata: done
Solving environment: done
# All requested packages already installed.
If I run pip search jpype I will get the following:
jtypes.jpype (0.6.3b3) - A Python to Java bridge (ctypes/cffi-based JPype).
JPype1-py3 (0.5.5.2) - Python-Java bridge. Fork of the jPype project by Steve Menard (http://jpype.sourceforge.net/), with the modifications applied by Luis Nell
(https://github.com/originell/jpype)
but still cannot call the library
>>> import jpype
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'jpype'
It might be because it is installed as a Python 2 package and you're running Python 3 or the opposite.
To specifically install the package for Python 3, try entering this command:
pip3 install jpype1
or
python3 -m pip install jpype1
To specifically install the package for Python 2, try entering this command:
pip2 install jpype1
or
python -m pip install jpype1
I found the problem.
By mistake, I had two versions of Anaconda 3.6 and 3.7 which was causing the problem.

Install google-auth in raspberry (Python)

I'm installing google-auth on raspberry pi 3 using pip:
pip install google-auth
pip install --upgrade google-auth
But when I used import google.auth, I get the following error No module named 'google.auth'
(env) pi#raspberrypi:~/Desktop $ python
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linu
Type "help", "copyright", "credits" or "license" for more information
>>> import google.auth
Traceback (most recent call last)
File "<stdin>", line 1, in <module
ImportError: No module named 'google.auth
If you have installed pip by sudo,then try installing it using:
sudo pip install google-auth

No module named 'pafy'

sudo pip3.6 install pafy
Collecting pafy
Downloading pafy-0.5.3.1.tar.gz
Installing collected packages: pafy
Running setup.py install for pafy ... done
Successfully installed pafy-0.5.3.1
It is Successfully installed pafy-0.5.3.1.
~$ python
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pafy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pafy'
Why no module named 'pafy'?
You ran pip as sudo but you're running python as normal user. Programs run as normal user don't have rights to access super-user's content (sudo).
To verify this, you can run python as super-user and try to import pafy.
$ sudo python
>>> import pafy
You should never use sudo pip install; you could overwrite important stuff.
Use pip install --user <packagename> or install and use virtualenv. I prefer virtualenv.
pip install pafy
This would work

No module named osgeo.ogr

I need to import osgeo.ogr module to virtualenv python program.
Global python has this module:
user#ubuntu:~/$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import osgeo
>>>
But into virtualenv (without --no-site-packages):
user#ubuntu:~$ temp/bin/python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import osgeo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named osgeo
What is the reason for this behavior?
Able to solve with this one
pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==`gdal-config --version`
Problem is solved in this way:
(VIRTUAL_ENV)user#ubuntu:~$ pip install --no-install GDAL==1.11.2
1.11.2 because my version GDAL is 1.11.2:
(VIRTUAL_ENV)user#ubuntu:~$ gdal-config --version
1.11.2
next:
(VIRTUAL_ENV)user#ubuntu:~$ cd ~/.virtualenvs/VIRTUAL_ENV/build/GDAL/
(VIRTUAL_ENV)user#ubuntu:~/.virtualenvs/VIRTUAL_ENV/build/GDAL$ python setup.py build_ext --include-dirs=/usr/include/gdal/
(VIRTUAL_ENV)user#ubuntu:~/.virtualenvs/bamap/build/GDAL$ pip install --no-download GDAL
--include-dirs=/usr/include/gdal/ for me is correct. GDAL was installed with Quantum GIS in my system. If you will build GDAL manually paths may be others. Full version:
python setup.py build_ext --gdal-config=/YOUR_PATH/bin/gdal-config --library-dirs=/YOUR_PATH/gdal/VERSION_GDAL/lib --libraries=gdal --include-dirs=/YOUR_PATH/gdal/VERSION_GDAL/include
Details may find here.
sudo add-apt-repository ppa:ubuntugis/ppa && sudo apt-get update
if you have facing the issue " Cannot import name '_gi' ":
visit this link to fix
sudo apt-get update
sudo apt-get install gdal-bin
sudo apt-get install libgdal-dev
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal
pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-
option="-I/usr/include/gdal"

Categories

Resources