Python 不会“看到”一个包裹

发布于 2024-12-09 01:24:13 字数 4108 浏览 1 评论 0原文

我按照本教程在我的 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 install Flask。 pip 将 Flask 安装到 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< /code> - 相同的结果。

Flask安装在env/lib/python2.7/site-packages下,所以主要问题是:virtualenv不必添加env/lib/ python2.7/site-packages 到 python 的 sys 路径?如果没有,我该怎么做更好:

  • 在运行时在 python 路径上添加适当的 site-packages 目录(例如,在我的烧瓶中添加 sys.path.insert(0,)应用程序,
  • 修改系统的 PYTHONPATH
  • 触发 virtualenv 的 after_install 方法将 添加到 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 which 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 安装教程之后,我在 which python 命令上得到了 /opt/python2.7/bin/python 。无论如何,问题就在那里,我们将不胜感激。请考虑一下我是一个Python新手,-可能这是我在这里问的一个愚蠢的问题。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-12-16 01:24:13

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

请注意,您的系统中现在有三个 python:系统的 2.6 在 /usr/bin 中,2.7 在 /opt/python2.7/bin/ 中,virtualenv 的 2.7 在 env/bin 中。只有最后一个安装了烧瓶。请特别确保您正在拨打该电话。

另外,不要弄乱 .bash_profile。特别是root的.bash_profile——你不需要flask的root权限,是吗?坚持使用 virtualenv,它更干净,而且搞乱系统的可能性也低得多。
(话虽如此,$PATH 会被扫描以查找第一个匹配项,因此您需要 export PATH=/opt/ 而不是 PATH=$PATH:/opt/python2.7/bin python2.7/bin:$PATH。但那是下次您需要更改 $PATH 时使用的,不是现在。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文