开发者

Python does not "see" a package

I followed this tutorial in order to install python2.7 on my CentOS 5.5: http://villaroad.com/2010/10/rolling-python-2-6-2-on-centos-5-3/

After installation of python 2.7 I've got:

/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

I use /opt/python2.7/bin/virtualenv binary to create an environment in /var/www/myproj/env

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

Activate it, and do pip install flask. pip installes flask to env/lib/python2.7/site开发者_JS百科-packages and it's not on 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']

I tried the same but using easy_install instead of pip - same result.

Flask is installed under env/lib/python2.7/site-packages, so the main question is: don't virtualenv have to add env/lib/python2.7/site-packages to the python's sys path? If not, what do I do better:

  • add an appropriate site-packages directory on python path at runtime (e.g. sys.path.insert(0, <path to site-packages>) in my flask application,
  • modify system's PYTHONPATH or
  • trigger virtualenv's after_install method to add <path to site-packages> to python's path

?

some more info: I've added to user's and root's ~/.bash_profile (~/.profile on Ubuntu)

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

did source ~/.profile which python produces /usr/bin/python which is 2.6.4 (hmmmm...)

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()

Odd enough, but if I run python I'm getting the right version:

$ 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.

What is even more odd is that after following to the python2.7 installation tutorial I was getting /opt/python2.7/bin/python on which python command. Anyway, the question is up there, any help is appreciated. Please, consider that I'm a python newbie, - probably it's a silly question I'm asking here.


Whenever you run a a command with Bash, it caches the command's location. That may be what you're running into here. Use hash -r to clean the cache.

Note that you have three pythons in your system now: the system's 2.6 in /usr/bin, the 2.7 in /opt/python2.7/bin/, and the virtualenv's 2.7 in env/bin. Only the last one has flask installed. Make extra sure you're caling that one.

Also, don't mess with the .bash_profile. Especially root's .bash_profile – you don't need root privileges for flask, do you? Stick to a virtualenv, it's much cleaner and there's a much lower chance you'll mess up your system. (That being said, $PATH is scanned for the first match, so instead of PATH=$PATH:/opt/python2.7/bin you'd want export PATH=/opt/python2.7/bin:$PATH. But that's for the next time you need to change $PATH, not for now.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