将不同版本的 python 与 virtualenvwrapper 一起使用

发布于 2024-11-16 04:06:28 字数 479 浏览 2 评论 0原文

我使用 Macports 在我的 Mac 上安装了各种版本的 python。当我通过 $ port select python python27 选择 python 2.7 时,virtualenvwrapper 可以正常工作。

但是,如果我选择另一个版本的 python,即 2.6,virtualenvwrapper 会生成错误消息: ImportError: No module named virtualenvwrapper.hook_loader

我检查了我的 .profile,并将 VIRTUALENVWRAPPER_PYTHON 设置为 /opt/local /bin/python,所以在我看来,无论我选择哪个 python,virtualenvwrapper 都应该工作。

知道当我切换 python 版本时,什么会导致 virtualenvwrapper 生成 .hook_loader 错误吗?

I've got various versions of python installed on my Mac using Macports. When I've selected python 2.7 via $ port select python python27, virtualenvwrapper works perfectly.

But if I select another version of python, i.e. 2.6, virtualenvwrapper generates an error message: ImportError: No module named virtualenvwrapper.hook_loader

I checked my .profile and it's setting VIRTUALENVWRAPPER_PYTHON to /opt/local/bin/python, so it seems to me virtualenvwrapper should work regardless of which python I've selected.

Any idea what would cause virtualenvwrapper to generate a .hook_loader error when I switch python versions?

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

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

发布评论

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

