我如何解决这个问题,用自定义构建的 Python 创建一个 virtualenv 环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2278028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:14:58  来源:igfitidea点击:

How do I work around this problem creating a virtualenv environment with a custom-build Python?

pythonvirtualenv

提问by Daryl Spitzer

I need to run some code on a Linux machine with Python 2.3.4 pre-installed. I'm not on the sudoers list for that machine, so I built Python 2.6.4 into (a subdirectory in) my home directory. Then I attempted to use virtualenv (for the first time), but got:

我需要在预装 Python 2.3.4 的 Linux 机器上运行一些代码。我不在该机器的 sudoers 列表中,所以我将 Python 2.6.4 构建到我的主目录(子目录)中。然后我尝试使用 virtualenv(第一次),但得到:

$ Python-2.6.4/python virtualenv/virtualenv.py ENV
New python executable in ENV/bin/python
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Installing setuptools.........
 Complete output from command /apps/users/dspitzer/ENV/bin/python -c "#!python
\"\"\"Bootstrap setuptoo...

" /apps/users/dspitzer/virtualen...6.egg:
 Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
'import site' failed; use -v for traceback
Traceback (most recent call last):
 File "<string>", line 67, in <module>
ImportError: No module named md5
----------------------------------------
...Installing setuptools...done.
Traceback (most recent call last):
 File "virtualenv/virtualenv.py", line 1488, in <module>
  main()
 File "virtualenv/virtualenv.py", line 529, in main
  use_distribute=options.use_distribute)
 File "virtualenv/virtualenv.py", line 619, in create_environment
  install_setuptools(py_executable, unzip=unzip_setuptools)
 File "virtualenv/virtualenv.py", line 361, in install_setuptools
  _install_req(py_executable, unzip)
 File "virtualenv/virtualenv.py", line 337, in _install_req
  cwd=cwd)
 File "virtualenv/virtualenv.py", line 590, in call_subprocess
  % (cmd_desc, proc.returncode))
OSError: Command /apps/users/dspitzer/ENV/bin/python -c "#!python
\"\"\"Bootstrap setuptoo...

" /apps/users/dspitzer/virtualen...6.egg failed with error code 1

Should I be setting PYTHONHOME to some value? (I intentionally named my ENV "ENV" for lack of a better name.)

我应该将 PYTHONHOME 设置为某个值吗?(我故意将我的 ENV 命名为“ENV”,因为没有更好的名字。)

Not knowing if I can ignore those errors, I tried installing nose (0.11.1) into my ENV:

不知道我是否可以忽略这些错误,我尝试将鼻子(0.11.1)安装到我的 ENV 中:

$ cd nose-0.11.1/
$ ls
AUTHORS    doc/               lgpl.txt     nose.egg-info/  selftest.py*
bin/       examples/          MANIFEST.in  nosetests.1     setup.cfg
build/     functional_tests/  NEWS         PKG-INFO        setup.py
CHANGELOG  install-rpm.sh*    nose/        README.txt      unit_tests/
$ ~/ENV/bin/python setup.py install
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Traceback (most recent call last):
 File "setup.py", line 1, in <module>
  from nose import __version__ as VERSION
 File "/apps/users/dspitzer/nose-0.11.1/nose/__init__.py", line 1, in <module>
  from nose.core import collector, main, run, run_exit, runmodule
 File "/apps/users/dspitzer/nose-0.11.1/nose/core.py", line 3, in <module>
  from __future__ import generators
ImportError: No module named __future__

Any advice?

有什么建议吗?

回答by Crast

Have you actually run "make install" on your custom python build? Usually you'll want to do something like

你真的在你的自定义 python 版本上运行过“make install”吗?通常你会想要做类似的事情

./configure --prefix=/path/to/installdir  (other options)
make
make install

NotePrefix can be any directory you have full write-permissions to, for example I very often use $HOME/apps on shared-hosting environments.

