Python Virtual Environment - sys.path

I am trying to use python 3 virtual environment on Kubuntu 17.04 (same error on Kubuntu 16.10).

The python packages are installed by using apt and not by some other source such as pip to do the virtual environment. The same code is running in a Windows environment. So it seems to me an OS issue rather than a python issue. It does not find any library I install using pip within the virtual environment. I did the following steps:

user@computer:~/development/python/testDB$ python3.6 -m venv /home/user/development/python/testDB/
user@computer:~/development/python/testDB$ source bin/activate
(testDB) user@computer:~/development/python/testDB$ pip install pymysql
Collecting pymysql
Using cached PyMySQL-0.7.11-py2.py3-none-any.whl
Installing collected packages: pymysql
Successfully installed pymysql-0.7.11
(testDB) user@computer:~/development/python/testDB$ ./testDB.py
['/home/user/development/python/testDB', '/usr/lib/python35.zip','/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/home/user/.local/lib/python3.5/site-packages', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
Traceback (most recent call last): File "./testDB.py", line 4, in <module> import pymysql
ImportError: No module named 'pymysql'
(testDB) user@computer:~/development/python/testDB$ python --version
Python 3.6.1

The path to

 /home/user/development/python/testDB/lib/python3.6/site-packages

is not set and therefore the library is not found.

The script is very simple:

#!/usr/bin/python3
import sys
print(sys.path)
import pymysql 

I do not see where I do the error. Can anybody please help?

Added Info 2017-04-22

I did some additional testing and reconfiguration.... unfortunately the error does not vanish. Listing of the pip3 installed packages are:

sudo pip3 freeze
apt-xapian-index==0.47
apturl==0.5.2
chardet==2.3.0
command-not-found==0.3
cryptography==1.7.1
defer==1.0.6
distro-info===0.14build1
idna==2.2
keyring==10.3.1
keyrings.alt==2.2
language-selector==0.1
pdb-clone==1.10
pexpect==4.2.1
Pillow==4.0.0
ptyprocess==0.5.1
pyasn1==0.1.9
pyclewn==2.3
pycrypto==2.6.1
pycups==1.9.73
pycurl==7.43.0
Pygments==2.2.0
pygobject==3.22.0
python-apt==1.4.0b2
python-debian==0.1.30
pyxdg==0.25
PyYAML==3.12
reportlab==3.3.0
requests==2.10.0
SecretStorage==2.3.1
six==1.10.0
systemd-python==233
ubuntu-drivers-common==0.0.0
ufw==0.35
unattended-upgrades==0.1
urllib3==1.15.1
usb-creator==0.3.3
xkit==0.0.0
virtualenv==15.1.0

Pyhton 3 packages installed with apt for the virtual environment are:

apt list --installed |grep python3-v
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
python3-venv/zesty,now 3.5.3-1 amd64 [installed]
python3-virtualenv/zesty,zesty,now 15.1.0+ds-1 all [installed]

I removed all packages and reinstalled it. Do now use Python 3.5, however it didn't help.

2

1 Answer

Seems you are using #!/usr/bin/python3 in head of your script. This tells bash to execute script with /usr/bin/python3, which might not be the same as python. When using virtual environment this is almost certainly not true. You can check which python is ran in venv with$ which python

As a solution, try deleting first line and execute script as$ python testDB.pyor, if you need to run it as script, replace first line with #!python.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like