我正在尝试在Kubuntu 17.04上使用python 3虚拟环境(在Kubuntu 16.10上出现相同错误)。
python软件包是使用apt安装的,而不是通过pip等其他来源安装的虚拟环境。在Windows环境中运行相同的代码。在我看来,这似乎是操作系统问题,而不是python问题。它找不到我在虚拟环境中使用pip安装的任何库。我做了以下步骤:
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
通往的道路
/home/user/development/python/testDB/lib/python3.6/site-packages
未设置,因此找不到该库。
该脚本非常简单:
#!/usr/bin/python3
import sys
print(sys.path)
import pymysql
我看不到我在哪里做错误。有人可以帮忙吗?
添加的信息2017-04-22
我做了一些其他的测试和重新配置。...不幸的是,该错误并未消失。列出了pip3已安装的软件包:
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
与apt一起安装的适用于虚拟环境的Pyhton 3软件包是:
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]
我删除了所有软件包并重新安装了它。现在,请使用Python 3.5,但并没有帮助。
似乎您正在#!/usr/bin/python3
脚本开头使用。这告诉bash使用来执行脚本/usr/bin/python3
,该脚本可能与相同python
。当使用虚拟环境时,这几乎肯定是不正确的。您可以使用以下命令在venv中检查运行哪个python $ which python
作为解决方案,请尝试删除第一行并按那样执行脚本, $ python testDB.py
或者,如果需要将其作为脚本运行,请用替换第一行#!python
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句