注意前缀可以是您拥有完全写入权限的任何目录,例如我经常在共享托管环境中使用 $HOME/apps。

Then run /path/to/installdir/bin/python, not the one from your build directory. This should create the correct variables, and after that you can install virtualenv. Might be best to install virtualenv using its setup.py:

然后运行 ​​/path/to/installdir/bin/python,而不是构建目录中的那个。这应该会创建正确的变量,然后您可以安装 virtualenv。最好使用它的 setup.py 安装 virtualenv:

cd virtualenv_source_dir
/path/to/installdir/bin/python setup.py install

This may require installing setuptools first, using the same method.

这可能需要首先使用相同的方法安装 setuptools。

Then finally:

然后最后:

# Just to be safe
export PATH="/path/to/installdir/bin:$PATH" 

virtualenv ~/ENV
~/ENV/bin/pip install somepackage # (and such)

回答by gotgenes

In addition to Crast's suggestion of making sure you actually installed your custom compiled Python, you should also check that the custom Python can actually find its libraries. This is the hint you're getting with the message about PYTHONHOME. The import errors suggest you need to set in your .bashrcor appropriate shell configuration export PYTHONHOME=/path/to/python_installation.

除了 Crast 建议确保您确实安装了自定义编译的 Python 之外,您还应该检查自定义 Python 是否确实可以找到它的库。这是您从有关PYTHONHOME.导入错误表明您需要在您的.bashrc或适当的 shell 配置中进行设置export PYTHONHOME=/path/to/python_installation

Additionally, when you are trying to tell virtualenv to use a non-default version of python, you need to use the -p,--pythonflag, e.g.,

此外,当您尝试告诉 virtualenv 使用非默认版本的 python 时,您需要使用该-p,--python标志,例如,

virtualenv --python=/path/to/python_installation/bin/python myenv

See also the related question, "Use different Python version with virtualenv".

另请参阅相关问题“在 virtualenv 中使用不同的 Python 版本”

回答by Mathieu Longtin

I had the same error when trying to install on an existing directory that already had easy_install in lib/python2.6. I had to put a link from lib64/python2.6 to lib/python2.6.

尝试安装在 lib/python2.6 中已经有 easy_install 的现有目录上时,我遇到了同样的错误。我不得不把一个从 lib64/python2.6 到 lib/python2.6 的链接。

I am not saying my fix is the right fix, rather, I'm pointing to another reason why you might get this error.

我并不是说我的修复是正确的修复,而是指出您可能会收到此错误的另一个原因。

回答by Bradley Kreider

Now you can easily install Python as an unpriviledged user using Anaconda: http://continuum.io/downloads

现在,您可以使用 Anaconda 以非特权用户的身份轻松安装 Python:http: //continuum.io/downloads

It's similar to this question. Once of the answers details making a new environment, so you don't need to use virtual-env and avoid the occasional gotchas: Installing Anaconda into a Virtual Environment.

这类似于这个问题。一个答案详细说明了创建新环境,因此您不需要使用 virtual-env 并避免偶尔遇到的问题:将Anaconda 安装到虚拟环境中

conda create -n myenv1 ipython scipy

conda create -n myenv1 ipython scipy

回答by Mike Davlantes

I don't have enough rep to add this as a comment on @Crast's answer and this question is 4 years old, but this might be useful to someone. In Windows, you have to path out to python.exe, but it seems that in Linux/OS X you just path to the folder. Example:

我没有足够的代表将此添加为对@Crast 答案的评论,这个问题已有 4 年历史,但这可能对某人有用。在 Windows 中,您必须指向python.exe,但在 Linux/OS X 中,您似乎只需指向文件夹的路径。例子:

Windows:

视窗:

virtualenv -p <PATH TO YOUR DESIRED PYTHON.EXE> venv

Creates a virtual environment in subfolder "venv" in current directory.

在当前目录的子文件夹“venv”中创建一个虚拟环境。