评论(6

懒猫 2024-11-23 04:06:28

您可以明确选择 python 版本

mkvirtualenv -p python3 venvname

mkvirtualenv -p python2.7 venvname

You can select the python version explicitly

mkvirtualenv -p python3 venvname

or

mkvirtualenv -p python2.7 venvname
内心旳酸楚 2024-11-23 04:06:28

我知道这在你的评论中已经基本解决了,但它仅限于Mac,

而且我认为正确的方法应该是将 VIRTUALENVWRAPPER_PYTHON 设置为你在命令行上使用的真实Python。

确保你可以执行which python

实际上,你甚至可以这样做:

export VIRTUALENVWRAPPER_PYTHON=`which python`

在linux上,我在我的.bashrc中执行此操作,所以总而言之,假设你安装了virtualenv并创建了你的第一个“虚拟环境”virtualenv(多么原始)

. virtualenv/bin/activate
export WORKON_HOME=$HOME/.virtualenvs # or whatever else you want
export VIRTUALENVWRAPPER_PYTHON=`which python`
export PROJECT_HOME=SOMETHING
source $HOME/virtualenv/bin/virtualenvwrapper.sh # or wherever else you got that installed

(并且由方式,你写道:

我检查了我的.profile,它将 VIRTUALENVWRAPPER_PYTHON 设置为 /opt/local/bin/python,所以在我看来,无论我选择哪个 python,virtualenvwrapper 都应该工作

这实际上是相反的 - virtualenv 依赖于使用正确的 python (并且随之而来的软件包)因此相应地设置 python 路径非常重要。

一旦你是 virtualenved,即使运行带有“#!/bin/python”的 py 文件也可能会带来麻烦!

I know this is pretty much solved in your comments, but it's mac only,

and even more I think the correct way should be to set VIRTUALENVWRAPPER_PYTHON to the real python you are using on the command line.

To be sure you can do which python.

Actually, you can even do:

export VIRTUALENVWRAPPER_PYTHON=`which python`

On linux I do this in my .bashrc, so all in all, assuming you installed virtualenv and created your first "virtual environment" virtualenv (how original)

. virtualenv/bin/activate
export WORKON_HOME=$HOME/.virtualenvs # or whatever else you want
export VIRTUALENVWRAPPER_PYTHON=`which python`
export PROJECT_HOME=SOMETHING
source $HOME/virtualenv/bin/virtualenvwrapper.sh # or wherever else you got that installed

(and by the way, you wrote:

I checked my .profile and it's setting VIRTUALENVWRAPPER_PYTHON to /opt/local/bin/python, so it seems to me virtualenvwrapper should work regardless of which python I've selected

which is actually the opposite - virtualenv relies on using the correct python (and the packages that go with it) so it's very important to set the python path accordingly.

Even running a py file with a "#!/bin/python" might bring trouble once you are virtualenved!

追星践月 2024-11-23 04:06:28

这些都不起作用。我在设置 osx 机器时首先安装了 Python3,并且 pip 和 all 默认安装的。

首先,检查您安装了哪个 python:

$ `which python` -V

如果这返回“Python 2.7.12”,那么您就可以运行:

$ mkvirtualenv -p `which python` api_server
Running virtualenv with interpreter /usr/local/bin/python
New python executable in /Users/eric/.virtualenvs/api_server/bin/python2.7
Also creating executable in /Users/eric/.virtualenvs/api_server/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/get_env_details

这还将激活 api_server 工作,这会更改您的 python 可执行文件:

$ which python
/Users/eric/.virtualenvs/api_server/bin/python
$ python -V
Python 2.7.12

< code>哪个python实际上是做什么的?它输出在您的 PATH 中找到的 python 可执行文件的目录:

$ which python
/usr/local/bin/python

通过使用 which python,您基本上将 /usr/local/bin/python 传递给 mkvirtualenv 目录中的 -p 选项。

which python 返回多个 python 可执行文件时会发生什么?只需找到您想要的并将其传入即可:

$ mkvirtualenv -p /usr/local/bin/python3 api_server

virtualenvwrapper 最终将使用该 python 可执行文件。

None of these worked. I installed Python3 first when setting up my osx machine, and pip and all default to that.

First, check which python you have installed:

$ `which python` -V

If this returns "Python 2.7.12", then you are set to run:

$ mkvirtualenv -p `which python` api_server
Running virtualenv with interpreter /usr/local/bin/python
New python executable in /Users/eric/.virtualenvs/api_server/bin/python2.7
Also creating executable in /Users/eric/.virtualenvs/api_server/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/get_env_details

This will also activate the api_server workon, which changes your python executable:

$ which python
/Users/eric/.virtualenvs/api_server/bin/python
$ python -V
Python 2.7.12

What does which python actually do? It outputs the directory of the python executables found in your PATH:

$ which python
/usr/local/bin/python

By using which python, you are basically passing in /usr/local/bin/python to the -p option in the mkvirtualenv directory.

What happens when you have more than one python executable returned in which python? Just find the one you want and pass it in:

$ mkvirtualenv -p /usr/local/bin/python3 api_server

And virtualenvwrapper will end up using that python executable instead.

瞄了个咪的 2024-11-23 04:06:28

确认使用了两个名称相似的环境变量:

VIRTUALENVWRAPPER_PYTHON——virtualenvwrapper 实用程序本身使用哪个 Python 版本。换句话说,哪个版本的 Python 执行 virtualenvwrapper,就好像该 Python 版本已在 virtualenvwrapper 脚本文件的 #! 行中显式命名。

VIRTUALENV_PYTHON - 当您创建新的虚拟环境时,virtualenv 将安装哪个 Python 版本。相当于 virtualenv 命令行上的 -p / --python 选项。

也许显然:)在虚拟环境中运行的Python版本是您为该环境安装的版本——在创建env之后与上述环境变量没有关系。

请参阅 https://stackoverflow.com/a/24724360/763269 了解如何在 virtualenv 中升级 Python。

Confirmed the use of two similarly-named environment variables:

VIRTUALENVWRAPPER_PYTHON -- which Python version is used by the virtualenvwrapper utility itself. In other words, which version of Python executes virtualenvwrapper, as if that Python version had been explicitly named in the #! line of the virtualenvwrapper script file.

VIRTUALENV_PYTHON -- which Python version will be installed by virtualenv when you create a new virtual environment. Equivalent to -p / --python option on the virtualenv command line.

And perhaps obviously :) the version of Python run in a virtual environment is the version you install for that environment -- has no relation to the above environment variables after the env is created.

See https://stackoverflow.com/a/24724360/763269 for how to upgrade Python within a virtualenv.

開玄 2024-11-23 04:06:28

要将多个版本的 python 与 virtualwrapper 一起使用,您需要为该新版本及其相应的 pip 管理器重新安装 virtualwrapper

假设您刚刚安装了 Python-3.12.0,那么您将需要在终端中安装,

sudo pip3.12 install virtualenv virtualenvwrapper

然后您可以继续使用不同的 python 版本创建环境。

mkvirtualenv sexyspace -p python3.12
workon sexyspace
python

请注意,只要我输入 python ,它就会加载正确的 python 版本。您也可以在环境中正常pip install,因为它知道它必须在内部使用 pip3.12

To use multiple versions of python with virtualwrapper you need to reinstall the virtualwrapper for that new version with its corresponding pip manager.

Say you just installed Python-3.12.0 then in your terminal you will need to

sudo pip3.12 install virtualenv virtualenvwrapper

then you can proceed to create your environment with the different python version

mkvirtualenv sexyspace -p python3.12
workon sexyspace
python

Note that just my typing python it will load the correct python version. Also you can pip install normally within the environment since it knows that it has to use pip3.12 internally

一曲琵琶半遮面シ 2024-11-23 04:06:28

您(OP)似乎使用python2.7安装了virtualenv和virtualenvwrapper,而不是使用python2.6。如果在 shell 加载 virtualenvwrapper.sh 脚本时调用 python2.6,它会不高兴。非常简单。

VIRTUALENVWRAPPER_PYTHON 就是为这些情况而设计的。有了它,您可以确保始终使用正确版本的python,并且不必总是添加 -p /path/to/python2.7

所以,我不同意在这种情况下,斯特凡诺的回答是,在OP的情况下,你应该在.bashrc中清楚地解释要使用哪个python:

...
export VIRTUALENVWRAPPER_PYTHON=/path/to/your/python2.7
source /path/to/bin/virtualenvwrapper.sh

就像那样,它应该一直没问题! Virtualenvwrapper 的目的是为了简化事情。

另外,请注意,/opt/local/bin/python 必须是您使用 port python select 选择的 Python 版本的符号链接(使用 ls 检查-l /opt/local/bin/python 是肯定的)。

You (the OP) seem to have installed virtualenv and virtualenvwrapper with python2.7, and not with python2.6. If python2.6 is called at the moment your shell loads the virtualenvwrapper.sh script, it is unhappy. Pretty straightforward.

VIRTUALENVWRAPPER_PYTHON is made for those situations. With it, you can make sure you always use the right version of python, and don't have to always add that -p /path/to/python2.7

So, I don't agree with Stefano's answer in that case, in the OP's situation, you should have explained clearly in your .bashrc which python to use:

...
export VIRTUALENVWRAPPER_PYTHON=/path/to/your/python2.7
source /path/to/bin/virtualenvwrapper.sh

Like that it should be ok all the time! Virtualenvwrapper is done to simplify things.

Also, please note that /opt/local/bin/python must be a symlink to the version of python you select with port python select (check that with ls -l /opt/local/bin/python to be sure).

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