用户本地的Python安装混乱(virtualenv、pip、distribute)

3 投票
2 回答
4535 浏览
提问于 2025-04-16 14:04

我尝试了下面的方法来安装Python,但没有成功。出现了“错误:安装目录不正确或PYTHONPATH设置有问题”的提示。

安装pip、virtualenv和分发工具的正确方法是什么?

首先,创建一个目录

$ mkdir -p ~/.python

然后,添加.bashrc文件

#Use local python
export PATH=$HOME/.python/bin:$PATH
export PYTHONPATH=$HOME/.python

接着,创建一个文件 ~/.pydistutils.cfg

[install]
prefix=~/.python

获取安装脚本

$ cd ~/src
$ curl -O http://python-distribute.org/distribute_setup.py

执行时出现错误

$ python ./distribute_setup.py
Extracting in /tmp/tmpsT2kdA
Now working in /tmp/tmpsT2kdA/distribute-0.6.15
Installing Distribute
Before install bootstrap.
Scanning installed packages
No setuptools distribution foundrunning install
Checking .pth file support in /home/sane/.python/lib/python2.6/site-packages//usr/bin/python -E -c pass
TEST FAILED: /home/sane/.python/lib/python2.6/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from.  The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/home/sane/.python/lib/python2.6/site-packages/
and your PYTHONPATH environment variable currently contains:
'/home/sane/.python'
Here are some of your options for correcting the problem:
* You can choose a different installation directory, i.e., one that is 
on PYTHONPATH or supports .pth files
* You can add the installation directory to the PYTHONPATH environment 
variable.  (It must then also be on PYTHONPATH whenever you run 
Python and want to use the package(s) you are installing.)
* You can set up the installation directory to support ".pth" files by 
using one of the approaches described here:
http://packages.python.org/distribute/easy_install.html#custom-installation-locations

Please make the appropriate changes for your system and try again.
Something went wrong during the installation.
See the error message above.

我的环境('sane'是我的Unix用户名。)

$ python -V
Python 2.6.4
$ which python
/usr/bin/python
$ uname -a
Linux localhost.localdomain 2.6.34.8-68.fc13.x86_64 #1 SMP Thu Feb 17 15:03:58 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

2 个回答

0

通常,我会在我的全局Python安装中安装分布式工具/easy_install,也就是setuptools,同时还会安装virtualenv。从这个时候开始,我就可以使用下面的命令创建专门的虚拟环境。

virtualenv --no-site-packages name-of-environment

我真的不知道为什么要在这里搞PYTHONPATH。

只需要把/path/to/name-of-environment/bin添加到$PATH中,就足够让虚拟的Python被识别了。你不需要做其他的事情。

0

我选择了pythonbrew。
通过这个工具,我可以在我的家目录里使用多个不同版本的Python。
这真是个不错的工具。

utahta/pythonbrew - GitHub https://github.com/utahta/pythonbrew

撰写回答