将不同版本的 python 与 virtualenvwrapper 一起使用
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以明确选择 python 版本
或
You can select the python version explicitly
or
我知道这在你的评论中已经基本解决了,但它仅限于Mac,
而且我认为正确的方法应该是将 VIRTUALENVWRAPPER_PYTHON 设置为你在命令行上使用的真实Python。
确保你可以执行
which python
。实际上,你甚至可以这样做:
在linux上,我在我的.bashrc中执行此操作,所以总而言之,假设你安装了virtualenv并创建了你的第一个“虚拟环境”
virtualenv
(多么原始)(并且由方式,你写道:
这实际上是相反的 - 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:
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)(and by the way, you wrote:
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!
这些都不起作用。我在设置 osx 机器时首先安装了 Python3,并且 pip 和 all 默认安装的。
首先,检查您安装了哪个 python:
如果这返回“Python 2.7.12”,那么您就可以运行:
这还将激活
api_server
工作,这会更改您的 python 可执行文件:< code>哪个python实际上是做什么的?它输出在您的 PATH 中找到的 python 可执行文件的目录:
通过使用
which python
,您基本上将/usr/local/bin/python
传递给mkvirtualenv 目录中的 -p
选项。当
which python
返回多个 python 可执行文件时会发生什么?只需找到您想要的并将其传入即可: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:
If this returns "Python 2.7.12", then you are set to run:
This will also activate the
api_server
workon, which changes your python executable:What does
which python
actually do? It outputs the directory of the python executables found in your PATH: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:And virtualenvwrapper will end up using that python executable instead.
确认使用了两个名称相似的环境变量:
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 executesvirtualenvwrapper
, 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 byvirtualenv
when you create a new virtual environment. Equivalent to-p / --python
option on thevirtualenv
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.
要将多个版本的 python 与 virtualwrapper 一起使用,您需要为该新版本及其相应的 pip 管理器重新安装 virtualwrapper。
假设您刚刚安装了 Python-3.12.0,那么您将需要在终端中安装,
然后您可以继续使用不同的 python 版本创建环境。
请注意,只要我输入 python ,它就会加载正确的 python 版本。您也可以在环境中正常
pip install
,因为它知道它必须在内部使用 pip3.12To 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
then you can proceed to create your environment with the different python version
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您(OP)似乎使用python2.7安装了virtualenv和virtualenvwrapper,而不是使用python2.6。如果在 shell 加载 virtualenvwrapper.sh 脚本时调用 python2.6,它会不高兴。非常简单。
VIRTUALENVWRAPPER_PYTHON
就是为这些情况而设计的。有了它,您可以确保始终使用正确版本的python,并且不必总是添加-p /path/to/python2.7
所以,我不同意在这种情况下,斯特凡诺的回答是,在OP的情况下,你应该在.bashrc中清楚地解释要使用哪个python:
就像那样,它应该一直没问题! 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:
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 withport python select
(check that withls -l /opt/local/bin/python
to be sure).