Python没有“看到”一个包

时间:2011-10-09 10:53:03

标签: python virtualenv pip flask

我按照本教程在CentOS 5.5上安装python2.7: http://villaroad.com/2010/10/rolling-python-2-6-2-on-centos-5-3/

安装python 2.7之后我得到了:

/opt/python2.7$ ls -la
total 24
drwxr-xr-x 6 root root 4096 2011-10-01 22:32 .
drwxr-xr-x 4 root root 4096 2011-10-01 22:32 ..
drwxr-xr-x 2 root root 4096 2011-10-09 19:51 bin
drwxr-xr-x 3 root root 4096 2011-10-01 22:32 include
drwxr-xr-x 4 root root 4096 2011-10-01 23:34 lib
drwxr-xr-x 3 root root 4096 2011-10-01 22:32 share

/opt/python2.7$ ls -la bin
total 88
drwxr-xr-x 2 root root  4096 2011-10-09 19:51 .
drwxr-xr-x 6 root root  4096 2011-10-01 22:32 ..
-rwxr-xr-x 1 root root   105 2011-10-01 22:32 2to3
-rwxr-xr-x 1 root root   296 2011-10-09 19:49 easy_install
-rwxr-xr-x 1 root root   304 2011-10-09 19:49 easy_install-2.7
-rwxr-xr-x 1 root root   103 2011-10-01 22:32 idle
-rwxr-xr-x 1 root root   254 2011-10-03 01:37 pip
-rwxr-xr-x 1 root root   262 2011-10-03 01:37 pip-2.7
-rwxr-xr-x 1 root root    88 2011-10-01 22:32 pydoc
-rwxr-xr-x 2 root root 11783 2011-10-01 23:34 python
-rwxr-xr-x 2 root root 11783 2011-10-01 23:34 python2.7
-rwxr-xr-x 1 root root  1628 2011-10-01 23:34 python2.7-config
lrwxrwxrwx 1 root root    16 2011-10-01 23:34 python-config -> python2.7-config
-rwxr-xr-x 1 root root 18551 2011-10-01 22:32 smtpd.py
-rwxr-xr-x 1 root root   289 2011-10-09 19:51 virtualenv

我使用/opt/python2.7/bin/virtualenv二进制文件在/var/www/myproj/env

中创建环境
cd /var/www/myproj 
virtualenv --no-site-packages -p /opt/python2.7/bin/python \ 
     --clear --prompt="(myproj.com) " env

激活它,并做pip安装烧瓶。 pip安装烧瓶到env/lib/python2.7/site-packages,它不在sys.path

$ python
Python 2.7.2 (default, Oct  1 2011, 23:29:08)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named flask
>>> import sys
>>> sys.path
['', '/opt/python2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', 
'/opt/python2.7/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg', 
'/opt/python2.7/lib/python27.zip', '/opt/python2.7/lib/python2.7', 
'/opt/python2.7/lib/python2.7/plat-linux2', '/opt/python2.7/lib/python2.7/lib-tk', 
'/opt/python2.7/lib/python2.7/lib-old', '/opt/python2.7/lib/python2.7/lib-dynload', 
'/opt/python2.7/lib/python2.7/site-packages']

我尝试了相同但使用easy_install而不是pip - 结果相同。

Flask安装在env/lib/python2.7/site-packages下,因此主要问题是:不要virtualenv必须将env/lib/python2.7/site-packages添加到python的sys路径中吗?如果没有,我该做得更好:

  • 在运行时在python路径上添加适当的site-packages目录(例如我的烧瓶应用程序中的sys.path.insert(0, <path to site-packages>)
  • 修改系统的PYTHONPATH
  • 触发virtualenv的after_install方法,将<path to site-packages>添加到python的路径

更多信息: 我已添加到用户和root的〜/ .bash_profile(Ubuntu上的〜/ .profile)

alias python='/opt/python2.7/bin/python'
alias python2.7='/opt/python2.7/bin/python'
PATH=$PATH:/opt/python2.7/bin

source ~/.profile 哪个python产生/usr/bin/python,即2.6.4(嗯......)

Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

奇怪,但如果我运行python我的版本正确:

$ python
Python 2.7.2 (default, Oct  1 2011, 23:29:08)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

更奇怪的是,在关注python2.7安装教程之后,我在/opt/python2.7/bin/python命令上获得了which python。无论如何,问题在那里,任何帮助表示赞赏。请考虑一下,我是一个蟒蛇新手, - 这可能是我在这里问的一个愚蠢的问题。

1 个答案:

答案 0 :(得分:4)

每当您使用Bash运行命令时,它都会缓存命令的位置。这可能就是你在这里碰到的。使用hash -r清除缓存。

请注意,您的系统中现在有三个蟒蛇:系统在/ usr / bin中的2.6,/ opt / python2.7 / bin /中的2.7,以及env / bin中的virtualenv's 2.7。只有最后一个安装了烧瓶。更加确定你是在打算那个。

另外,不要乱用.bash_profile。特别是root的.bash_profile - 你不需要烧瓶的root权限,对吗?坚持使用virtualenv,它更清洁,你的系统陷入困境的可能性要小得多。 (话虽如此,$ PATH会针对第一场比赛进行扫描,因此您需要PATH=$PATH:/opt/python2.7/bin代替export PATH=/opt/python2.7/bin:$PATH。但这是下次您需要更改$ PATH时,而不是现在。)