标签归档:virtualenv

如何卸载使用pip install –user安装的软件包

问题:如何卸载使用pip install –user安装的软件包

--userpip 有一个选项,可以为每个用户安装Python软件包:

pip install --user [python-package-name]

我使用此选项在没有root访问权限的服务器上安装软件包。我现在需要的是在当前用户上卸载已安装的软件包。我试图执行以下命令:

pip uninstall --user [python-package-name]

但是我得到了:

no such option: --user

pip install --user除了手动查找和删除软件包外,如何卸载与之一起安装的软件包?

我发现这篇文章

pip无法从每个用户的站点软件包目录中卸载

描述了不支持从用户目录卸载软件包。根据文章,如果正确实施,则使用

pip uninstall [package-name]

安装的软件包也将在用户目录中搜索。但是对我来说仍然是一个问题。如果在系统范围和每个用户都安装了相同的软件包,该怎么办?如果有人需要针对特定​​用户目录怎么办?

There is a --user option for pip which can install a Python package per user:

pip install --user [python-package-name]

I used this option to install a package on a server for which I do not have root access. What I need now is to uninstall the installed package on the current user. I tried to execute this command:

pip uninstall --user [python-package-name]

But I got:

no such option: --user

How can I uninstall a package that I installed with pip install --user, other than manually finding and deleting the package?

I’ve found this article

pip cannot uninstall from per-user site-packages directory

which describes that uninstalling packages from user directory does not supported. According to the article if it was implemented correctly then with

pip uninstall [package-name]

the package that was installed will be also searched in user directories. But a problem still remains for me. What if the same package was installed both system-wide and per-user? What if someone needs to target a specific user directory?


回答 0

在Linux上使用Python 3.5和pip 7.1.2测试了这一情况之后,情况似乎是这样的:

  • pip install --user somepackage可以使用进行安装$HOME/.local和卸载pip uninstall somepackage

  • 无论是否somepackage同时在全系统范围内安装,都是如此。

  • 如果在两个地方都安装了该软件包,则只​​会卸载本地软件包。要使用卸载系统范围的软件包pip,请先在本地将其卸载,然后使用root特权再次运行相同的卸载命令。

  • 除了预定义的用户安装目录外,pip install --target somedir somepackage还将软件包安装到中somedir。无法使用从这样的位置卸载软件包pip。(但是在Github上实现了一个有点旧的未合并的pull请求,它实现了pip uninstall --target。)

  • 由于pip将要卸载的唯一位置是系统范围的和预定义的本地用户,因此您需要以pip uninstall相应用户身份运行才能从给定用户的本地安装目录中卸载。

Having tested this using Python 3.5 and pip 7.1.2 on Linux, the situation appears to be this:

  • pip install --user somepackage installs to $HOME/.local, and uninstalling it does work using pip uninstall somepackage.

  • This is true whether or not somepackage is also installed system-wide at the same time.

  • If the package is installed at both places, only the local one will be uninstalled. To uninstall the package system-wide using pip, first uninstall it locally, then run the same uninstall command again, with root privileges.

  • In addition to the predefined user install directory, pip install --target somedir somepackage will install the package into somedir. There is no way to uninstall a package from such a place using pip. (But there is a somewhat old unmerged pull request on Github that implements pip uninstall --target.)

  • Since the only places pip will ever uninstall from are system-wide and predefined user-local, you need to run pip uninstall as the respective user to uninstall from a given user’s local install directory.


回答 1

在MacOS上卸载软件包’oauth2client’的示例:

pip uninstall oauth2client

example to uninstall package ‘oauth2client’ on MacOS:

pip uninstall oauth2client

回答 2

但是,对于pip install --user some_pkg 在虚拟环境中使用的人员要小心。

$ path/to/python -m venv ~/my_py_venv
$ source ~/my_py_venv/bin/activate
(my_py_venv) $ pip install --user some_pkg
(my_py_venv) $ pip uninstall some_pkg
WARNING: Skipping some_pkg as it is not installed.
(my_py_venv) $ pip list
# Even `pip list` will not properly list the `some_pkg` in this case

在这种情况下,您必须停用当前的虚拟环境,然后使用相应的python/ pip可执行文件列出或卸载用户站点软件包:

(my_py_venv) $ deactivate
$ path/to/python -m pip list
$ path/to/python -m pip uninstall some_pkg

请注意,此问题是几年前报告的。似乎当前的结论是:在虚拟环境中--user无效pip,因为用户位置对于虚拟环境而言实际上没有任何意义。

Be careful though, for those who using pip install --user some_pkg inside a virtual environment.

$ path/to/python -m venv ~/my_py_venv
$ source ~/my_py_venv/bin/activate
(my_py_venv) $ pip install --user some_pkg
(my_py_venv) $ pip uninstall some_pkg
WARNING: Skipping some_pkg as it is not installed.
(my_py_venv) $ pip list
# Even `pip list` will not properly list the `some_pkg` in this case

In this case, you have to deactivate the current virtual environment, then use the corresponding python/pip executable to list or uninstall the user site packages:

(my_py_venv) $ deactivate
$ path/to/python -m pip list
$ path/to/python -m pip uninstall some_pkg

Note that this issue was reported few years ago. And it seems that the current conclusion is: --user is not valid inside a virtual env’s pip, since a user location doesn’t really make sense for a virtual environment.


回答 3

我认为可以卸载带有--userflag的软件包。这个为我工作;

pip freeze --user | xargs pip uninstall -y

对于python 3;

pip3 freeze --user | xargs pip3 uninstall -y

但是以某种方式,这些命令不会卸载setuptools和pip。在这些命令之后(如果您确实想要干净的python),可以使用以下命令删除它们:

pip uninstall setuptools && pip uninstall pip

I think it’s possible to uninstall packages installed with --user flag. This one worked for me;

pip freeze --user | xargs pip uninstall -y

For python 3;

pip3 freeze --user | xargs pip3 uninstall -y

But somehow these commands don’t uninstall setuptools and pip. After those commands (if you really want clean python) you may delete them with;

pip uninstall setuptools && pip uninstall pip


回答 4

答案尚不可行。您必须手动将其删除。

The answer is Not possible yet. You have to remove it manually.


回答 5

正如@ thomas-lotze提到的那样,由于没有相应的–user选项,当前pip工具无法执行此操作。但是我发现我可以检入〜/ .local / bin并查找特定的pip#。#,在我看来它对应于–user选项。

就我而言:

antho@noctil: ~/.l/bin$ pwd
/home/antho/.local/bin
antho@noctil: ~/.l/bin$ ls pip*
pip  pip2  pip2.7  pip3  pip3.5

然后只需卸载特定的pip版本即可。

As @thomas-lotze has mentioned, currently pip tooling does not do that as there is no corresponding –user option. But what I find is that I can check in ~/.local/bin and look for the specific pip#.# which looks to me like it corresponds to the –user option.

In my case:

antho@noctil: ~/.l/bin$ pwd
/home/antho/.local/bin
antho@noctil: ~/.l/bin$ ls pip*
pip  pip2  pip2.7  pip3  pip3.5

And then just uninstall with the specific pip version.


回答 6

我正在运行Anaconda 4.3.22版和python3.6.1环境,并且遇到了此问题。这是历史记录和修复方法:

pip uninstall opencv-python # -- the original step. failed.

ImportError: DLL load failed: The specified module could not be found.

我在python3.6环境中执行了此操作,并收到此错误。

python -m pip install opencv-python # same package as above.
conda install -c conda-forge opencv # separate install parallel to opencv
pip-install opencv-contrib-python # suggested by another user here. doesn't resolve it.

接下来,我尝试下载python3.6并将python3.dll放入该文件夹和各个文件夹中。没有改变。

最后,它解决了:

pip uninstall opencv-python

(仍然安装了另一个conda-forge版本)。这仅留下了conda版本,并且在3.6版本中有效。

>>>import cv2
>>>

加工!

I am running Anaconda version 4.3.22 and a python3.6.1 environment, and had this problem. Here’s the history and the fix:

pip uninstall opencv-python # -- the original step. failed.

ImportError: DLL load failed: The specified module could not be found.

I did this into my python3.6 environment and got this error.

python -m pip install opencv-python # same package as above.
conda install -c conda-forge opencv # separate install parallel to opencv
pip-install opencv-contrib-python # suggested by another user here. doesn't resolve it.

Next, I tried downloading python3.6 and putting the python3.dll in the folder and in various folders. nothing changed.

finally, this fixed it:

pip uninstall opencv-python

(the other conda-forge version is still installed) This left only the conda version, and that works in 3.6.

>>>import cv2
>>>

working!


使virtualenv继承全局站点程序包中的特定程序包

问题:使virtualenv继承全局站点程序包中的特定程序包

我正在寻找一种制作virtualenv的方法,该方法只包含基本python安装的一些库(我选择了这些库)。

更具体地说,我正在尝试在创建virtualenv的过程中将matplotlib导入virtualenv。由于缺少某些fortran编译器库,因此无法使用pip或easy_install有效安装。我到目前为止的方法是手动从

/usr/lib/python2.7/dist-packages/ to virtualenv_name/lib/python2.7/dist-packages/

但是,这会阻止手动导入的链接被蛋黄注册(这会在virtualenv中打印所有当前可用的库)。

因此,有没有一种方法可以对

virtualenv --system-site-packages

I’m looking for a way to make a virtualenv which will contain just some libraries (which i chose) of the base python installation.

To be more concrete, I’m trying to import my matplotlib to virtualenv during the creation of virtualenv. It can’t be installed efficiently with pip or easy_install since it misses some fortran compiler libs. The way i did it till now was to manually copy from

/usr/lib/python2.7/dist-packages/ to virtualenv_name/lib/python2.7/dist-packages/

however this prevents the manully imported links to be registerd by yolk (which prints all currently available libs in virtualenv).

So, is there a way to do a selective variant of the

virtualenv --system-site-packages

回答 0

用创建环境virtualenv --system-site-packages。然后,激活virtualenv,并在将其安装在virtualenv中而不是系统python中时,请使用pip install --ignore-installedpip install -I。这样,即使存在系统范围的版本,pip也会在本地安装您所请求的内容。您的python解释器将首先在virtualenv的package目录中查找,因此这些程序包应覆盖全局程序包。

Create the environment with virtualenv --system-site-packages . Then, activate the virtualenv and when you want things installed in the virtualenv rather than the system python, use pip install --ignore-installed or pip install -I . That way pip will install what you’ve requested locally even though a system-wide version exists. Your python interpreter will look first in the virtualenv’s package directory, so those packages should shadow the global ones.


回答 1

您可以使用--system-site-packages,然后“过度安装”您的virtualenv特定的东西。这样,您安装到virtualenv中的所有内容都将从此处获取,否则将从系统中获取。

You can use the --system-site-packages and then “overinstall” the specific stuff for your virtualenv. That way, everything you install into your virtualenv will be taken from there, otherwise it will be taken from your system.


回答 2

使用安装虚拟环境

virtualenv --system-site-packages

和使用 pip install -U to install matplotlib

Install virtual env with

virtualenv --system-site-packages

and use pip install -U to install matplotlib


回答 3

您可以使用virtualenv --clear。不会安装任何软件包,然后安装所需的软件包。

You can use virtualenv --clear. which won’t install any packages, then install the ones you want.


找不到Virtualenv命令

问题:找不到Virtualenv命令

virtualenv尽管进行了种种尝试,我还是无法上班。我virtualenv使用以下命令安装在MAC OS X上:

pip install virtualenv

并已将新增PATH至我的.bash_profile。每当我尝试运行该virtualenv命令时,它都会返回:

-bash: virtualenv: command not found

每次运行pip install virtualenv,它都会返回:

Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

我了解在Mac中,virtualenv应正确安装

/usr/local/bin

virtualenv确实安装/usr/local/bin,但每当我试图运行virtualenv命令,该命令没有找到。我也尝试virtualenv在目录中运行命令/usr/local/bin,它给我相同的结果:

-bash: virtualenv: command not found

这些是我添加到.bash_profile中的路径

export PATH=$PATH:/usr/local/bin
export PATH=$PATH:/usr/local/bin/python
export PATH=$PATH:/Library/Framework/Python.framework/Version/2.7/lib/site-packages

有任何解决方法吗?为什么会这样呢?

I couldn’t get virtualenv to work despite various attempts. I installed virtualenv on MAC OS X using:

pip install virtualenv

and have also added the PATH into my .bash_profile. Every time I try to run the virtualenv command, it returns:

-bash: virtualenv: command not found

Every time I run pip install virtualenv, it returns:

Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

I understand that in mac, the virtualenv should be correctly installed in

/usr/local/bin

The virtualenv is indeed installed in /usr/local/bin, but whenever I try to run the virtualenv command, the command is not found. I’ve also tried to run the virtualenv command in the directory /usr/local/bin, and it gives me the same result:

-bash: virtualenv: command not found

These are the PATHs I added to my .bash_profile

export PATH=$PATH:/usr/local/bin
export PATH=$PATH:/usr/local/bin/python
export PATH=$PATH:/Library/Framework/Python.framework/Version/2.7/lib/site-packages

Any workarounds for this? Why is this the case?


回答 0

如果您使用

pip install virtualenv

你需要跑步

sudo /usr/bin/easy_install virtualenv

把它放进去/usr/local/bin/

上面的目录默认情况下应该在您的目录中PATH;否则,请相应地编辑您的.zshrc(或。bashrc)。

If you installed it with

pip install virtualenv

You need to run

sudo /usr/bin/easy_install virtualenv

which puts it in /usr/local/bin/.

The above directory by default should be in your PATH; otherwise, edit your .zshrc (or .bashrc) accordingly.


回答 1

我遇到了同样的问题,这就是我解决的方法:

  1. 这个问题发生在我身上,因为我是通过pip以普通用户(不是root)的身份安装virtualenv的。pip将软件包安装到目录中~/.local/lib/pythonX.X/site-packages
  2. 当我以root用户或具有管理员权限(sudo)运行pip时,它在中安装了软件包/usr/lib/pythonX.X/dist-packages。对于您来说,此路径可能有所不同。
  3. 只在第二种情况下才能识别virtualenv命令
  4. 因此,要解决此问题,请执行pip uninstall virtualenv然后重新安装sudo pip install virtualenv(或以root身份安装)

I faced the same issue and this is how I solved it:

  1. The issue occurred to me because I installed virtualenv via pip as a regular user (not root). pip installed the packages into the directory ~/.local/lib/pythonX.X/site-packages
  2. When I ran pip as root or with admin privileges (sudo), it installed packages in /usr/lib/pythonX.X/dist-packages. This path might be different for you.
  3. virtualenv command gets recognized only in the second scenario
  4. So, to solve the issue, do pip uninstall virtualenv and then reinstall it with sudo pip install virtualenv (or install as root)

回答 2

最简单的答案。只是:

pip uninstall virtualenv

然后:

pip install virtualenv

或者sudo,在这种情况下,您可能使用来安装了virtualenv :

pip install --user virtualenv

The simplest answer. Just:

pip uninstall virtualenv

and then:

pip install virtualenv

Or you maybe installed virtualenv with sudo, in that case:

pip install --user virtualenv

回答 3

在Ubuntu 18.04 LTS上,我也遇到了相同的错误。以下命令有效:

sudo apt-get install python-virtualenv

On Ubuntu 18.04 LTS I also faced same error. Following command worked:

sudo apt-get install python-virtualenv

回答 4

我在Mac OS X El Capitan上也遇到了同样的问题。

当我这样安装时virtualenvsudo pip3 install virtualenv我没有virtualenv在命令行下。

我按照以下步骤解决了这个问题:

  1. 卸载以前的安装。
  2. virtualenv安装前通过调用切换到超级用户帐户sudo su
  3. virtualenv通过调用安装pip3 install virtualenv
  4. 最后,您应该可以virtualenv同时从usersuper user帐户访问。

I had same problem on Mac OS X El Capitan.

When I installed virtualenv like that sudo pip3 install virtualenv I didn’t have virtualenv under my command line.

I solved this problem by following those steps:

  1. Uninstall previous installations.
  2. Switch to super user account prior to virtualenv installation by calling sudo su
  3. Install virtualenv by calling pip3 install virtualenv
  4. Finally you should be able to access virtualenv from both user and super user account.

回答 5

找出问题

尝试使用--verbose标志进行安装

pip install virtualenv --verbose

输出看起来像这样

  ..
  Using cached virtualenv-15.1.0-py2.py3-none-any.whl
  Downloading from URL https://pypi.python.org/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl#md5=aa7e5b86cc8cdb99794c4b99e8d670f3 (from https://pypi.python.org/simple/virtualenv/)
Installing collected packages: virtualenv

  changing mode of /home/manos/.local/bin/virtualenv to 755
Successfully installed virtualenv-15.1.0
Cleaning up...

从输出中我们可以看到它已安装在,/home/manos/.local/bin/virtualenv因此我们确保PATH包含该文件。

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

以我为例,我们可以清楚地看到它/home/manos/.local/bin完全丢失了,这就是为什么Shell无法找到程序的原因。

解决方案

我们可以通过多种方式解决此问题:

  1. 我们可以通过点子选项直接安装到特定目录(不推荐)。
  2. /usr/local/bin或类似位置创建适当的符号链接。
  3. 附加/home/manos/.local/bin到PATH。
  4. 以sudo的形式安装以直接安装到 /usr/local/bin

最后两个选项可能是最明智的。最后一个解决方案是最简单的,因此我将仅介绍解决方案3。

将此添加到〜/ .profile:

PATH="$PATH:$HOME/.local/bin"

注销并再次登录,它应该可以工作。

Figure out the problem

Try installing with the --verbose flag

pip install virtualenv --verbose

Output will look something like this

  ..
  Using cached virtualenv-15.1.0-py2.py3-none-any.whl
  Downloading from URL https://pypi.python.org/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl#md5=aa7e5b86cc8cdb99794c4b99e8d670f3 (from https://pypi.python.org/simple/virtualenv/)
Installing collected packages: virtualenv

  changing mode of /home/manos/.local/bin/virtualenv to 755
Successfully installed virtualenv-15.1.0
Cleaning up...

From the output we can see that it’s installed at /home/manos/.local/bin/virtualenv so let’s ensure PATH includes that.

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

In my case we can clearly see that /home/manos/.local/bin is totally missing and that’s why the shell can’t find the program.

Solutions

We can solve this in many ways:

  1. We can install directly to a specific directory by fiddling with pip options (not recomended).
  2. Create appropriate symlinks at /usr/local/bin or similar.
  3. Append /home/manos/.local/bin to PATH.
  4. Install as sudo to install directly to /usr/local/bin

The two last options are probably the most sensible. The last solution is the simplest so therefore I will just show solution 3.

Add this to ~/.profile:

PATH="$PATH:$HOME/.local/bin"

Logout out and in again and it should work.


回答 6

python3 -m virtualenv virtualenv_name

python -m virtualenv virtualenv_name

python3 -m virtualenv virtualenv_name

python -m virtualenv virtualenv_name


回答 7

就我而言,我跑来pip show virtualenv获取有关virtualenv软件包的信息。我将看起来与此相似,还将显示软件包的位置:

user@machine:~$ pip show virtualenv
Name: virtualenv
Version: 16.2.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb@colorstudy.com
License: MIT
Location: /home/user/.local/lib/python3.6/site-packages
Requires: setuptools

从那个位置抓取到该位置的.local一部分,在这种情况下为/home/user/.local/。您可以在下找到virtualenv命令/home/user/.local/bin/virtualenv

然后,您可以运行命令/home/user/.local/bin/virtualenv newvirtualenv

In my case, I ran pip show virtualenv to get the information about virtualenv package. I will look similar to this and will also show location of the package:

user@machine:~$ pip show virtualenv
Name: virtualenv
Version: 16.2.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb@colorstudy.com
License: MIT
Location: /home/user/.local/lib/python3.6/site-packages
Requires: setuptools

From that grab the part of location up to the .local part, which in this case is /home/user/.local/. You can find virtualenv command under /home/user/.local/bin/virtualenv.

You can then run commands like /home/user/.local/bin/virtualenv newvirtualenv.


回答 8

我遇到过同样的问题。我使用以下步骤使其工作

sudo pip uninstall virtualenv

sudo -H pip install virtualenv

这就对了。它开始工作了。

sudo -H—-> sudo -H:HOME变量设置为目标用户主目录的用法。

I had the same issue. I used the following steps to make it work

sudo pip uninstall virtualenv

sudo -H pip install virtualenv

That is it. It started working.

Usage of sudo -H—-> sudo -H: set HOME variable to target user’s home dir.


回答 9

您说过,每次运行pip install都会得到回报Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages。您需要执行以下操作:

  1. 更改目录(转到其中virtualenv.py的目录) cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
  2. 如果您执行了一个操作,ls您将看到脚本在那里virtualenv.py
  3. 像这样运行脚本: python virtualenv.py --distribute /the/path/at/which/you/want/the/new/venv/at theNameOfTheNewVirtualEnv

希望这可以帮助。我的建议是研究更多静脉。这是一个很好的资源:https : //www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/

You said that every time you run the pip install you get Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. What you need to do is the following:

  1. Change Directory (go to to the one where the virtualenv.py) cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
  2. If you do an ls you will see that the script is there virtualenv.py
  3. Run the script like this: python virtualenv.py --distribute /the/path/at/which/you/want/the/new/venv/at theNameOfTheNewVirtualEnv

Hope this helps. My advice would be to research venvs more. Here is a good resource: https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/


回答 10

我遇到了麻烦,因为我习惯于安装python-virtualenv软件包。要使其正常工作,我必须使用删除该软件包apt-get remove python-virtualenv并使用进行安装pip install virtualenv

I had troubles because I used apt to install python-virtualenv package. To get it working I had to remove this package with apt-get remove python-virtualenv and install it with pip install virtualenv.


回答 11

确保该virtualenv可执行文件。

如果virtualenv未找到,/usr/local/bin/virtualenv则应运行完整路径()。

Ensure that virtualenv is executable.

If virtualenv is not found, running the full path (/usr/local/bin/virtualenv) should work.


回答 12

我认为可以使用简单的符号链接来解决您的问题,但是您正在创建指向错误文件的符号链接。据我所知virtualenv已安装到/Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv,(您可以更改Python版本的数字),因此用于创建符号链接命令应为:

ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv /usr/local/bin/virtualenv

I think your problem can be solved using a simple symbolic link, but you are creating the symbolic link to the wrong file. As far as I know virtualenv is installed to /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv, (you can change the numbers for your Python version) so the command for creating the symbolic link should be:

ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv /usr/local/bin/virtualenv

回答 13

在ubuntu 18.4上,使用pip的AWS安装无法正常工作。使用apt-get install为我解决了问题。

sudo apt-get install python-virtualenv

并检查

virtualenv --version

On ubuntu 18.4 on AWS installation with pip don’t work correctly. Using apt-get install the problem was solved for me.

sudo apt-get install python-virtualenv

and to check

virtualenv --version

回答 14

同样的问题: 所以,我只是做了pip uninstall virtualenv 那么 pip install virtualenv

pip install virtualenv --user

使用缓存的https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl 安装收集的软件包:virtualenv

然后我得到了:

脚本virtualenv安装在PATH之外的’/Users/brahim/Library/Python/2.7/bin’中。考虑将该目录添加到PATH,或者,如果您不想显示此警告,请使用–no-warn-script-location。

它清楚地说明了它的安装位置以及如何获得它

Same problem: So I just did pip uninstall virtualenv Then pip install virtualenv

pip install virtualenv --user

Collecting virtualenv Using cached https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl Installing collected packages: virtualenv

Then I got this :

The script virtualenv is installed in ‘/Users/brahim/Library/Python/2.7/bin’ which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.

which clearly says where it is installed and what to do to get it


回答 15

如果您使用的是Linux,请打开您的终端,然后在其中途键入virtualenv,然后使用Tab键自动完成。如果没有自动完成功能,请运行以下命令在系统上安装virtualenv:

mycomp$sudo apt-get install virtualenv
//if you're already super user.
mycomp#apt-get install virtualenv

现在,您可以导航到要创建项目的位置并执行以下操作:

myprj$pip3 install virtualenv    
//to install python 3.5 and above  
myprj$virtualenv venv --python=python3.5  
//to activate virtualenv  
(venv)myprj$source venv/bin/activate  
(venv)myprj$deactivate

If you’re using Linux, open your terminal and type virtualenv halfway and autocomplete with tab key. If there’s no auto-completion install virtualenv on your system by running:

mycomp$sudo apt-get install virtualenv
//if you're already super user.
mycomp#apt-get install virtualenv

You can now navigate to where you want to create your project and do:

myprj$pip3 install virtualenv    
//to install python 3.5 and above  
myprj$virtualenv venv --python=python3.5  
//to activate virtualenv  
(venv)myprj$source venv/bin/activate  
(venv)myprj$deactivate

回答 16

这在ubuntu 18及更高版本中有效(未经先前版本测试):

sudo apt install python3-virtualenv

this works in ubuntu 18 and above (not tested in previous versions):

sudo apt install python3-virtualenv

回答 17

请按照以下基本步骤设置虚拟环境

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

我们需要更新我们的 ~/.bashrc

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

~/.bashrc文件只是Shell脚本,只要您启动新终端,Bash就会运行。通常,您使用此文件来设置各种配置。在这种情况下,我们将设置一个名为的环境变量,WORKON_HOME 以指向我们的Python虚拟环境所在的目录。然后,我们从virtualenvwrapper加载任何必要的配置。

要更新~/.bashrc文件,只需使用标准的文本编辑器,nano可能是最容易操作的。一个更简单的解决方案是使用cat命令并完全避免使用编辑器:

echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

编辑〜/ .bashrc文件后,我们需要重新加载更改:

source ~/.bashrc

现在我们已经安装了virtualenv和virtualenvwrapper,下一步是实际创建Python虚拟环境-我们使用mkvirtualenv命令执行此操作。

mkvirtualenv YOURENV

Follow these basic steps to setup the virtual env

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

we need to update our ~/.bashrc

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

The ~/.bashrc file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called WORKON_HOME to point to the directory where our Python virtual environments live. We then load any necessary configurations from virtualenvwrapper .

To update your ~/.bashrc file simply use a standard text editor, nano is likely the easiest to operate. A more simple solution is to use the cat command and avoid editors entirely:

echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

After editing our ~/.bashrc file, we need to reload the changes:

source ~/.bashrc

Now that we have installed virtualenv and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv command.

mkvirtualenv YOURENV

回答 18

对我来说,它已安装在以下路径中(在MacOS上为python 2.7):$ HOME / Library / Python / 2.7 / bin

For me it was installed in this path (python 2.7 on MacOS): $HOME/Library/Python/2.7/bin


回答 19

我正在做Angela Yu的在线iOS类,遇到同样的问题,当我尝试运行时也遇到权限被拒绝的错误13 virtualenv --python=/{myPath} {newVirtualEnvName}

我通过以下方法解决了它:

  1. 切换到sudo用户 sudo su
  2. 导航到我的目标文件夹(我希望我的新虚拟环境驻留在其中),即。/ Users / muUserName / Environments /
  3. 运行命令python -m virtualenv python27,其中python27是我的新的虚拟环境的名称
  4. 上面在我的环境文件夹中创建了文件夹pathon27,然后我能够运行以启动我的virtualenvsource python27/bin/activate

I’m doing Angela Yu’s online iOS course and I was getting same problem plus also was getting permission denied error 13 when I was trying to run virtualenv --python=/{myPath} {newVirtualEnvName}

I solved it by:

  1. switching to sudo user sudo su
  2. navigating to my destination folder (where I want my new virtual env to live) ie. /Users/muUserName/Environments/
  3. run command python -m virtualenv python27 where python27 is a name of my new virtual environment
  4. above created folder pathon27 in my Environments folder, and then I was able to run source python27/bin/activate to start my virtualenv

回答 20

简单的答案是,如果您不是我(不是我)的sudo用户,则需要添加bin文件夹(/home/myusername/.local/bin)的路径。因此基本上,命令行会在您键入这些命令的路径中搜索哪个。

export PATH=/home/b18150/.local/bin:/usr/bin:/bin

在这里它将先搜索local/bin然后/usr/bin再搜索/bin

Simple answer is that if you are not a sudo user as I was not one.You need to add path of your bin folder (/home/myusername/.local/bin).So basically the command line searches in which of these path is the command which you have typed.

export PATH=/home/b18150/.local/bin:/usr/bin:/bin

here it will search in local/bin first then /usr/bin and then /bin.


回答 21

我有相同的问题很长时间了。我通过运行以下两个命令解决了这个问题,首先是安装,其次是激活env:

python3 -m pip install virtualenv
python3 -m virtualenv yourenvname

请注意,我使用python3,你可以把它改成只是python如果python3失败。谢谢。

I had the same problem for a long time. I solved it by running these two commands, first is to install second is to activate the env:

python3 -m pip install virtualenv
python3 -m virtualenv yourenvname

Note that I’m using python3, you can change it to just python if python3 fails. Thanks.


回答 22

apt update
apt upgrade
apt install ufw python virtualenv git unzip pv

3个命令,一切正常!

apt update
apt upgrade
apt install ufw python virtualenv git unzip pv

3 commands and everything working!


回答 23

sudo apt-get install python-virtualenv
sudo apt-get install python-virtualenv

在virtualenv中升级python

问题:在virtualenv中升级python

有没有一种方法可以升级virtualenv中使用的python版本(例如,如果出现错误修复版本)?

我可以pip freeze --local > requirements.txt删除目录和pip install -r requirements.txt,但这需要大量重新安装大型库,例如,numpy我经常使用。

我可以看到从2.6-> 2.7升级时这是一个优势,但是2.7.x-> 2.7.y呢?

Is there a way to upgrade the version of python used in a virtualenv (e.g. if a bugfix release comes out)?

I could pip freeze --local > requirements.txt, then remove the directory and pip install -r requirements.txt, but this requires a lot of reinstallation of large libraries, for instance, numpy, which I use a lot.

I can see this is an advantage when upgrading from, e.g., 2.6 -> 2.7, but what about 2.7.x -> 2.7.y?


回答 0

你看到吗?如果我没有误解这个答案,您可以尝试在旧版本的基础上创建一个新的virtualenv。您只需要知道哪个python将使用您的virtualenv(您将需要查看您的virtualenv版本)。

如果您的virtualenv安装了与旧版本相同的python版本,并且无法升级virtualenv软件包,则可能需要阅读此内容,以便使用所需的python版本安装virtualenv。

编辑

我已经测试了这种方法(在旧方法的基础上创建新的virtualenv的方法),它对我来说很好用。我认为,如果您从python 2.6更改为2.7或从2.7更改为3.x,则可能会遇到一些问题,但是如果您在同一版本内升级(保持在2.7不变),则不会有任何问题,因为所有软件包对于两个python版本,它们都位于相同的文件夹中(2.7.x和2.7.y软件包位于your_env / lib / python2.7 /中)。

如果更改了virtualenv python版本,则需要再次安装该版本的所有软件包(或仅将所需的软件包链接到新版本的packages文件夹中,即:your_env / lib / python_newversion / site-packages)

Did you see this? If I haven’t misunderstand that answer, you may try to create a new virtualenv on top of the old one. You just need to know which python is going to use your virtualenv (you will need to see your virtualenv version).

If your virtualenv is installed with the same python version of the old one and upgrading your virtualenv package is not an option, you may want to read this in order to install a virtualenv with the python version you want.

EDIT

I’ve tested this approach (the one that create a new virtualenv on top of the old one) and it worked fine for me. I think you may have some problems if you change from python 2.6 to 2.7 or 2.7 to 3.x but if you just upgrade inside the same version (staying at 2.7 as you want) you shouldn’t have any problem, as all the packages are held in the same folders for both python versions (2.7.x and 2.7.y packages are inside your_env/lib/python2.7/).

If you change your virtualenv python version, you will need to install all your packages again for that version (or just link the packages you need into the new version packages folder, i.e: your_env/lib/python_newversion/site-packages)


回答 1

如果您恰巧使用的是Python 3.3+随附的venv模块,则它支持一个--upgrade选项。根据文档

假设Python已就地升级,请升级环境目录以使用此版本的Python

python3 -m venv --upgrade ENV_DIR

If you happen to be using the venv module that comes with Python 3.3+, it supports an --upgrade option. Per the docs:

Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place

python3 -m venv --upgrade ENV_DIR

回答 2

再次更新: 以下方法在较新版本的virtualenv中可能不起作用。在尝试对旧版virtualenv进行修改之前,应将依赖项保存在需求文件(pip freeze > requirements.txt)中,并在其他位置进行备份。如果有任何问题,您仍然可以创建一个新的virtualenv并在其中安装旧的依赖项(pip install -r requirements.txt)。

更新:我最初回答5个月后就更改了答案。以下方法更方便,更可靠。

副作用:在将Python升级到v2.7.8之后在虚拟环境中Symbol not found: _SSLv2_method执行操作时,它还修复了异常import ssl

注意:目前,这仅适用于Python2.7.x


如果您在OS X上使用Homebrew Python,请首先使用deactivate所有virtualenv,然后升级Python:

brew update && brew upgrade python

运行以下命令(<EXISTING_ENV_PATH>是您的虚拟环境的路径):

cd <EXISTING_ENV_PATH>
rm .Python
rm bin/pip{,2,2.7}
rm bin/python{,2,2.7}
rm -r include/python2.7
rm lib/python2.7/*
rm -r lib/python2.7/distutils
rm lib/python2.7/site-packages/easy_install.*
rm -r lib/python2.7/site-packages/pip
rm -r lib/python2.7/site-packages/pip-*.dist-info
rm -r lib/python2.7/site-packages/setuptools
rm -r lib/python2.7/site-packages/setuptools-*.dist-info

最后,重新创建您的虚拟环境:

virtualenv <EXISTING_ENV_PATH>

这样一来,旧的Python核心文件和标准库(plus setuptoolspip)就被删除,而安装在其中的自定义库site-packages则在纯Python 中被保留并正常工作。二进制库可能需要也可能不需要重新安装才能正常运行。

这在安装了Django的5个虚拟环境中为我工作。

顺便说一句,如果./manage.py compilemessages之后仍然无法使用,请尝试以下操作:

brew install gettext && brew link gettext --force

Updated again: The following method might not work in newer versions of virtualenv. Before you try to make modifications to the old virtualenv, you should save the dependencies in a requirement file (pip freeze > requirements.txt) and make a backup of it somewhere else. If anything goes wrong, you can still create a new virtualenv and install the old dependencies in it (pip install -r requirements.txt).

Updated: I changed the answer 5 months after I originally answered. The following method is more convenient and robust.

Side effect: it also fixes the Symbol not found: _SSLv2_method exception when you do import ssl in a virtual environment after upgrading Python to v2.7.8.

Notice: Currently, this is for Python 2.7.x only.


If you’re using Homebrew Python on OS X, first deactivate all virtualenv, then upgrade Python:

brew update && brew upgrade python

Run the following commands (<EXISTING_ENV_PATH> is path of your virtual environment):

cd <EXISTING_ENV_PATH>
rm .Python
rm bin/pip{,2,2.7}
rm bin/python{,2,2.7}
rm -r include/python2.7
rm lib/python2.7/*
rm -r lib/python2.7/distutils
rm lib/python2.7/site-packages/easy_install.*
rm -r lib/python2.7/site-packages/pip
rm -r lib/python2.7/site-packages/pip-*.dist-info
rm -r lib/python2.7/site-packages/setuptools
rm -r lib/python2.7/site-packages/setuptools-*.dist-info

Finally, re-create your virtual environment:

virtualenv <EXISTING_ENV_PATH>

By doing so, old Python core files and standard libraries (plus setuptools and pip) are removed, while the custom libraries installed in site-packages are preserved and working, as soon as they are in pure Python. Binary libraries may or may not need to be reinstalled to function properly.

This worked for me on 5 virtual environments with Django installed.

BTW, if ./manage.py compilemessages is not working afterwards, try this:

brew install gettext && brew link gettext --force

回答 3

我无法在旧版本的基础上创建新的virtualenv。但是pip中有一些工具,可以更快地将需求重新安装到全新的venv中。Pip可以将requirements.txt中的每个项目构建到wheel包中,并将其存储在本地缓存中。当您创建新的venv并在其中运行pip install时,如果pip找到了它们,它将自动使用预建的轮子。车轮的安装速度比每个模块的setup.py运行速度快得多。

我的〜/ .pip / pip.conf看起来像这样:

[global]
download-cache = /Users/me/.pip/download-cache
find-links =
/Users/me/.pip/wheels/

[wheel]
wheel-dir = /Users/me/.pip/wheels

我安装了wheel(pip install wheel),然后运行pip wheel -r requirements.txt。这会将已构建的车轮存储在我的pip.conf中的wheel-dir中。

从那时起,每当我点安装这些要求中的任何一个时,它都会从轮子上安装它们,这非常快。

I wasn’t able to create a new virtualenv on top of the old one. But there are tools in pip which make it much faster to re-install requirements into a brand new venv. Pip can build each of the items in your requirements.txt into a wheel package, and store that in a local cache. When you create a new venv and run pip install in it, pip will automatically use the prebuilt wheels if it finds them. Wheels install much faster than running setup.py for each module.

My ~/.pip/pip.conf looks like this:

[global]
download-cache = /Users/me/.pip/download-cache
find-links =
/Users/me/.pip/wheels/

[wheel]
wheel-dir = /Users/me/.pip/wheels

I install wheel (pip install wheel), then run pip wheel -r requirements.txt. This stores the built wheels in the wheel-dir in my pip.conf.

From then on, any time I pip install any of these requirements, it installs them from the wheels, which is pretty quick.


回答 4

如何为现有的virtualenvwrapper项目升级Python版本并保持相同名称

我要为使用Doug Hellmann出色的virtualenvwrapper的任何人添加一个答案,特别是因为现有的答案对我没有帮助。

一些背景:

  • 我从事一些Python 2项目和Python 3项目;虽然我想使用python3 -m venv它,但它不支持Python 2环境
  • 当我开始一个新项目时,我使用mkproject它来创建虚拟环境,创建一个空项目目录,并在其中进行cds
  • 我想继续使用virtualenvwrapper的workon命令来激活任何项目,而与Python版本无关

方向:

假设您的现有项目已命名foo,并且当前正在运行Python 2(mkproject -p python2 foo),尽管从2.x升级到3.x,从3.6.0升级到3.6.1等的命令都是相同的。我还假设您当前位于激活的虚拟环境中。

1.停用并删除旧的虚拟环境:

$ deactivate
$ rmvirtualenv foo

请注意,如果您已将任何自定义命令添加到了挂钩(例如bin/postactivate),则需要在删除环境之前保存这些自定义命令。

2.将实际项目存储在temp目录中:

$ cd ..
$ mv foo foo-tmp

3.创建新的虚拟环境(和项目目录)并激活:

$ mkproject -p python3 foo

4.用实际项目替换生成的空项目目录,改回项目目录:

$ cd ..
$ mv -f foo-tmp foo
$ cdproject

5.重新安装依赖项,确认新的Python版本,等等:

$ pip install -r requirements.txt
$ python --version

如果这是一个常见的用例,我将考虑打开PR,为virtualenvwrapper 添加类似$ upgradevirtualenv/的内容$ upgradeproject

How to upgrade the Python version for an existing virtualenvwrapper project and keep the same name

I’m adding an answer for anyone using Doug Hellmann’s excellent virtualenvwrapper specifically since the existing answers didn’t do it for me.

Some context:

  • I work on some projects that are Python 2 and some that are Python 3; while I’d love to use python3 -m venv, it doesn’t support Python 2 environments
  • When I start a new project, I use mkproject which creates the virtual environment, creates an empty project directory, and cds into it
  • I want to continue using virtualenvwrapper’s workon command to activate any project irrespective of Python version

Directions:

Let’s say your existing project is named foo and is currently running Python 2 (mkproject -p python2 foo), though the commands are the same whether upgrading from 2.x to 3.x, 3.6.0 to 3.6.1, etc. I’m also assuming you’re currently inside the activated virtual environment.

1. Deactivate and remove the old virtual environment:

$ deactivate
$ rmvirtualenv foo

Note that if you’ve added any custom commands to the hooks (e.g., bin/postactivate) you’d need to save those before removing the environment.

2. Stash the real project in a temp directory:

$ cd ..
$ mv foo foo-tmp

3. Create the new virtual environment (and project dir) and activate:

$ mkproject -p python3 foo

4. Replace the empty generated project dir with real project, change back into project dir:

$ cd ..
$ mv -f foo-tmp foo
$ cdproject

5. Re-install dependencies, confirm new Python version, etc:

$ pip install -r requirements.txt
$ python --version

If this is a common use case, I’ll consider opening a PR to add something like $ upgradevirtualenv / $ upgradeproject to virtualenvwrapper.


回答 5

这种方法总是对我有用:

# First of all, delete all broken links. Replace  my_project_name` to your virtual env name
find ~/.virtualenvs/my_project_name/ -type l -delete
# Then create new links to the current Python version
virtualenv ~/.virtualenvs/my_project_name/
# It's it. Just repeat for each virtualenv located in ~/.virtualenvs

取自:

This approach always works for me:

# First of all, delete all broken links. Replace  my_project_name` to your virtual env name
find ~/.virtualenvs/my_project_name/ -type l -delete
# Then create new links to the current Python version
virtualenv ~/.virtualenvs/my_project_name/
# It's it. Just repeat for each virtualenv located in ~/.virtualenvs

Taken from:


回答 6

我将主目录从一台Mac移到了另一台(Mountain Lion到优胜美地),直到我失去了旧笔记本电脑的机会,才意识到虚拟机损坏了。brew自从Yosemite随附Python 2.7以来,我已经安装了指向Python 2.7的virtualenv点,因此我想将我的virtualenv更新为系统python。当我virtualenv在现有目录上运行时,OSError: [Errno 17] File exists: '/Users/hdara/bin/python2.7/lib/python2.7/config'出现错误。通过反复试验,我通过删除一些链接并手动修复了一些问题来解决此问题。这是我最终所做的(类似于@Rockalite所做的,但更简单):

cd <virtualenv-root>
rm lib/python2.7/config
rm lib/python2.7/lib-dynload
rm include/python2.7
rm .Python
cd lib/python2.7
gfind . -type l -xtype l | while read f; do ln -s -f /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/${f#./} $f; done

之后,我就可以在现有目录之上运行virtualenv了。

I moved my home directory from one mac to another (Mountain Lion to Yosemite) and didn’t realize about the broken virtualenv until I lost hold of the old laptop. I had the virtualenv point to Python 2.7 installed by brew and since Yosemite came with Python 2.7, I wanted to update my virtualenv to the system python. When I ran virtualenv on top of the existing directory, I was getting OSError: [Errno 17] File exists: '/Users/hdara/bin/python2.7/lib/python2.7/config' error. By trial and error, I worked around this issue by removing a few links and fixing up a few more manually. This is what I finally did (similar to what @Rockalite did, but simpler):

cd <virtualenv-root>
rm lib/python2.7/config
rm lib/python2.7/lib-dynload
rm include/python2.7
rm .Python
cd lib/python2.7
gfind . -type l -xtype l | while read f; do ln -s -f /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/${f#./} $f; done

After this, I was able to just run virtualenv on top of the existing directory.


回答 7

在使用Homebrew安装和升级Python3的OS X或macOS上,我必须先删除符号链接,然后python -m venv --upgrade ENV_DIR才能使用。

我将以下内容保存在upgrade_python3.sh中,所以我会记得从现在开始需要几个月的时间:

brew upgrade python3
find ~/.virtualenvs/ -type l -delete
find ~/.virtualenvs/ -type d -mindepth 1 -maxdepth 1 -exec python3 -m venv --upgrade "{}" \;

更新:虽然这乍看起来似乎很好,但是当我运行py.test时却出现了错误。最后,我只是从需求文件中重新创建了环境。

On OS X or macOS using Homebrew to install and upgrade Python3 I had to delete symbolic links before python -m venv --upgrade ENV_DIR would work.

I saved the following in upgrade_python3.sh so I would remember how months from now when I need to do it again:

brew upgrade python3
find ~/.virtualenvs/ -type l -delete
find ~/.virtualenvs/ -type d -mindepth 1 -maxdepth 1 -exec python3 -m venv --upgrade "{}" \;

UPDATE: while this seemed to work well at first, when I ran py.test it gave an error. In the end I just re-created the environment from a requirements file.


回答 8

如果您使用pipenv,我不知道是否可以在适当的位置升级环境,但是至少对于次要版本升级来说,它似乎足够聪明,在创建新环境时不从头开始重建软件包。例如,从3.6.4到3.6.5:

$ pipenv --python 3.6.5 install
Virtualenv already exists!
Removing existing virtualenv
Creating a v$ pipenv --python 3.6.5 install
Virtualenv already exists!
Removing existing virtualenv
Creating a virtualenv for this project
Using /usr/local/bin/python3.6m (3.6.5) to create virtualenv
Running virtualenv with interpreter /usr/local/bin/python3.6m
Using base prefix '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/python3.6
Also creating executable in /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD
Installing dependencies from Pipfile.lock (84dd0e)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 47/47  00:00:24
To activate this project's virtualenv, run the following:
 $ pipenv shell
$ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.
. /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/activate
bash-3.2$ . /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/activate
(autoscale-aBUhewiD) bash-3.2$ python
Python 3.6.5 (default, Mar 30 2018, 06:41:53) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>

If you’re using pipenv, I don’t know if it’s possible to upgrade an environment in place, but at least for minor version upgrades it seems to be smart enough not to rebuild packages from scratch when it creates a new environment. E.g., from 3.6.4 to 3.6.5:

$ pipenv --python 3.6.5 install
Virtualenv already exists!
Removing existing virtualenv…
Creating a v$ pipenv --python 3.6.5 install
Virtualenv already exists!
Removing existing virtualenv…
Creating a virtualenv for this project…
Using /usr/local/bin/python3.6m (3.6.5) to create virtualenv…
⠋Running virtualenv with interpreter /usr/local/bin/python3.6m
Using base prefix '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/python3.6
Also creating executable in /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD
Installing dependencies from Pipfile.lock (84dd0e)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 47/47 — 00:00:24
To activate this project's virtualenv, run the following:
 $ pipenv shell
$ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.
. /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/activate
bash-3.2$ . /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/activate
(autoscale-aBUhewiD) bash-3.2$ python
Python 3.6.5 (default, Mar 30 2018, 06:41:53) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>

回答 9

我只想澄清一下,因为有些答案是指venv,有些则是指virtualenv

在上支持使用-p--python标志virtualenv,但不支持venv。如果您有多个Python版本,并且想要指定使用哪个版本创建venv,请在命令行上执行以下操作:

malikarumi@Tetuoan2:~/Projects$ python3.6 -m venv {path to pre-existing dir you want venv in}

当然,您也可以venv像其他人指出的那样进行升级,但是前提是您已经升级了最初用于创建该Python的Python venv。您无法升级到系统上某个地方尚未拥有的Python版本,因此请确保首先获取所需的版本,然后再从中获取所需的所有Venv。

I just want to clarify, because some of the answers refer to venv and others refer to virtualenv.

Use of the -p or --python flag is supported on virtualenv, but not on venv. If you have more than one Python version and you want to specify which one to create the venv with, do it on the command line, like this:

malikarumi@Tetuoan2:~/Projects$ python3.6 -m venv {path to pre-existing dir you want venv in}

You can of course upgrade with venv as others have pointed out, but that assumes you have already upgraded the Python that was used to create that venv in the first place. You can’t upgrade to a Python version you don’t already have on your system somewhere, so make sure to get the version you want, first, then make all the venvs you want from it.


回答 10

步骤1:冻结要求并备份现有环境

pip freeze > requirements.txt
deactivate
mv env env_old

步骤2:安装Python 3.7并激活虚拟环境

sudo apt-get install python3.7-venv
python3.7 -m venv env
source env/bin/activate
python --version

步骤3:安装需求

sudo apt-get install python3.7-dev
pip3 install -r requirements.txt

Step 1: Freeze requirement & take a back-up of existing env

pip freeze > requirements.txt
deactivate
mv env env_old

Step 2: Install Python 3.7 & activate virutal environment

sudo apt-get install python3.7-venv
python3.7 -m venv env
source env/bin/activate
python --version

Step 3: Install requirements

sudo apt-get install python3.7-dev
pip3 install -r requirements.txt

回答 11

对于有问题的每个人

错误:命令'[‘/ Users / me / Sites / site / venv3 / bin / python3’,’-Im’,’ensurepip’,’-upgrade’,’-default-pip’]’返回非零退出状态1。

您必须安装python3.6-venv

 sudo apt-get install python3.6-venv

For everyone with the problem

Error: Command ‘[‘/Users/me/Sites/site/venv3/bin/python3’, ‘-Im’, ‘ensurepip’, ‘–upgrade’, ‘–default-pip’]’ returned non-zero exit status 1.

You have to install python3.6-venv

 sudo apt-get install python3.6-venv

康达能否取代对virtualenv的需求?

问题:康达能否取代对virtualenv的需求?

我在安装SciPy时遇到了麻烦,最近发现了Conda,特别是在我正在开发的Heroku应用程序上。

使用Conda,您可以创建与virtualenv十分相似的环境。我的问题是:

  1. 如果我使用Conda,它将取代对virtualenv的需求吗?如果没有,如何将两者一起使用?是否在Conda中安装virtualenv或在virtualenv中安装Conda?
  2. 我还需要使用点子吗?如果是这样,我仍然可以在隔离的环境中使用pip安装软件包吗?

I recently discovered Conda after I was having trouble installing SciPy, specifically on a Heroku app that I am developing.

With Conda you create environments, very similar to what virtualenv does. My questions are:

  1. If I use Conda will it replace the need for virtualenv? If not, how do I use the two together? Do I install virtualenv in Conda, or Conda in virtualenv?
  2. Do I still need to use pip? If so, will I still be able to install packages with pip in an isolated environment?

回答 0

  1. 康达取代了virtualenv。我认为这更好。它不仅限于Python,还可以用于其他语言。以我的经验,它提供了更加流畅的体验,尤其是对于科学包装。我第一次在Mac上正确安装MayaVi是使用conda

  2. 您仍然可以使用pip。实际上,conda安装pip在每个新环境中。它知道有关pip安装的软件包的信息。

例如:

conda list

列出当前环境中所有已安装的软件包。Conda安装的软件包显示如下:

sphinx_rtd_theme          0.1.7                    py35_0    defaults

并通过安装的pip带有<pip>标记:

wxpython-common           3.0.0.0                   <pip>
  1. Conda replaces virtualenv. In my opinion it is better. It is not limited to Python but can be used for other languages too. In my experience it provides a much smoother experience, especially for scientific packages. The first time I got MayaVi properly installed on Mac was with conda.

  2. You can still use pip. In fact, conda installs pip in each new environment. It knows about pip-installed packages.

For example:

conda list

lists all installed packages in your current environment. Conda-installed packages show up like this:

sphinx_rtd_theme          0.1.7                    py35_0    defaults

and the ones installed via pip have the <pip> marker:

wxpython-common           3.0.0.0                   <pip>

回答 1

简短的答案是,您只需要conda。

  1. Conda在单个软件包中有效地结合了pip和virtualenv的功能,因此,如果您使用的是conda,则不需要virtualenv。

  2. 您会惊讶conda支持多少个软件包。如果还不够,可以在conda下使用pip。

这是到conda页面的链接,用于比较conda,pip和virtualenv:

https://docs.conda.io/projects/conda/zh-CN/latest/commands.html#conda-vs-pip-vs-virtualenv-commands

Short answer is, you only need conda.

  1. Conda effectively combines the functionality of pip and virtualenv in a single package, so you do not need virtualenv if you are using conda.

  2. You would be surprised how many packages conda supports. If it is not enough, you can use pip under conda.

Here is a link to the conda page comparing conda, pip and virtualenv:

https://docs.conda.io/projects/conda/en/latest/commands.html#conda-vs-pip-vs-virtualenv-commands.


回答 2

虚拟环境和 pip

我会补充说,使用Anaconda可以轻松创建删除 conda环境。

> conda create --name <envname> python=<version> <optional dependencies>

> conda remove --name <envname> --all 

激活的环境中,通过conda或安装软件包pip

(envname)> conda install <package>

(envname)> pip install <package>

这些环境与conda的pip式软件包管理紧密相关,因此创建环境以及安装Python和非Python软件包都很简单。


朱皮特

此外,在环境中安装ipykernel在Jupyter笔记本的“内核”下拉菜单中会添加一个新列表,从而将可复制的环境扩展到笔记本。从Anaconda 4.1开始,添加了nbextensions,更轻松地为笔记本添加了扩展名。

可靠性

以我的经验,conda在安装大型库(例如numpy和)时更快,更可靠pandas。此外,如果您希望转移环境的保留状态,则可以通过共享克隆环境来实现。

Virtual Environments and pip

I will add that creating and removing conda environments is simple with Anaconda.

> conda create --name <envname> python=<version> <optional dependencies>

> conda remove --name <envname> --all 

In an activated environment, install packages via conda or pip:

(envname)> conda install <package>

(envname)> pip install <package>

These environments are strongly tied to conda’s pip-like package management, so it is simple to create environments and install both Python and non-Python packages.


Jupyter

In addition, installing ipykernel in an environment adds a new listing in the Kernels dropdown menu of Jupyter notebooks, extending reproducible environments to notebooks. As of Anaconda 4.1, nbextensions were added, adding extensions to notebooks more easily.

Reliability

In my experience, conda is faster and more reliable at installing large libraries such as numpy and pandas. Moreover, if you wish to transfer your the preserved state of an environment, you can do so by sharing or cloning an env.


回答 3

安装Conda将使您能够根据需要创建和删除python环境,从而为您提供与virtualenv相同的功能。

对于这两种发行版,您将能够创建一个隔离的文件系统树,在其中您可以根据需要安装和删除python软件包(可能是pip)。如果您想为不同的用例使用同一个库的不同版本,或者只想尝试进行一些发行并将其删除以节省磁盘空间,则可能会派上用场。

差异:

许可协议。虽然virtualenv已获得MIT最宽松的许可证,但Conda使用3条款BSD许可。

康达为您提供了自己的包装控制系统。这个程序包控制系统通常提供流行的非python软件的预编译版本(对于大多数流行的系统),这可以很容易地使某些机器学习程序包正常工作。也就是说,您不必为系统编译优化的C / C ++代码。虽然这对我们大多数人来说是一个很大的缓解,但它可能会影响此类库的性能。

与virtualenv不同,Conda至少在Linux系统上复制了一些系统库。该库可能不同步,从而导致程序行为不一致。

判决:

Conda很棒,应该是您开始机器学习之路的默认选择。它将为您节省一些时间,使他们无法使用gcc和许多软件包。但是,Conda并没有取代virtualenv。它引入了一些额外的复杂性,而这些复杂性可能并非总是需要的。它具有不同的许可。您可能要避免在分布式环境或HPC硬件上使用conda。

Installing Conda will enable you to create and remove python environments as you wish, therefore providing you with same functionality as virtualenv would.

In case of both distributions you would be able to create an isolated filesystem tree, where you can install and remove python packages (probably, with pip) as you wish. Which might come in handy if you want to have different versions of same library for different use cases or you just want to try some distribution and remove it afterwards conserving your disk space.

Differences:

License agreement. While virtualenv comes under most liberal MIT license, Conda uses 3 clause BSD license.

Conda provides you with their own package control system. This package control system often provides precompiled versions (for most popular systems) of popular non-python software, which can easy ones way getting some machine learning packages working. Namely you don’t have to compile optimized C/C++ code for you system. While it is a great relief for most of us, it might affect performance of such libraries.

Unlike virtualenv, Conda duplicating some system libraries at least on Linux system. This libraries can get out of sync leading to inconsistent behavior of your programs.

Verdict:

Conda is great and should be your default choice while starting your way with machine learning. It will save you some time messing with gcc and numerous packages. Yet, Conda does not replace virtualenv. It introduces some additional complexity which might not always be desired. It comes under different license. You might want to avoid using conda on a distributed environments or on HPC hardware.


回答 4

我同时使用和(截至2020年1月)它们具有一些肤浅的差异,因此适合我的不同用法。通过默认康达更喜欢在一个中央位置来管理你的环境的列表,而使得的virtualenv在当前目录中的文件夹。前者(集中式)在您进行机器学习时才有意义,例如,您有几个广泛的环境可用于许多项目,并且想从任何地方跳入其中。如果您正在做一些一次性项目,这些项目具有完全不同的lib需求集,而lib需求集实际上更多地属于项目本身,则后者(每个项目文件夹)才有意义。

Conda创建的空环境约为122MB,而virtualenv约为12MB,因此,这是您可能不希望将Conda环境分散在各处的另一个原因。

最后,表明Conda偏爱其集中式env的另一个表面现象是(同样,默认情况下),如果您确实在自己的项目文件夹中创建了一个Conda env并激活它,则出现在外壳中的名称前缀是(太长)绝对值文件夹的路径。您可以通过给它命名来解决该问题,但是virtualenv默认情况下做对了。

我希望随着两个程序包管理者争夺霸主地位,此信息会迅速过时,但这是今天的权衡:)

I use both and (as of Jan, 2020) they have some superficial differences that lend themselves to different usages for me. By default Conda prefers to manage a list of environments for you in a central location, whereas virtualenv makes a folder in the current directory. The former (centralized) makes sense if you are e.g. doing machine learning and just have a couple of broad environments that you use across many projects and want to jump into them from anywhere. The latter (per project folder) makes sense if you are doing little one-off projects that have completely different sets of lib requirements that really belong more to the project itself.

The empty environment that Conda creates is about 122MB whereas the virtualenv’s is about 12MB, so that’s another reason you may prefer not to scatter Conda environments around everywhere.

Finally, another superficial indication that Conda prefers its centralized envs is that (again, by default) if you do create a Conda env in your own project folder and activate it the name prefix that appears in your shell is the (way too long) absolute path to the folder. You can fix that by giving it a name, but virtualenv does the right thing by default.

I expect this info to become stale rapidly as the two package managers vie for dominance, but these are the trade-offs as of today :)


回答 5

Pipenv是另一个新的选择,也是我当前首选的启动和运行环境的方法。

目前,它是Python.org官方推荐的Python打包工具

Another new option and my current preferred method of getting an environment up and running is Pipenv

It is currently the officially recommended Python packaging tool from Python.org


回答 6

是的,conda它比容易安装得多virtualenv,并且几乎可以替代后者。

Yes, conda is a lot easier to install than virtualenv, and pretty much replaces the latter.


回答 7

我在公司工作,在几台没有管理员权限的计算机的防火墙后面

以我有限的python使用经验(两年),我遇到了几个库(JayDeBeApi,sasl),这些库通过pip安装时会抛出C ++依赖项错误错误:需要Microsoft Visual C ++ 14.0。使用“ Microsoft Visual C ++生成工具”获得它:http : //landinghub.visualstudio.com/visual-cpp-build-tools

这些用conda安装得很好,因此从那以后,我开始使用conda env。但是要阻止conda从c.programfiles里面安装依赖项并不容易,因为我没有写权限。

I work in corporate, behind several firewall with machine on which I have no admin acces

In my limited experience with python (2 years) i have come across few libraries (JayDeBeApi,sasl) which when installing via pip threw C++ dependency errors error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: http://landinghub.visualstudio.com/visual-cpp-build-tools

these installed fine with conda, hence since those days i started working with conda env. however it isnt easy to stop conda from installing dependency inside c.programfiles where i dont have write access.


“ pip install –user…”的目的是什么?

问题:“ pip install –user…”的目的是什么?

来自pip install --help

 --user      Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on
             Windows. (See the Python documentation for site.USER_BASE for full details.)

site.USER_BASE的文档是一个令人困扰的有趣的* NIX主题,我不明白。

--user普通英语的目的是什么?为什么安装软件包很~/.local/重要?为什么不将可执行文件放在$ PATH中?

From pip install --help:

 --user      Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on
             Windows. (See the Python documentation for site.USER_BASE for full details.)

The documentation for site.USER_BASE is a terrifying wormhole of interesting *NIX subject matter that I don’t understand.

What is the purpose of --user in plain english? Why would intalling the package to ~/.local/ matter? Why not just put an executable somewhere in my $PATH?


回答 0

pip默认将Python软件包安装到系统目录(例如/usr/local/lib/python3.4)。这需要root访问。

--user 而是在您的主目录中进行pip安装软件包,该软件包不需要任何特殊特权。

pip defaults to installing Python packages to a system directory (such as /usr/local/lib/python3.4). This requires root access.

--user makes pip install packages in your home directory instead, which doesn’t require any special privileges.


回答 1

--user安装在中site.USER_SITE

就我而言,是/Users/.../Library/Python/2.7/bin。因此,我已将其添加到我的PATH(~/.bash_profile文件中):

export PATH=$PATH:/Users/.../Library/Python/2.7/bin

--user installs in site.USER_SITE.

For my case, it was /Users/.../Library/Python/2.7/bin. So I have added that to my PATH (in ~/.bash_profile file):

export PATH=$PATH:/Users/.../Library/Python/2.7/bin

回答 2

其他答案提到site.USER_SITE放置Python包的位置。如果您要查找二进制文件,请输入{site.USER_BASE}/bin

如果要将此目录添加到外壳程序的搜索路径中,请使用:

export PATH="${PATH}:$(python3 -c 'import site; print(site.USER_BASE)')/bin"

Other answers mention site.USER_SITE as where Python packages get placed. If you’re looking for binaries, these go in {site.USER_BASE}/bin.

If you want to add this directory to your shell’s search path, use:

export PATH="${PATH}:$(python3 -c 'import site; print(site.USER_BASE)')/bin"

回答 3

只是警告:

根据此问题--user当前在虚拟环境中无效pip,因为用户位置对于虚拟环境而言实际上没有任何意义。

因此,请勿pip install --user some_pkg 在虚拟环境内使用,否则,虚拟环境pip会被混淆。有关更多详细信息,请参见此答案

Just a warning:

According to this issue, --user is currently not valid inside a virtual env’s pip, since a user location doesn’t really make sense for a virtual environment.

So do not use pip install --user some_pkg inside a virtual environment, otherwise, virtual environment’s pip will be confused. See this answer for more details.


回答 4

最好的方法是安装virtualenv,不需要--user混乱。您将获得更大的灵活性,而不必担心每次pip安装软件包时都会破坏不同的python版本和项目。

https://virtualenv.pypa.io/en/stable/

Best way to is install virtualenv and not require the --user confusion. You will get more flexibility and not worry about clobbering the different python versions and projects everytime you pip install a package.

https://virtualenv.pypa.io/en/stable/


回答 5

在macOS上,使用该--user标志的原因是确保我们不会破坏OS依赖的库。一个保守许多的MacOS用户的做法是为了避免在安装或使用,需要一个命令更新点子sudo。因此,这包括安装到/usr/local/bin

参考:为Neovim安装python(https://github.com/zchee/deoplete-jedi/wiki/Setting-up-Python-for-Neovim

我不是所有清楚为什么安装到/usr/local/bin是鉴于该系统只依赖于Python的二进制文件在Mac上的危险/Library/Frameworks//usr/bin。我怀疑是因为如上所述,安装到/usr/local/binrequire sudo可以打开导致系统库代价高昂的错误的大门。因此,安装到内部~/.local/bin是避免这种风险的可靠方法。

参考:在Mac上使用python(https://docs.python.org/2/using/mac.html

最后,程度有安装软件包到的好处/usr/local/bin,我不知道它是有道理的,从改变目录的所有者rootuser这样可以避免必须使用,sudo同时仍可以防止进行依赖于系统的更改。*这是否是安全性默认设置,它是过去更频繁地使用Unix系统(作为服务器)的结果吗?或者至少,对于不托管服务器的Mac用户,这是一个好方法吗?

*注意:Mac的系统完整性保护(SIP)功能似乎还可以保护用户免于更改依赖于系统的库。

-E

On macOS, the reason for using the --user flag is to make sure we don’t corrupt the libraries the OS relies on. A conservative approach for many macOS users is to avoid installing or updating pip with a command that requires sudo. Thus, this includes installing to /usr/local/bin

Ref: Installing python for Neovim (https://github.com/zchee/deoplete-jedi/wiki/Setting-up-Python-for-Neovim)

I’m not all clear why installing into /usr/local/bin is a risk on a Mac given the fact that the system only relies on python binaries in /Library/Frameworks/ and /usr/bin. I suspect it’s because as noted above, installing into /usr/local/bin requires sudo which opens the door to making a costly mistake with the system libraries. Thus, installing into ~/.local/bin is a sure fire way to avoid this risk.

Ref: Using python on a Mac (https://docs.python.org/2/using/mac.html)

Finally, to the degree there is a benefit of installing packages into the /usr/local/bin, I wonder if it makes sense to change the owner of the directory from root to user? This would avoid having to use sudo while still protecting against making system-dependent changes.* Is this a security default a relic of how Unix systems were more often used in the past (as servers)? Or at minimum, just a good way to go for Mac users not hosting a server?

*Note: Mac’s System Integrity Protection (SIP) feature also seems to protect the user from changing the system-dependent libraries.

– E


回答 6

没有虚拟环境

pip <command> --user 更改当前pip命令的范围,使其在当前用户帐户的本地python软件包安装位置(而不是系统范围的软件包安装位置)上起作用,这是默认设置。

  • 请参阅《 PIP用户指南》中的“ 用户安装 ”。

这仅在多用户计算机上才真正重要。安装到系统位置的所有内容都将对所有用户可见,因此,安装到该用户位置将使该软件包安装与其他用户分开(他们不会看到该软件包,因此必须自己单独安装才能使用)。因为可能存在版本冲突,所以安装具有其他软件包所需要的依赖关系的软件包可能会引起问题,因此最好不要将给定用户使用的所有软件包推送到系统安装位置。

  • 如果是单用户计算机,则安装到该--user位置几乎没有差异。它将被安装到一个不同的文件夹中,该文件夹可能或不需要添加到路径中,具体取决于软件包及其使用方式(许多软件包都安装了命令行工具,这些工具必须位于要从外壳程序运行的路径上) 。
  • 如果它是多用户计算机,--user则首选使用root / sudo或要求管理员安装并影响每个用户的Python环境,除非管理员希望默认情况下使所有用户都能使用常规软件包。
    • 注意: 根据评论,在大多数Unix / Linux安装中,已经指出系统安装应使用常规的软件包管理器,例如apt,而不是pip

使用虚拟环境

--user在活动的venv / virtualenv环境中,该选项将安装到本地用户python位置(与没有虚拟环境时相同)。

默认情况下,软件包已安装到虚拟环境,但是如果使用--user它,将强制将其安装在虚拟环境之外的用户python脚本目录中(在Windows中,当前c:\users\<username>\appdata\roaming\python\python37\scripts适用于Python 3.7)。

但是,您将无法从虚拟环境中访问系统或用户安装(即使您--user在虚拟环境中使用过)。

如果使用--system-site-packages参数安装虚拟环境,则可以访问python的系统脚本文件夹。我相信这也包括用户python脚本文件夹,但是我不确定。但是,这样做可能会产生意想不到的后果,并且它不是使用虚拟环境的预期方式。


Python系统和本地用户安装文件夹的位置

您可以使用找到python用户安装文件夹的位置python -m site --user-base。我在“问题与解答”,文档中发现有冲突的信息,并且实际上在PC上使用此命令来确定默认值,但它们位于用户主目录(~* nix中的快捷方式,c:\users\<username>通常用于Windows)下。


其他详情

--user选项并非对每个命令都有效。例如,pip uninstall将在安装了软件包的位置(用户文件夹,虚拟环境文件夹等)中查找和卸载软件包,并且该--user选项无效。

随同pip install --user安装的东西将安装在本地位置,该位置只能由当前用户帐户查看,并且不需要root用户访问权限(在* nix上)或管理员访问权限(在Windows上)。

--user选项会修改所有 pip接受该命令的命令,以便在用户安装文件夹中查看/操作,因此,如果使用pip list --user该选项,则只会显示您使用进行安装的软件包pip install --user

Without Virtual Environments

pip <command> --user changes the scope of the current pip command to work on the current user account’s local python package install location, rather than the system-wide package install location, which is the default.

This only really matters on a multi-user machine. Anything installed to the system location will be visible to all users, so installing to the user location will keep that package installation separate from other users (they will not see it, and would have to install it themselves separately to use it). Because there can be version conflicts, installing a package with dependencies needed by other packages can cause problems, so it’s best not to push all packages a given user uses to the system install location.

  • If it is a single-user machine, there is little or no difference to installing to the --user location. It will be installed to a different folder, that may or may not need to be added to the path, depending on the package and how it’s used (many packages install command-line tools that must be on the path to run from a shell).
  • If it is a multi-user machine, --user is preferred to using root/sudo or requiring administrator installation and affecting the Python environment of every user, except in cases of general packages that the administrator wants to make available to all users by default.
    • Note: Per comments, on most Unix/Linux installs it has been pointed out that system installs should use the general package manager, such as apt, rather than pip.

With Virtual Environments

The --user option in an active venv/virtualenv environment will install to the local user python location (same as without a virtual environment).

Packages are installed to the virtual environment by default, but if you use --user it will force it to install outside the virtual environments, in the users python script directory (in Windows, this currently is c:\users\<username>\appdata\roaming\python\python37\scripts for me with Python 3.7).

However, you won’t be able to access a system or user install from within virtual environment (even if you used --user while in a virtual environment).

If you install a virtual environment with the --system-site-packages argument, you will have access to the system script folder for python. I believe this included the user python script folder as well, but I’m unsure. However, there may be unintended consequences for this and it is not the intended way to use virtual environments.


Location of the Python System and Local User Install Folders

You can find the location of the user install folder for python with python -m site --user-base. I’m finding conflicting information in Q&A’s, the documentation and actually using this command on my PC as to what the defaults are, but they are underneath the user home directory (~ shortcut in *nix, and c:\users\<username> typically for Windows).


Other Details

The --user option is not a valid for every command. For example pip uninstall will find and uninstall packages wherever they were installed (in the user folder, virtual environment folder, etc.) and the --user option is not valid.

Things installed with pip install --user will be installed in a local location that will only be seen by the current user account, and will not require root access (on *nix) or administrator access (on Windows).

The --user option modifies all pip commands that accept it to see/operate on the user install folder, so if you use pip list --user it will only show you packages installed with pip install --user.


回答 7

为什么不将可执行文件放在$ PATH中

~/.local/bin directory从理论上讲应该在您的计算机中$PATH

根据这些人 说法,$PATH在使用时不添加它是一个错误systemd

该答案对其进行了更广泛的解释。

但是,即使你的发行版包括~/.local/bin目录$PATH,它可能是以下形式(内~/.profile):

if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

如果该目录以前不在目录中,则需要注销并再次登录

Why not just put an executable somewhere in my $PATH

~/.local/bin directory is theoretically expected to be in your $PATH.

According to these people it’s a bug not adding it in the $PATH when using systemd.

This answer explains it more extensively.

But even if your distro includes the ~/.local/bin directory to the $PATH, it might be in the following form (inside ~/.profile):

if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

which would require you to logout and login again, if the directory was not there before.


为Python安装pip,virtualenv和分发的正确方法是什么?

问题:为Python安装pip,virtualenv和分发的正确方法是什么?

简短问题

背景

4314376问题的回答中,我建议使用,ez_setup以便随后进行安装pipvirtualenv如下所示:

curl -O http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
sudo easy_install pip
sudo pip install virtualenv

我最初是从Jesse Noller的博客文章中删除这些说明的,所以您想在Mac上使用Python吗?。我喜欢保持干净的全局site-packages目录的想法,因此我安装的唯一其他软件包是 virtualenvwrapperand distribute。(distribute由于此Python公共服务公告,我最近添加到了我的工具箱中。要安装这两个软件包,我使用了:

sudo pip install virtualenvwrapper
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py

没有更多的设置工具和easy_install

要真正遵循该Python公共服务声明,在全新安装的Python上,请执行以下操作:

curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
sudo easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper

雕文的斥责

在对SO问题4314376的回答的评论中,SO用户Glyph说:

没有。永远不要做sudo python setup.py install任何事情。编写〜/ .pydistutils.cfg,将您的pip安装放入〜/ .local或其他内容。特别是命名为的文件ez_setup.py往往会吸收诸如setuptools和easy_install之类的较新版本,这可能会破坏操作系统上的其他内容。

回到简短的问题

因此,Glyph的回答使我想到了最初的问题:

Short Question

Background

In my answer to SO question 4314376, I recommended using ez_setup so that you could then install pip and virtualenv as follows:

curl -O http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
sudo easy_install pip
sudo pip install virtualenv

I originally pulled these instructions from Jesse Noller’s blog post So you want to use Python on the Mac?. I like the idea of keeping a clean global site-packages directory, so the only other packages I install there are virtualenvwrapper and distribute. (I recently added distribute to my toolbox because of this Python public service announcement. To install these two packages, I used:

sudo pip install virtualenvwrapper
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py

No more setuptools and easy_install

To really follow that Python public service announcement, on a fresh Python install, I would do the following:

curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
sudo easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper

Glyph’s Rebuke

In a comment to my answer to SO question 4314376, SO user Glyph stated:

NO. NEVER EVER do sudo python setup.py install whatever. Write a ~/.pydistutils.cfg that puts your pip installation into ~/.local or something. Especially files named ez_setup.py tend to suck down newer versions of things like setuptools and easy_install, which can potentially break other things on your operating system.

Back to the short question

So Glyph’s response leads me to my original question:


回答 0

您可以执行此操作而无需在python本身中安装任何东西

您不需要sudo或任何特权。

您无需编辑任何文件。

将virtualenv安装到引导虚拟环境中。使用该虚拟环境创建更多内容。由于virtualenv附带了pip和分发,因此您一次安装即可获得所有内容。

  1. 下载virtualenv:
  2. 解压源压缩包
  3. 使用解压缩的tarball创建干净的虚拟环境。该虚拟环境将用于“引导”其他环境。您的所有虚拟环境将自动包含pip和分发。
  4. 使用pip将virtualenv安装到该引导环境中。
  5. 使用该引导环境创建更多内容!

这是bash中的一个示例:

# Select current version of virtualenv:
VERSION=12.0.7
# Name your first "bootstrap" environment:
INITIAL_ENV=bootstrap
# Set to whatever python interpreter you want for your first environment:
PYTHON=$(which python)
URL_BASE=https://pypi.python.org/packages/source/v/virtualenv

# --- Real work starts here ---
curl -O $URL_BASE/virtualenv-$VERSION.tar.gz
tar xzf virtualenv-$VERSION.tar.gz
# Create the first "bootstrap" environment.
$PYTHON virtualenv-$VERSION/virtualenv.py $INITIAL_ENV
# Don't need this anymore.
rm -rf virtualenv-$VERSION
# Install virtualenv into the environment.
$INITIAL_ENV/bin/pip install virtualenv-$VERSION.tar.gz

现在,您可以使用“引导”环境创建更多内容:

# Create a second environment from the first:
$INITIAL_ENV/bin/virtualenv py-env1
# Create more:
$INITIAL_ENV/bin/virtualenv py-env2

发疯!

注意

假设您没有使用过旧的virtualenv版本。旧版本需要标记--no-site-packges(并取决于Python的版本--distribute)。现在,您可以使用just python virtualenv.py path-to-bootstrap或来创建引导环境python3 virtualenv.py path-to-bootstrap

You can do this without installing anything into python itself.

You don’t need sudo or any privileges.

You don’t need to edit any files.

Install virtualenv into a bootstrap virtual environment. Use the that virtual environment to create more. Since virtualenv ships with pip and distribute, you get everything from one install.

  1. Download virtualenv:
  2. Unpack the source tarball
  3. Use the unpacked tarball to create a clean virtual environment. This virtual environment will be used to “bootstrap” others. All of your virtual environments will automatically contain pip and distribute.
  4. Using pip, install virtualenv into that bootstrap environment.
  5. Use that bootstrap environment to create more!

Here is an example in bash:

# Select current version of virtualenv:
VERSION=12.0.7
# Name your first "bootstrap" environment:
INITIAL_ENV=bootstrap
# Set to whatever python interpreter you want for your first environment:
PYTHON=$(which python)
URL_BASE=https://pypi.python.org/packages/source/v/virtualenv

# --- Real work starts here ---
curl -O $URL_BASE/virtualenv-$VERSION.tar.gz
tar xzf virtualenv-$VERSION.tar.gz
# Create the first "bootstrap" environment.
$PYTHON virtualenv-$VERSION/virtualenv.py $INITIAL_ENV
# Don't need this anymore.
rm -rf virtualenv-$VERSION
# Install virtualenv into the environment.
$INITIAL_ENV/bin/pip install virtualenv-$VERSION.tar.gz

Now you can use your “bootstrap” environment to create more:

# Create a second environment from the first:
$INITIAL_ENV/bin/virtualenv py-env1
# Create more:
$INITIAL_ENV/bin/virtualenv py-env2

Go nuts!

Note

This assumes you are not using a really old version of virtualenv. Old versions required the flags --no-site-packges (and depending on the version of Python, --distribute). Now you can create your bootstrap environment with just python virtualenv.py path-to-bootstrap or python3 virtualenv.py path-to-bootstrap.


回答 1

我认为Glyph的意思是这样的:

  1. 创建目录~/.local(如果尚不存在)。
  2. 在你的~/.bashrc,保证~/.local/binPATH~/.localPYTHONPATH
  3. 创建一个~/.pydistutils.cfg包含以下内容的文件

    [install]
    prefix=~/.local

    这是一个标准的ConfigParser格式文件。

  4. 下载 distribute_setup.py并运行python distribute_setup.py(否sudo)。如果它抱怨site-packages目录不存在,请手动创建它:

    mkdir -p〜/ .local / lib / python2.7 / site-packages /

  5. 运行which easy_install以验证它来自~/.local/bin

  6. pip install virtualenv
  7. pip install virtualenvwrapper
  8. 创建一个包含文件夹的虚拟环境,例如 ~/.virtualenvs
  9. ~/.bashrc添加

    export WORKON_HOME
    source ~/.local/bin/virtualenvwrapper.sh

就是这样,根本不使用sudo任何功能,并且您的Python环境在中~/.local,与操作系统的Python完全分开。免责声明:不确定virtualenvwrapper这种情况下的兼容性-我无法在我的系统上测试它:-)

I think Glyph means do something like this:

  1. Create a directory ~/.local, if it doesn’t already exist.
  2. In your ~/.bashrc, ensure that ~/.local/bin is on PATH and that ~/.local is on PYTHONPATH.
  3. Create a file ~/.pydistutils.cfg which contains

    [install]
    prefix=~/.local
    

    It’s a standard ConfigParser-format file.

  4. Download distribute_setup.py and run python distribute_setup.py (no sudo). If it complains about a non-existing site-packages directory, create it manually:

    mkdir -p ~/.local/lib/python2.7/site-packages/

  5. Run which easy_install to verify that it’s coming from ~/.local/bin

  6. Run pip install virtualenv
  7. Run pip install virtualenvwrapper
  8. Create a virtual env containing folder, say ~/.virtualenvs
  9. In ~/.bashrc add

    export WORKON_HOME
    source ~/.local/bin/virtualenvwrapper.sh
    

That’s it, no use of sudo at all and your Python environment is in ~/.local, completely separate from the OS’s Python. Disclaimer: Not sure how compatible virtualenvwrapper is in this scenario – I couldn’t test it on my system :-)


回答 2

如果您按照我在此答案中链接的若干教程中的建议进行操作,则无需在Walker和Vinay的答案中进行一些复杂的“手动”步骤即可获得所需的效果。如果您使用的是Ubuntu:

sudo apt-get install python-pip python-dev

在OS X中,可以通过使用自制软件安装python来实现等效功能(此处有更多详细信息)。

brew install python

随着pip安装,你可以用它来获得剩余的包(可以省略sudo在OS X,如您使用的Python的安装)。

sudo pip install virtualenvwrapper

(这是您需要全局安装的唯一软件包,我怀疑它是否会与操作系统中的任何系统级冲突。如果您想超级安全,可以保留发行版的版本sudo apt-get install virtualenvwrapper

注意:在Ubuntu 14.04中,pip install会出现一些错误,因此我使用pip3 install virtualenv virtualenvwrapper并添加VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3到我的.bashrc/.zshrc文件中。

然后添加到.bashrc文件中

export WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

并获取它

. ~/.bashrc

基本上就是这样。现在唯一的决定是是否要创建一个virtualenv来包含系统级程序包

mkvirtualenv --system-site-packages foo

在不需要重新安装现有系统软件包的情况下,它们会与系统解释器的版本符号链接。注意:您仍然可以安装新软件包并升级现有的包含在系统中的软件包,而无需使用sudo-我对其进行了测试,并且它可以在不中断系统解释器的情况下正常运行。

kermit@hocus-pocus:~$ sudo apt-get install python-pandas
kermit@hocus-pocus:~$ mkvirtualenv --system-site-packages s
(s)kermit@hocus-pocus:~$ pip install --upgrade pandas
(s)kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.10.1
(s)kermit@hocus-pocus:~$ deactivate
kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.8.0

如果您想要一个完全独立的环境,那么另一种方法是

mkvirtualenv --no-site-packages bar

或假设这是默认选项,只需

mkvirtualenv bar

结果是您拥有了一个新的virtualenv,您可以在其中自由而无须地安装自己喜欢的软件包

pip install flask

If you follow the steps advised in several tutorials I linked in this answer, you can get the desired effect without the somewhat complicated “manual” steps in Walker’s and Vinay’s answers. If you’re on Ubuntu:

sudo apt-get install python-pip python-dev

The equivalent is achieved in OS X by using homebrew to install python (more details here).

brew install python

With pip installed, you can use it to get the remaining packages (you can omit sudo in OS X, as you’re using your local python installation).

sudo pip install virtualenvwrapper

(these are the only packages you need installed globally and I doubt that it will clash with anything system-level from the OS. If you want to be super-safe, you can keep the distro’s versions sudo apt-get install virtualenvwrapper)

Note: in Ubuntu 14.04 I receive some errors with pip install, so I use pip3 install virtualenv virtualenvwrapper and add VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 to my .bashrc/.zshrc file.

You then append to your .bashrc file

export WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

and source it

. ~/.bashrc

This is basically it. Now the only decision is whether you want to create a virtualenv to include system-level packages

mkvirtualenv --system-site-packages foo

where your existing system packages don’t have to be reinstalled, they are symlinked to the system interpreter’s versions. Note: you can still install new packages and upgrade existing included-from-system packages without sudo – I tested it and it works without any disruptions of the system interpreter.

kermit@hocus-pocus:~$ sudo apt-get install python-pandas
kermit@hocus-pocus:~$ mkvirtualenv --system-site-packages s
(s)kermit@hocus-pocus:~$ pip install --upgrade pandas
(s)kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.10.1
(s)kermit@hocus-pocus:~$ deactivate
kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.8.0

The alternative, if you want a completely separated environment, is

mkvirtualenv --no-site-packages bar

or given that this is the default option, simply

mkvirtualenv bar

The result is that you have a new virtualenv where you can freely and sudolessly install your favourite packages

pip install flask

回答 3

从Python 3.4开始

Python 3.3添加了venv模块,Python 3.4添加了surepip模块。这使自举点变得简单:

python -m确保

可能在venv虚拟环境中呼吁这样做之前。

PEP 453中描述了保证的点子。

Python 3.4 onward

Python 3.3 adds the venv module, and Python 3.4 adds the ensurepip module. This makes bootstrapping pip as easy as:

python -m ensurepip

Perhaps preceded by a call to venv to do so inside a virtual environment.

Guaranteed pip is described in PEP 453.


回答 4

在Ubuntu上:

sudo apt-get install python-virtualenv

该软件包python-pip是一个依赖项,因此也将被安装。

On Ubuntu:

sudo apt-get install python-virtualenv

The package python-pip is a dependency, so it will be installed as well.


回答 5

我做了这个程序供我们在工作中使用。

cd ~
curl -s https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz | tar xvz
cd pip-1.3.1
python setup.py install --user
cd ~
rm -rf pip-1.3.1

$HOME/.local/bin/pip install --user --upgrade pip distribute virtualenvwrapper

# Might want these three in your .bashrc
export PATH=$PATH:$HOME/.local/bin
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--distribute"
source $HOME/.local/bin/virtualenvwrapper.sh

mkvirtualenv mypy
workon mypy
pip install --upgrade distribute
pip install pudb # Or whatever other nice package you might want.

安全意识的要点:

  1. curl会进行ssl验证。wget没有。
  2. 从pip 1.3.1开始,pip还执行ssl验证。
  3. 与github tarball相比,更少的用户可以上传pypi tarball。

I made this procedure for us to use at work.

cd ~
curl -s https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz | tar xvz
cd pip-1.3.1
python setup.py install --user
cd ~
rm -rf pip-1.3.1

$HOME/.local/bin/pip install --user --upgrade pip distribute virtualenvwrapper

# Might want these three in your .bashrc
export PATH=$PATH:$HOME/.local/bin
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--distribute"
source $HOME/.local/bin/virtualenvwrapper.sh

mkvirtualenv mypy
workon mypy
pip install --upgrade distribute
pip install pudb # Or whatever other nice package you might want.

Key points for the security minded:

  1. curl does ssl validation. wget doesn’t.
  2. Starting from pip 1.3.1, pip also does ssl validation.
  3. Fewer users can upload the pypi tarball than a github tarball.

回答 6

更新:自2013年7月起,该项目不再维护。作者建议使用pyenv。(pyenv没有对virtualenv的内置支持,但是可以很好地使用它。)

Pythonbrew是python的版本管理器,并支持virtualenv。

使用venvs安装pythonbrew和python版本后,真的很容易:

# Initializes the virtualenv 
pythonbrew venv init

# Create a virtual/sandboxed environment 
pythonbrew venv create mycoolbundle  

# Use it 
pythonbrew venv use mycoolbundle

Update: As of July 2013 this project is no longer maintained. The author suggests using pyenv. (pyenv does not have built-in support for virtualenv, but plays nice with it.)

Pythonbrew is a version manager for python and comes with support for virtualenv.

After installing pythonbrew and a python-version using venvs is really easy:

# Initializes the virtualenv 
pythonbrew venv init

# Create a virtual/sandboxed environment 
pythonbrew venv create mycoolbundle  

# Use it 
pythonbrew venv use mycoolbundle

回答 7

如果您确定这是您要执行的操作,则执行sudo python setup.py安装没有问题。

不同之处在于它将使用操作系统的site-packages目录作为要复制的.py文件的目标。

因此,如果您希望点子在整个范围内都可访问,那可能就是方法。我并不是说其他​​人的方法是不好的,但这可能很公平。

There is no problem to do sudo python setup.py install, if you’re sure it’s what you want to do.

The difference is that it will use the site-packages directory of your OS as a destination for .py files to be copied.

so, if you want pip to be accessible os wide, that’s probably the way to go. I do not say that others way are bad, but this is probably fair enough.


回答 8

安装ActivePython。它包括pip,virtualenv和Distribute。

Install ActivePython. It includes pip, virtualenv and Distribute.


回答 9

我最近遇到了同样的问题。我越来越偏爱“始终使用virtualenv”的心态,所以我的问题是使用pip安装virtualenv,而没有将distribution安装到我的全局目录或用户site-packages目录中。为此,我手动下载了distribute,pip和virtualenv,然后为每个文件运行“ python setup.py install –prefix〜/ .local / python-private”(临时设置为PYTHONPATH =〜/ .local / python-private),以便安装脚本能够找到分发)。我已经将virtualenv脚本移到了我在PATH上的另一个目录中,并对其进行了编辑,以便可以在sys.path上找到分发和virtualenv模块。Tada:我没有在/ usr,/ usr / local或用户站点软件包目录中安装任何东西,但是我可以在任何地方运行virtualenv,在该virtualenv中我得到了pip。

I came across the same problem recently. I’m becoming more partial to the “always use a virtualenv” mindset, so my problem was to install virtualenv with pip without installing distribute to my global or user site-packages directory. To do this, I manually downloaded distribute, pip and virtualenv, and for each one I ran “python setup.py install –prefix ~/.local/python-private” (with a temporary setting of PYTHONPATH=~/.local/python-private) so that setup scripts were able to find distribute). I’ve moved the virtualenv script to another directory I have on my PATH and edited it so that the distribute and virtualenv modules can be found on sys.path. Tada: I did not install anything to /usr, /usr/local or my user site-packages dir, but I can run virtualenv anywhere, and in that virtualenv I get pip.


回答 10

在较旧的操作系统提供的Python版本之上,即使在virtualenv中也安装了升级的SSL模块时,我遇到了各种各样的问题(请参阅下文),所以我现在使用pyenv

pyenv使安装新的Python版本变得非常容易,并支持virtualenvs。入门是比的食谱在其他的答案中所列的virtualenv容易:

  • 在Mac上,键入brew install pyenv,在Linux上,使用pyenv-installer
  • 这将为您提供内置的virtualenv支持以及Python版本切换(如果需要)
  • 与Python 2或3搭配使用时效果很好,可以一次安装多个版本

这样可以很好地将“新Python”版本和virtualenv与系统Python隔离开。因为您可以轻松地使用更新的Python(第2.7.9版),所以SSL模块已经升级,当然,像任何现代的virtualenv设置一样,您与系统Python模块隔离。

一些不错的教程:

pyenv-virtualenv插件现在内建-类型pyenv commands | grep virtualenv检查。我不会首先使用pyenv-virtualenvwrapper插件-了解如何与pyenv集成程度更高的pyenv-virtualenv进行交流,因为这涵盖了virtualenvwrapper的大部分工作。

pyenv建模rbenv(用于Ruby版本切换的好工具),并且其唯一依赖项是bash。

  • pyenv与非常相似的名称无关pyvenv-它是等效的virtualenv,是最新Python 3版本的一部分,并且不处理Python版本切换

注意事项

关于pyenv的两个警告:

  1. 它只能从bash或类似贝壳的作品-或者更具体地说,pyenv-的virtualenv插件不一样dash,这是/bin/sh在Ubuntu或Debian的。
  2. 它必须从交互式登录外壳程序(例如bash --login使用终端)运行,使用诸如Ansible之类的自动化工具并不总是很容易实现

因此,pyenv最适合交互式使用,而对脚本服务器则不太好。

SSL模块问题

使用的原因之一pyenv是,在使用较早的系统提供的Python版本时,升级Python SSL模块常常会遇到问题:

  • Ubuntu 14.04 包含Python 2.7.6,这需要大量的工作才能将其升级到正确的SSL模块,因此它可以将SNI(服务器名称指示)作为客户端进行处理。(我为此编写了一些Ansible脚本,这很痛苦,在某些情况下仍然会中断。)
  • 升级的SSL模块将变得更加重要,因为Python.org网站仅在2017年和2018年才迁移到TLS 1.2

I’ve had various problems (see below) installing upgraded SSL modules, even inside a virtualenv, on top of older OS-provided Python versions, so I now use pyenv.

pyenv makes it very easy to install new Python versions and supports virtualenvs. Getting started is much easier than the recipes for virtualenv listed in other answers:

  • On Mac, type brew install pyenv and on Linux, use pyenv-installer
  • this gets you built-in virtualenv support as well as Python version switching (if required)
  • works well with Python 2 or 3, can have many versions installed at once

This works very well to insulate the “new Python” version and virtualenv from system Python. Because you can easily use a more recent Python (post 2.7.9), the SSL modules are already upgraded, and of course like any modern virtualenv setup you are insulated from the system Python modules.

A couple of nice tutorials:

The pyenv-virtualenv plugin is now built in – type pyenv commands | grep virtualenv to check. I wouldn’t use the pyenv-virtualenvwrapper plugin to start with – see how you get on with pyenv-virtualenv which is more integrated into pyenv, as this covers most of what virtualenvwrapper does.

pyenv is modelled on rbenv (a good tool for Ruby version switching) and its only dependency is bash.

  • pyenv is unrelated to the very similarly named pyvenv – that is a virtualenv equivalent that’s part of recent Python 3 versions, and doesn’t handle Python version switching

Caveats

Two warnings about pyenv:

  1. It only works from a bash or similar shell – or more specifically, the pyenv-virtualenv plugin doesn’t like dash, which is /bin/sh on Ubuntu or Debian.
  2. It must be run from an interactive login shell (e.g. bash --login using a terminal), which is not always easy to achieve with automation tools such as Ansible.

Hence pyenv is best for interactive use, and less good for scripting servers.

SSL module problems

One reason to use pyenv is that there are often problems with upgrading Python SSL modules when using older system-provided Python versions:


回答 11

  • 您可以执行此操作而无需在python本身中安装任何东西。

  • 您不需要sudo或任何特权。

  • 您无需查找virtualenvtar文件的最新版本

  • 您无需在bash脚本中编辑版本信息即可保持最新状态。

  • 你不需要curl/ wgettar安装,也不pipeasy_install

  • 这适用于2.7和3.X

将以下内容保存到/tmp/initvenv.py

将来导入print_function

import os, sys, shutil, tempfile, subprocess, tarfile, hashlib

try:
    from urllib2 import urlopen
except ImportError:
    from urllib.request import urlopen

tmp_dir = tempfile.mkdtemp(prefix='initvenv_')
try:
    # read the latest version from PyPI
    f = urlopen("https://pypi.python.org/pypi/virtualenv/")
    # retrieve the .tar.gz file
    tar_found = False
    url = None
    sha256 = None
    for line in f.read().splitlines():
        if isinstance(line, bytes):
            line = line.decode('utf-8')
        if tar_found:
            if 'sha256' in line:
                sha256 = line.split('data-clipboard-text')[1].split('"')[1]
                break
            continue
        if not tar_found and 'tar.gz">' not in line:
            continue
        tar_found = True
        for url in line.split('"'):
            if url.startswith('https'):
                break
    else:
        print('tar.gz not found')
        sys.exit(1)
    file_name = url.rsplit('/', 1)[1]
    print(file_name)
    os.chdir(tmp_dir)
    data = urlopen(url).read()
    data_sha256 = hashlib.sha256(data).hexdigest()
    if sha256 != data_sha256:
        print('sha256 not correct')
        print(sha256)
        print(data_sha256)
        sys.exit(1)
    with open(file_name, 'wb') as fp:
        fp.write(data)
    tar = tarfile.open(file_name)
    tar.extractall()
    tar.close()
    os.chdir(file_name.replace('.tar.gz', ''))
    print(subprocess.check_output([sys.executable, 'virtualenv.py'] +
                                  [sys.argv[1]]).decode('utf-8'), end='')
    if len(sys.argv) > 2:
        print(subprocess.check_output([
            os.path.join(sys.argv[1], 'bin', 'pip'), 'install', 'virtualenv'] +

            sys.argv[2:]).decode('utf-8'), end='')
except:
    raise
finally:
    shutil.rmtree(tmp_dir)  # always clean up

并用作

python_binary_to_use_in_venv /tmp/initvenv.py your_venv_name [optional packages]

例如(如果您确实需要的distribute兼容性层setuptools

python /tmp/initvenv.py venv distribute

请注意,使用较旧的python版本,这可能会给您InsecurePlatformWarnings¹。

获得virtualenv(例如名称venv)后,您可以使用virtualenv刚刚安装的虚拟环境来设置另一个virtualenv :

venv/bin/virtualenv venv2

虚拟环境包装器

我建议在一次设置后也查看一下virtualenvwrapper

% /opt/python/2.7.10/bin/python /tmp/initvenv.py venv virtualenvwrapper

和激活(可以通过您的登录脚本完成):

% source venv/bin/virtualenvwrapper.sh

您可以执行以下操作:

% mktmpenv 
New python executable in tmp-17bdc3054a46b2b/bin/python
Installing setuptools, pip, wheel...done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
(tmp-17bdc3054a46b2b)% 

¹ 我还没有找到抑制警告的方法。可以在pip和/或中解决它request,但开发人员相互指出是原因。我得到了通常不切实际的建议,将我使用的python版本升级到最新版本。我确信这会中断我的Linux Mint 17安装。幸运的是pip缓存了软件包,因此每次安装软件包时仅发出一次警告。

  • You can do this without installing anything into python itself.

  • You don’t need sudo or any privileges.

  • You don’t need to find the latest version of a virtualenv tar file

  • You don’t need to edit version info in a bash script to keep things up-to-date.

  • You don’t need curl/wget or tar installed, nor pip or easy_install

  • this works for 2.7 as well as for 3.X

Save the following to /tmp/initvenv.py:

from future import print_function

import os, sys, shutil, tempfile, subprocess, tarfile, hashlib

try:
    from urllib2 import urlopen
except ImportError:
    from urllib.request import urlopen

tmp_dir = tempfile.mkdtemp(prefix='initvenv_')
try:
    # read the latest version from PyPI
    f = urlopen("https://pypi.python.org/pypi/virtualenv/")
    # retrieve the .tar.gz file
    tar_found = False
    url = None
    sha256 = None
    for line in f.read().splitlines():
        if isinstance(line, bytes):
            line = line.decode('utf-8')
        if tar_found:
            if 'sha256' in line:
                sha256 = line.split('data-clipboard-text')[1].split('"')[1]
                break
            continue
        if not tar_found and 'tar.gz">' not in line:
            continue
        tar_found = True
        for url in line.split('"'):
            if url.startswith('https'):
                break
    else:
        print('tar.gz not found')
        sys.exit(1)
    file_name = url.rsplit('/', 1)[1]
    print(file_name)
    os.chdir(tmp_dir)
    data = urlopen(url).read()
    data_sha256 = hashlib.sha256(data).hexdigest()
    if sha256 != data_sha256:
        print('sha256 not correct')
        print(sha256)
        print(data_sha256)
        sys.exit(1)
    with open(file_name, 'wb') as fp:
        fp.write(data)
    tar = tarfile.open(file_name)
    tar.extractall()
    tar.close()
    os.chdir(file_name.replace('.tar.gz', ''))
    print(subprocess.check_output([sys.executable, 'virtualenv.py'] +
                                  [sys.argv[1]]).decode('utf-8'), end='')
    if len(sys.argv) > 2:
        print(subprocess.check_output([
            os.path.join(sys.argv[1], 'bin', 'pip'), 'install', 'virtualenv'] +

            sys.argv[2:]).decode('utf-8'), end='')
except:
    raise
finally:
    shutil.rmtree(tmp_dir)  # always clean up

and use it as

python_binary_to_use_in_venv /tmp/initvenv.py your_venv_name [optional packages]

e.g. (if you really need the distribute compatibility layer for setuptools)

python /tmp/initvenv.py venv distribute

Please note that, with older python versions, this might give you InsecurePlatformWarnings¹.

Once you have your virtualenv (name e.g. venv) you can setup another virtualenv by using the virtualenv just installed:

venv/bin/virtualenv venv2

virtualenvwrapper

I recommend taking a look at virtualenvwrapper as well, after a one time setup:

% /opt/python/2.7.10/bin/python /tmp/initvenv.py venv virtualenvwrapper

and activation (can be done from your login script):

% source venv/bin/virtualenvwrapper.sh

you can do things like:

% mktmpenv 
New python executable in tmp-17bdc3054a46b2b/bin/python
Installing setuptools, pip, wheel...done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
(tmp-17bdc3054a46b2b)% 

¹ I have not found a way to suppress the warning. It could be solved in pip and/or request, but the developers point to each other as the cause. I got the, often non-realistic, recommendation to upgrade the python version I was using to the latest version. I am sure this would break e.g my Linux Mint 17 install. Fortunately pip caches packages, so the Warning is made only once per package install.


回答 12

在Virtualenv官方网站上有很好的说明。https://pypi.python.org/pypi/virtualenv

基本上我做什么,是安装pipsudo easy_install pip,然后用于sudo pip install virtualenv再创建一个环境:virtualenv my_env(命名为你想要的),下面是我所做的:virtualenv --distribute my_env; 其安装distributepip在我的virtualenv。

再次,按照virtualenv页面上的说明进行操作。

有点麻烦,来自Ruby; P

There are good instructions on the Virtualenv official site. https://pypi.python.org/pypi/virtualenv

Basically what I did, is install pip with sudo easy_install pip, then used sudo pip install virtualenv then created an environment with: virtualenv my_env (name it what you want), following that I did: virtualenv --distribute my_env; which installed distribute and pip in my virtualenv.

Again, follow the instruction on the virtualenv page.

Kind of a hassle, coming from Ruby ;P


回答 13

这是安装virtualenvwrapper(this的更新)的一种好方法。

下载virtualenv-1.11.4(可在此处找到最新信息),将其解压缩,打开terminal

# Create a bootstrapenv and activate it:
$ cd ~
$ python <path to unzipped folder>/virtualenv.py bootstrapenv
$ source bootstrapenv/bin/activate

# Install virtualenvwrapper:
$ pip install virtualenvwrapper
$ mkdir -p ~/bootstrapenv/Envs

# append it to file `.bashrc`
$ vi ~/.bashrc
  source ~/bootstrapenv/bin/activate
  export WORKON_HOME=~/bootstrapenv/Envs
  source ~/bootstrapenv/bin/virtualenvwrapper.sh

# run it now.
$ source ~/.bashrc

这就是它,现在你可以使用mkvirtualenv env1lsvirtualenv…等

注意:您可以删除virtualenv-1.11.4,并virtualenv-1.11.4.zip从下载文件夹。

Here is a nice way to install virtualenvwrapper(update of this).

Download virtualenv-1.11.4 (you can find latest at here), Unzip it, open terminal

# Create a bootstrapenv and activate it:
$ cd ~
$ python <path to unzipped folder>/virtualenv.py bootstrapenv
$ source bootstrapenv/bin/activate

# Install virtualenvwrapper:
$ pip install virtualenvwrapper
$ mkdir -p ~/bootstrapenv/Envs

# append it to file `.bashrc`
$ vi ~/.bashrc
  source ~/bootstrapenv/bin/activate
  export WORKON_HOME=~/bootstrapenv/Envs
  source ~/bootstrapenv/bin/virtualenvwrapper.sh

# run it now.
$ source ~/.bashrc

That is it, now you can use mkvirtualenv env1, lsvirtualenv ..etc

Note: you can delete virtualenv-1.11.4 and virtualenv-1.11.4.zip from Downloads folders.


回答 14

好消息是,如果您已安装python3.4,则已经安装了pyvenv。所以就

pyvenv project_dir
source project_dir/bin/activate
python --version   
python 3.4.*

现在,在此虚拟环境中,您可以使用pip为该项目安装模块。

离开这个虚拟环境,

deactivate

The good news is if you have installed python3.4, pyvenv is already been installed. So, Just

pyvenv project_dir
source project_dir/bin/activate
python --version   
python 3.4.*

Now in this virtual env, you can use pip to install modules for this project.

Leave this virtual env , just

deactivate

安装python时在$ PATH中找不到可接受的C编译器

问题:安装python时在$ PATH中找不到可接受的C编译器

我正在尝试在共享主机上安装新的python环境。我按照这篇文章写的步骤:

mkdir ~/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -zxvf Python-2.7.1.tar.gz
cd Python-2.7.1
mkdir ~/.localpython
./configure --prefix=/home/<user>/.localpython
make
make install

进入“ ./configure –prefix = / home //。localpython”命令后,我得到以下输出:

checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux3
checking EXTRAPLATDIR... 
checking machine type as reported by uname -m... x86_64
checking for --without-gcc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home3/mikos89/Python-2.7.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.

这个问题怎么解决?我已经尝试了3个小时的解决方案,但仍然停留在一个地方。

更新

Hostgator不允许在其共享帐户上使用gcc:http : //support.hostgator.com/articles/pre-sales-questions/compatible-technologies

I’m trying to install new python environment on my shared hosting. I follow the steps written in this post:

mkdir ~/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -zxvf Python-2.7.1.tar.gz
cd Python-2.7.1
mkdir ~/.localpython
./configure --prefix=/home/<user>/.localpython
make
make install

After coming to “./configure –prefix=/home//.localpython” command I get the following output:

checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux3
checking EXTRAPLATDIR... 
checking machine type as reported by uname -m... x86_64
checking for --without-gcc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home3/mikos89/Python-2.7.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.

How can this problem be solved? I’ve been trying to find a solution for 3 hours but still stuck in one place.

UPDATE

Hostgator does not allow gcc on their shared accounts: http://support.hostgator.com/articles/pre-sales-questions/compatible-technologies


回答 0

gcc编译器不在您的中$PATH。这意味着您没有安装gcc或不在$ PATH变量中。

要安装gcc,请使用以下命令:(以root身份运行)

  • Redhat基础:

    yum groupinstall "Development Tools"
  • Debian基础:

    apt-get install build-essential

The gcc compiler is not in your $PATH. It means either you dont have gcc installed or it’s not in your $PATH variable.

To install gcc use this: (run as root)

  • Redhat base:

    yum groupinstall "Development Tools"
    
  • Debian base:

    apt-get install build-essential
    

回答 1

你需要跑步

yum install gcc

you need to run

yum install gcc

回答 2

对于Ubuntu / Debian:

# sudo apt-get install build-essential

对于RHEL / CentOS

#rpm -qa | grep gcc
# yum install gcc glibc glibc-common gd gd-devel -y

要么

 # yum groupinstall "Development tools" -y

更多细节请参考链接

for Ubuntu / Debian :

# sudo apt-get install build-essential

For RHEL/CentOS

#rpm -qa | grep gcc
# yum install gcc glibc glibc-common gd gd-devel -y

or

 # yum groupinstall "Development tools" -y

More details refer the link


回答 3

您将需要运行

sudo apt-get install build-essential

首先假设您使用的是debain / ubuntu系统

You will need to run

sudo apt-get install build-essential

first assuming you’re on a debain/ubuntu system


回答 4

由于它是共享主机,因此您需要以非root用户身份安装它。这是一个指示如何执行此步骤的Tut。 http://luiarthur.github.io/gccinstall

cd ~/src
wget http://www.netgull.com/gcc/releases/gcc-5.2.0/gcc-5.2.0.tar.gz

或等效的gcc来源,然后

tar -xvf gcc-5.2.0.tar.gz
cd gcc-5.2.0
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-5.2.0/configure --prefix=$HOME/gcc-5.2.0 --enable-languages=c,c++,fortran,go
make
make install

然后添加到.bashrc或等效文件

export PATH=~/gcc-5.2.0/bin:$PATH
export LD_LIBRARY_PATH=~/gcc-5.2.0/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=~/gcc-5.2.0/lib64:$LD_LIBRARY_PATH

You would need to install it as non root, since its shared hosting. Here is a tut that points how this step. http://luiarthur.github.io/gccinstall

cd ~/src
wget http://www.netgull.com/gcc/releases/gcc-5.2.0/gcc-5.2.0.tar.gz

or equivalent gcc source, then

tar -xvf gcc-5.2.0.tar.gz
cd gcc-5.2.0
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-5.2.0/configure --prefix=$HOME/gcc-5.2.0 --enable-languages=c,c++,fortran,go
make
make install

then add to .bashrc, or equivalent

export PATH=~/gcc-5.2.0/bin:$PATH
export LD_LIBRARY_PATH=~/gcc-5.2.0/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=~/gcc-5.2.0/lib64:$LD_LIBRARY_PATH

回答 5

如果将Alphine与docker结合使用,请执行以下操作:

apk --update add gcc make g++ zlib-dev

If you are using alphine with docker, do this:

apk --update add gcc make g++ zlib-dev

回答 6

让有权访问该服务器上的root帐户的人运行sudo apt-get install build-essential。如果您不知道谁有权访问根目录,请与支持团队联系以获取共享主机并询问他们。

编辑:如果不允许访问root用户,则永远不会使它工作。恐怕您必须更改托管服务提供商。

Get someone with access to the root account on that server to run sudo apt-get install build-essential. If you don’t know who has root access, contact the support team for your shared hosting and ask them.

Edit: If you aren’t allowed access to root, you aren’t ever going to get it working. You’ll have to change hosting provider I’m afraid.


回答 7

apt-get install gcc在Suse Linux中运行

Run apt-get install gcc in Suse Linux


回答 8

sudo apt install build-essential 是命令

但是如果得到“ 可以找到该软件包 ”的错误,请运行

  • sudo apt update 第一
  • 然后 sudo apt install build-essential

这对我有用。

sudo apt install build-essential is the command

But if you get the “the package can be found” kind of error, Run

  • sudo apt update first
  • then sudo apt install build-essential

This worked for me.


回答 9

在Arch Linux上,运行以下命令:

sudo pacman -S base-devel

On Arch Linux run the following:

sudo pacman -S base-devel


Virtualenvs中的参考损坏

问题:Virtualenvs中的参考损坏

我最近在Mac上安装了许多点文件以及其他一些应用程序(我改为使用iTerm而不是Terminal,将Sublime设置为默认文本编辑器),但是此后,尽管它们的文件夹位于.virtualenvs中,但我所有的虚拟环境都停止了工作仍然在那里,每当我尝试在其中运行任何命令时,它们都会给出以下错误:

dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /Users/[user]/.virtualenvs/modclass/bin/python
  Reason: image not found
Trace/BPT trap: 5

我已经删除了所有与dotfiles相关的文件,并将.bash_profile还原到以前的状态,但是问题仍然存在。是否有任何方法可以诊断问题或以简单的方式解决问题(例如,无需再次创建所有虚拟环境)?

I recently installed a bunch of dotfiles on my Mac along with some other applications (I changed to iTerm instead of Terminal, and Sublime as my default text editor) but ever since, all my virtual environments have stopped working, although their folders inside .virtualenvs are still there and they give the following error whenever I try to run anything in them:

dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /Users/[user]/.virtualenvs/modclass/bin/python
  Reason: image not found
Trace/BPT trap: 5

I have removed all the files related to dotfiles and have restored my .bash_profile to what it was before, but the problem persists. Is there any way to diagnose the problem or solve it in an easy way (e.g. not requiring to create all the virtualenvs all over again)?


回答 0

我在这里找到了解决问题的方法,所以所有功劳都归功于作者。

要点是,当您创建一个virtualenv时,会为安装了Homebrew的Python创建许多符号链接。

这是一个例子:

$ ls -la ~/.virtualenvs/my-virtual-env
...
lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.7/Frameworks/Python.framework/Versions/2.7/Python
...

当您使用Homebrew升级Python然后运行时brew cleanup,virtualenv中的符号链接指向不再存在的路径(因为Homebrew删除了它们)。

符号链接需要指向新安装的Python:

lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/Python

解决方案是删除virtualenv中的符号链接,然后重新创建它们:

find ~/.virtualenvs/my-virtual-env/ -type l -delete
virtualenv ~/.virtualenvs/my-virtual-env

最好在删除链接之前先检查哪些链接将被删除:

find ~/.virtualenvs/my-virtual-env/ -type l

我认为,最好只删除损坏的符号链接。您可以使用GNU执行此操作find

gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete

find如果尚未安装GNU ,可以使用Homebrew 进行安装:

brew install findutils

请注意,默认情况下,随Homebrew一起安装的GNU程序通常以字母开头g。这是为了避免遮盖findOS X附带的二进制文件。

I found the solution to the problem here, so all credit goes to the author.

The gist is that when you create a virtualenv, many symlinks are created to the Homebrew installed Python.

Here is one example:

$ ls -la ~/.virtualenvs/my-virtual-env
...
lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.7/Frameworks/Python.framework/Versions/2.7/Python
...

When you upgrade Python using Homebrew and then run brew cleanup, the symlinks in the virtualenv point to paths that no longer exist (because Homebrew deleted them).

The symlinks needs to point to the newly installed Python:

lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/Python

The solution is to remove the symlinks in the virtualenv and then recreate them:

find ~/.virtualenvs/my-virtual-env/ -type l -delete
virtualenv ~/.virtualenvs/my-virtual-env

It’s probably best to check what links will be deleted first before deleting them:

find ~/.virtualenvs/my-virtual-env/ -type l

In my opinion, it’s even better to only delete broken symlinks. You can do this using GNU find:

gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete

You can install GNU find with Homebrew if you don’t already have it:

brew install findutils

Notice that by default, GNU programs installed with Homebrew tend to be prefixed with the letter g. This is to avoid shadowing the find binary that ships with OS X.


回答 1

在尝试了几件事之后,这对我有用:

转到您的virtualenv目录(但不要运行workon):

cd ~/.virtualenv/name_of_broken_venv

现在删除这些文件:

rm -rf .Python bin/python* lib/python2.7/* include/python2.7

然后重建您的venv,运行:

virtualenv .
workon name_of_broken_venv
pip freeze

现在,您应该再次看到已安装软件包的列表。

After trying a few things, this worked for me:

go to your virtualenv directory (but don’t run workon):

cd ~/.virtualenv/name_of_broken_venv

Now delete these files:

rm -rf .Python bin/python* lib/python2.7/* include/python2.7

Then to rebuild your venv, run:

virtualenv .
workon name_of_broken_venv
pip freeze

You should now see a list of your installed packages again.


回答 2

当我从Snow Leopard更新到Mac OS X Mavericks时,就会发生这种情况。我也必须事先重新安装brew。希望您使用pip为项目运行了冻结命令。

若要解决,您必须更新虚拟环境指向的路径。

  • 使用brew安装python版本:

brew install python

  • 重新安装virtualenvwrapper。

pip install --upgrade virtualenvwrapper

  • 删除了旧的虚拟环境:

rmvirtualenv old_project

  • 创建一个新的虚拟环境:

mkvirtualenv new_project

  • 在新的虚拟环境上工作

workon new_project

  • 使用pip安装新项目的要求。

pip install -r requirements.txt

这应该使项目保持以前的状态。

This occurred when I updated to Mac OS X Mavericks from Snow Leopard. I had to re-install brew beforehand too. Hopefully you ran the freeze command for your project with pip.

To resolve, you have to update the paths that the virtual environment points to.

  • Install a version of python with brew:

brew install python

  • Re-install virtualenvwrapper.

pip install --upgrade virtualenvwrapper

  • Removed the old virtual environment:

rmvirtualenv old_project

  • Create a new virtual environment:

mkvirtualenv new_project

  • Work on new virtual environment

workon new_project

  • Use pip to install the requirements for the new project.

pip install -r requirements.txt

This should leave the project as it was before.


回答 3

@Chris Wedgwood保留更新版本的答案site-packages(保留已安装的软件包)

cd ~/.virtualenv/name_of_broken_venv


mv lib/python2.7/site-packages ./    
rm -rf .Python bin lib include
virtualenv .
rm -rf lib/python2.7/site-packages
mv ./site-packages lib/python2.7/

A update version @Chris Wedgwood‘s answer for keeping site-packages (keeping packages installed)

cd ~/.virtualenv/name_of_broken_venv


mv lib/python2.7/site-packages ./    
rm -rf .Python bin lib include
virtualenv .
rm -rf lib/python2.7/site-packages
mv ./site-packages lib/python2.7/

回答 4

看来解决此问题的正确方法是运行

 pip install --upgrade virtualenv

用Homebrew升级python之后。

对于安装类似python的任何公式,该公式应该是一个通用过程,它具有自己的包管理系统。当您安装brew install python,在安装pythonpipeasy_installvirtualenv等。因此,如果这些工具可以自我更新,那么最好先尝试这样做,然后再将“自制”视为问题的根源。

It appears the proper way to resolve this issue is to run

 pip install --upgrade virtualenv

after you have upgraded python with Homebrew.

This should be a general procedure for any formula that installs something like python, which has it’s own package management system. When you install brew install python, you install python and pip and easy_install and virtualenv and so on. So, if those tools can be self-updated, it’s best to try to do so before looking to Homebrew as the source of problems.


回答 5

如果这是由brew upgrade升级其Python 引起的,并且可以降级到以前的版本,请尝试brew switch python [previous version],例如brew switch python 3.6.5从这里。

If this was caused by a brew upgrade that upgraded its Python, and you’re ok with downgrading to the previous version, try brew switch python [previous version], eg brew switch python 3.6.5. From here.


回答 6

virtualenvwrapper指令

如已接受的答案所示,根本原因可能是自制程序更新,这意味着您的virtualenv符号链接指向断开的python路径-请在此处查看详细信息。

对于每个虚拟环境,您需要重新分配符号链接以指向正确的python路径(在Brew酒窖中)。这是使用virtualenvwrapper进行操作的方法。在这里,我正在更新一个名为“ my-example-env”的虚拟环境。

cd ~/PYTHON_ENVS
find ./my-example-env -type l -delete
mkvirtualenv my-example-env

全做完了。

virtualenvwrapper instructions

As indicated in the accepted answer, the root cause is likely a homebrew update that means your virtualenv symlinks are pointing at broken python paths – see details here.

For each virtual env, you need to reassign the symlinks to point at the correct python path (in brew cellar). Here is how to do it with virtualenvwrapper. Here I am updating a virtual env called “my-example-env”.

cd ~/PYTHON_ENVS
find ./my-example-env -type l -delete
mkvirtualenv my-example-env

All done.


回答 7

任何使用pipenv的人(并且应该使用!)都可以简单地使用以下两个命令-无需激活venv:

rm -rf `pipenv --venv` # remove the broken venv
pipenv install --dev   # reinstall the venv from pipfile 

Anyone who is using pipenv (and you should!) can simply use these two commands — without having the venv activated:

rm -rf `pipenv --venv` # remove the broken venv
pipenv install --dev   # reinstall the venv from pipfile 

回答 8

如果您破坏了python3,请尝试brew upgrade python3为我修复它。

If you’ve busted python3 just try brew upgrade python3 that fixed it for me.


回答 9

我最近遇到了这个问题。以上解决方案均不适合我。看来这实际上不是Python的问题。当我运行

aws s3 ls

时,出现以下错误:

dyld: Library not loaded: @executable_path/../.Python

这意味着库aws可执行文件指向的是不存在或已损坏,因此我aws-cli按照此链接的说明进行了卸载并重新安装,并且有效!

I recently faced this. None of the above solutions worked for me. Seems it wasn’t actually Python’s problem. When I was running

aws s3 ls

I was getting following error:

dyld: Library not loaded: @executable_path/../.Python

This means, the library aws executable is pointing towards is either doesn’t exist or is corrupted, thus I uninstalled and reinstalled aws-cli following instructions from this link and it worked!!


回答 10

我(MacOS用户)遇到的问题是,brew将Python和virtualenvs链接更新为已删除的旧版本。

我们可以通过以下方式进行检查和修复

>> ls -al ~/.virtualenvs/<your-virtual-env>/.Python
.Python -> /usr/local/Cellar/python/<old-version>/Frameworks/Python.framework/Versions/3.7/Python
>> rm ~/.virtualenvs/<your-virtual-env>/.Python
>> ln -s  /usr/local/Cellar/python/<new-version>/Frameworks/Python.framework/Versions/3.7/Python ~/.virtualenvs/<your-virtual-env>/.Python

The problem for me(a MacOS user) is that brew updated the Python and virtualenvs links to the old version which was deleted.

We can check and fix it by

>> ls -al ~/.virtualenvs/<your-virtual-env>/.Python
.Python -> /usr/local/Cellar/python/<old-version>/Frameworks/Python.framework/Versions/3.7/Python
>> rm ~/.virtualenvs/<your-virtual-env>/.Python
>> ln -s  /usr/local/Cellar/python/<new-version>/Frameworks/Python.framework/Versions/3.7/Python ~/.virtualenvs/<your-virtual-env>/.Python

回答 11

我有一个类似的问题,我只是通过使用 virtualenv .

I had a similar issue and i solved it by just rebuilding the virtual environment with virtualenv .


回答 12

使用Python 2.7.10。

一个命令即可virtualenv path-to-env完成。文件资料

$ virtualenv path-to-env
Overwriting path-to-env/lib/python2.7/orig-prefix.txt with new content
New python executable in path-to-env/bin/python2.7
Also creating executable in path-to-env/bin/python
Installing setuptools, pip, wheel...done.

Using Python 2.7.10.

A single command virtualenv path-to-env does it. documentation

$ virtualenv path-to-env
Overwriting path-to-env/lib/python2.7/orig-prefix.txt with new content
New python executable in path-to-env/bin/python2.7
Also creating executable in path-to-env/bin/python
Installing setuptools, pip, wheel...done.

回答 13

由于python的Homebrew重新安装(因此损坏的符号链接)以及我之前完成的一些“ sudo pip安装”,我的虚拟环境也损坏了。Weizhong的技巧对解决问题非常有用,而无需重新安装软件包。对于混合权限问题,我还必须执行以下操作。

须藤chown -R my_username lib / python2.7 / site-packages

I had a broken virtual env due to a Homebrew reinstall of python (thereby broken symlinks) and also a few “sudo pip install”s I had done earlier. Weizhong’s tips were very helpful in fixing the issues without having to reinstall packages. I also had to do the following for the mixed permissions problem.

sudo chown -R my_username lib/python2.7/site-packages


回答 14

Virtualenvs已损坏。有时,简单的方法是删除venv文件夹并重新创建virutalenvs。

Virtualenvs are broken. Sometimes simple way is to delete venv folders and recreate virutalenvs.


回答 15

如果您使用pipenv,只需pipenv --rm解决即可。

If you using pipenv, just doing pipenv --rm solves the problem.


回答 16

在OSX Catalina上升级brew后,我遇到了同样的问题。

在尝试了一堆东西之后,我发现以下是最佳和简便的解决方案。

首先,删除虚拟环境。(可选的)

find myvirtualenv -type l -delete

然后重新创建一个新的virtualenv

virtualenv myvirtualenv

参考:https : //www.jeremycade.com/python/osx/homebrew/2015/03/02/fixing-virtualenv-after-a-python-upgrade/

I was facing the same issue after upgrading brew on my OSX Catalina.

After trying bunch of stuffs, I find the following is the best and easy solution.

At first, delete the virtual env. (Optional)

find myvirtualenv -type l -delete

then recreate a new virtualenv

virtualenv myvirtualenv

Reference: https://www.jeremycade.com/python/osx/homebrew/2015/03/02/fixing-virtualenv-after-a-python-upgrade/


回答 17

接受的答案对我不起作用:该文件$WORKON_HOME/*/bin/python2.7不再是符号链接,而是功能全面的可执行文件:

$ file $WORKON_HOME/*/bin/python2.7
/Users/sds/.virtualenvs/.../bin/python2.7: Mach-O 64-bit executable x86_64
...

a,解决方案是完全删除所有虚拟环境并从头开始重新创建。

供参考:

deactivate
pip install --user virtualenv virtualenvwrapper
pip install --user --upgrade virtualenv virtualenvwrapper
for ve in $(lsvirtualenv -b); do
  # assume that each VE is associated with a project
  # and the project has the requirements.txt file
  project=$(cat $WORKON_HOME/$ve/.project)
  rmvirtualenv $ve
  mkvirtualenv -a $project -r requirements.txt $ve
done

The accepted answer does not work for me: the file $WORKON_HOME/*/bin/python2.7 is no longer a symlink, it is a full-fledged executable:

$ file $WORKON_HOME/*/bin/python2.7
/Users/sds/.virtualenvs/.../bin/python2.7: Mach-O 64-bit executable x86_64
...

The solution is, alas, to completely remove and re-create from scratch all the virtual environments.

For the reference:

deactivate
pip install --user virtualenv virtualenvwrapper
pip install --user --upgrade virtualenv virtualenvwrapper
for ve in $(lsvirtualenv -b); do
  # assume that each VE is associated with a project
  # and the project has the requirements.txt file
  project=$(cat $WORKON_HOME/$ve/.project)
  rmvirtualenv $ve
  mkvirtualenv -a $project -r requirements.txt $ve
done

回答 18

简单地升级python3对我有用:

brew upgrade python3

Simply upgrading python3 worked for me:

brew upgrade python3

回答 19

我尝试了前几种方法,但对我而言,它们却无济于事,这些方法正试图使毒素发挥作用。最终有效的是:

sudo pip install tox

即使已经安装了tox。输出终止于:

Successfully built filelock
Installing collected packages: py, pluggy, toml, filelock, tox
Successfully installed filelock-3.0.10 pluggy-0.11.0 py-1.8.0 toml-0.10.0 tox-3.9.0

I tried the top few methods, but they didn’t work, for me, which were trying to make tox work. What eventually worked was:

sudo pip install tox

even if tox was already installed. The output terminated with:

Successfully built filelock
Installing collected packages: py, pluggy, toml, filelock, tox
Successfully installed filelock-3.0.10 pluggy-0.11.0 py-1.8.0 toml-0.10.0 tox-3.9.0

回答 20

对我来说,解决此问题的方法只是卸载python3和pipenv,然后重新安装它们。

brew uninstall pipenv
brew uninstall python3
brew install python3 
brew install pipenv

What fixed it for me was just uninstalling python3 and pipenv then reinstalling them.

brew uninstall pipenv
brew uninstall python3
brew install python3 
brew install pipenv

回答 21

这里所有的答案都很棒,我尝试了Ryan,Chris上面提到的几种解决方案,但无法解决问题,因此必须采取快速而肮脏的方法。

  1. rm -rf <project dir>(或者mv <project dir> <backup projct dir>如果您想保留备份)
  2. git clone <project git url>
  3. 继续!

这里没有什么新奇的东西,但是它使生活更轻松!

All the answers are great here, I tried a couple of solutions mentioned above by Ryan, Chris and couldn’t resolve the issue, so had to follow a quick and dirty way.

  1. rm -rf <project dir> (or mv <project dir> <backup projct dir> if you want to keep a backup)
  2. git clone <project git url>
  3. Move on!

Nothing novel here, but it makes life easier!


回答 22

我确定我晚会晚了,但是我想说,解决这个问题比这里讨论的要简单得多。

您可以轻松地重新生成虚拟环境,而无需删除/编辑任何内容。假设您调用了损坏的环境,则env_to_fix可以执行以下操作:

mkvirtualenv env_to_fix

这将重新生成链接并修复环境,而无需将当前状态转储到某个位置并进行恢复。

I am sure I am late to the party but I want to say that the resolution of this problem is much simpler than discussed here.

You can easily regenerate the virtual environment without having to delete/edit anything. Assuming that your broken environment is called env_to_fix you can just to the following:

mkvirtualenv env_to_fix

This will regenerate the links and fix the environment without the need to dump the current status somewhere and restore it.


回答 23

当我在Mac上将python运行时从2指向3时,遇到了相同的问题,将别名python指向python 3路径。然后,我重新创建一个新的virtualenv并重新安装我的项目所需的那些软件包。对于我的用例,我有一个写给Google工作表的python程序。清理一些与python 2实现不同的程序包,然后一切又开始起作用。

I came across the same issue when I was pointing my python run time from 2 to 3 on my mac, pointing the alias python to python 3 path. I then recreate a new virtualenv and re-install those packages i need for my project. For my use case i have had a python program writing to google sheet. Clean up a few packages that are different from python 2 implementation and wa la, things started working again.


如何激活virtualenv?

问题:如何激活virtualenv?

我一直在搜索,尝试了各种替代方法都没有成功,现在花了几天时间-令我发疯。

在具有Python 2.5.2的Red Hat Linux上运行开始使用最新的Virtualenv,但无法激活它,我发现某个地方提示需要较早的版本,因此我使用Virtualenv 1.6.4,因为它应可与Python 2.6一起使用。

看来安装虚拟环境还可以

[necrailk@server6 ~]$ python virtualenv-1.6.4/virtualenv.py virtual
New python executable in virtual/bin/python
Installing setuptools............done.
Installing pip...............done.

环境看起来还可以

[necrailk@server6 ~]$ cd virtual
[necrailk@server6 ~/virtual]$ dir
bin  include  lib

试图激活

[necrailk@server6 ~/virtual]$ . bin/activate
/bin/.: Permission denied.

已检查chmod

[necrailk@server6 ~/virtual]$ cd bin
[necrailk@server6 bin]$ ls -l
total 3160
-rw-r--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
-rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
-rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
-rw-r--r-

问题,所以我改变了

[necrailk@server6 bin]$ ls -l
total 3160
-rwxr--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
-rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
-rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
-rw-r--r--    1 necrailk biz12        1005 Jan 30 11:38 activate_this.py
-rwxr-xr-x    1 necrailk biz

再试activate一次

[necrailk@server6 ~/virtual]$ . bin/activate
/bin/.: Permission denied.

仍然没有喜悦…

I have been through search and tried various alternatives without success and spent several days on it now – driving me mad.

Running on Red Hat Linux with Python 2.5.2 Began using most recent Virtualenv but could not activate it, I found somewhere suggesting needed earlier version so I have used Virtualenv 1.6.4 as that should work with Python 2.6.

It seems to install the virtual environment ok

[necrailk@server6 ~]$ python virtualenv-1.6.4/virtualenv.py virtual
New python executable in virtual/bin/python
Installing setuptools............done.
Installing pip...............done.

Environment looks ok

[necrailk@server6 ~]$ cd virtual
[necrailk@server6 ~/virtual]$ dir
bin  include  lib

Trying to activate

[necrailk@server6 ~/virtual]$ . bin/activate
/bin/.: Permission denied.

Checked chmod

[necrailk@server6 ~/virtual]$ cd bin
[necrailk@server6 bin]$ ls -l
total 3160
-rw-r--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
-rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
-rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
-rw-r--r-

Problem, so I changed it

[necrailk@server6 bin]$ ls -l
total 3160
-rwxr--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
-rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
-rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
-rw-r--r--    1 necrailk biz12        1005 Jan 30 11:38 activate_this.py
-rwxr-xr-x    1 necrailk biz

Try activate again

[necrailk@server6 ~/virtual]$ . bin/activate
/bin/.: Permission denied.

Still no joy…


回答 0

这是创建文件夹并将cd其放入后的工作流程:

$ virtualenv venv --distribute
New python executable in venv/bin/python
Installing distribute.........done.
Installing pip................done.
$ source venv/bin/activate
(venv)$ python

Here is my workflow after creating a folder and cd‘ing into it:

$ virtualenv venv --distribute
New python executable in venv/bin/python
Installing distribute.........done.
Installing pip................done.
$ source venv/bin/activate
(venv)$ python

回答 1

您忘了source bin/activate在source是可执行文件名称的地方做。同样也打了我几次,容易想到手册说的是“从环境文件夹的根目录执行此操作”。

无需activate通过执行chmod

You forgot to do source bin/activate where source is a executable name. Struck me first few times as well, easy to think that manual is telling “execute this from root of the environment folder”.

No need to make activate executable via chmod.


回答 2

你可以做

source ./python_env/bin/activate

或只是转到目录

cd /python_env/bin/

然后

source ./activate

祝好运。

You can do

source ./python_env/bin/activate

or just go to the directory

cd /python_env/bin/

and then

source ./activate

Good Luck.


回答 3

将CD复制到环境路径,然后转到bin文件夹。此时,当您使用ls命令时,应该会看到“激活”文件。

现在输入

source activate

Cd to the environment path, go to the bin folder. At this point when you use ls command, you should see the “activate” file.

now type

source activate

回答 4

转到项目目录。在我的情况下microblog是flask项目目录,在microblog目录下应该有appvenv文件夹。然后运行以下命令,这是在Ubuntu中为我工作的命令。

source venv/bin/activate

Go to the project directory. In my case microblog is the flask project directory and under microblog directory there should be app and venv folders. then run the below command, This is one worked for me in Ubuntu.

source venv/bin/activate


回答 5

问题出在/bin/.命令。从那以来,那真的很奇怪。应该始终是它所在目录的链接。(老实说,除非.是一个奇怪的别名或函数,否则我什至不知道这是不可能的。)shell没有. 内置的forsource也是有点不寻常

一种快速的解决方法是仅在另一个Shell中运行virtualenv。(一个明显的第二个优势是,deactivate您不必仅仅拥有即可exit。)

/bin/bash --rcfile bin/activate

如果您的外壳支持它,则您可能还具有非标准source命令,该命令应与做相同的事情.,但可能不存在。(总而言之,您应该尝试弄清楚为什么您的环境很奇怪,否则将来会再次让您痛苦。)

顺便说一句,您不需要chmod +x这些文件。仅当您要直接执行文件时,才需要文件是可执行文件。在这种情况下,您尝试从中启动它们.,因此他们不需要它。

The problem there is the /bin/. command. That’s really weird, since . should always be a link to the directory it’s in. (Honestly, unless . is a strange alias or function, I don’t even see how it’s possible.) It’s also a little unusual that your shell doesn’t have a . builtin for source.

One quick fix would be to just run the virtualenv in a different shell. (An obvious second advantage being that instead of having to deactivate you can just exit.)

/bin/bash --rcfile bin/activate

If your shell supports it, you may also have the nonstandard source command, which should do the same thing as ., but may not exist. (All said, you should try to figure out why your environment is strange or it will cause you pain again in the future.)

By the way, you didn’t need to chmod +x those files. Files only need to be executable if you want to execute them directly. In this case you’re trying to launch them from ., so they don’t need it.


回答 6

$ mkdir <YOURPROJECT> 创建一个新项目

$ cd <YOURPROJECT> 将目录更改为该项目

$ virtualenv <NEWVIRTUALENV> 创建新的virtualenv

$ source <NEWVIRTUALENV>/bin/activate 激活新的virtualenv

$ mkdir <YOURPROJECT> Create a new project

$ cd <YOURPROJECT> Change directory to that project

$ virtualenv <NEWVIRTUALENV> Creating new virtualenv

$ source <NEWVIRTUALENV>/bin/activate Activating that new virtualenv


回答 7

代替 ./activate

source activate

instead of ./activate

use source activate


回答 8

对于Windows,您可以执行以下操作:

创建虚拟环境的方式为:virtualenv envName –python = python.exe(如果未创建环境变量)

激活虚拟环境:> \ path \ to \ envName \ Scripts \ activate

要停用虚拟环境:> \ path \ to \ env \ Scripts \ deactivate

可以在新的python版本上正常工作。

For Windows You can perform as:

TO create the virtual env as: virtualenv envName –python=python.exe (if not create environment variable)

To activate the virtual env : > \path\to\envName\Scripts\activate

To deactivate the virtual env : > \path\to\env\Scripts\deactivate

It fine works on the new python version .


回答 9

source virtualen_name/bin/activate

source virtualen_name/bin/activate


回答 10

我也会推荐virtualenvwrapper。它为我创造了奇迹,以及我在激活方面总是遇到问题。http://virtualenvwrapper.readthedocs.org/en/latest/

I would recommend virtualenvwrapper as well. It works wonders for me and how I always have problems with activating. http://virtualenvwrapper.readthedocs.org/en/latest/


回答 11

创建名为的自己的Python虚拟环境<Your Env _name >:。我给它VE。

git clone https://github.com/pypa/virtualenv.git
python virtualenv.py VE

要激活您的新虚拟环境,请运行(注意不在./此处):

. VE/bin/activate

示例输出(注释提示已更改):

(VE)c34299@a200dblr$

设置虚拟环境后,即可删除该存储Virtualenv库。

Create your own Python virtual environment called <Your Env _name >:. I have given it VE.

git clone https://github.com/pypa/virtualenv.git
python virtualenv.py VE

To activate your new virtual environment, run (notice it’s not ./ here):

. VE/bin/activate

Sample output (note prompt changed):

(VE)c34299@a200dblr$

Once your virtual environment is set, you can remove the Virtualenv repo.


回答 12

在Mac上,将shell更改为BASH(请注意,虚拟环境仅在bash shell中有效)

[user@host tools]$. venv/bin/activate 

.: Command not found.

[user@host tools]$source venv/bin/activate

Badly placed ()'s.

[user@host tools]$bash

bash-3.2$ source venv/bin/activate

(venv) bash-3.2$ 

宾果游戏,它奏效了。看到提示更改。

在Ubuntu上:

user@local_host:~/tools$ source toolsenv/bin/activate

(toolsenv) user@local_host~/tools$ 

注意:提示更改

On Mac, change shell to BASH (keep note that virtual env works only in bash shell )

[user@host tools]$. venv/bin/activate 

.: Command not found.

[user@host tools]$source venv/bin/activate

Badly placed ()'s.

[user@host tools]$bash

bash-3.2$ source venv/bin/activate

(venv) bash-3.2$ 

Bingo , it worked. See prompt changed.

On Ubuntu:

user@local_host:~/tools$ source toolsenv/bin/activate

(toolsenv) user@local_host~/tools$ 

Note : prompt changed


回答 13

我在运行源代码/ bin / activate时遇到了麻烦,然后我意识到我将tcsh用作终端shell而不是bash。切换后,我便可以激活venv。

I had trouble getting running source /bin/activate then I realized I was using tcsh as my terminal shell instead of bash. once I switched I was able to activate venv.


回答 14

Windows 10

在Windows中,将创建以下目录:

在Windows 10中激活虚拟环境。

down\scripts\activate

\ scripts目录包含激活文件。

Linux Ubuntu

在Ubuntu中,将创建以下目录:

在Linux Ubuntu中激活虚拟环境。

source ./bin/activate

/ bin目录包含激活文件。


虚拟环境从Windows复制到Linux Ubuntu反之亦然

如果将虚拟环境文件夹从Windows复制到Linux Ubuntu,则根据目录:

source ./down/Scripts/activate

Windows 10

In Windows these directories are created :

To activate Virtual Environment in Windows 10.

down\scripts\activate

\scripts directory contain activate file.

Linux Ubuntu

In Ubuntu these directories are created :

To activate Virtual Environment in Linux Ubuntu.

source ./bin/activate

/bin directory contain activate file.


Virtual Environment copied from Windows to Linux Ubuntu vice versa

If Virtual environment folder copied from Windows to Linux Ubuntu then according to directories:

source ./down/Scripts/activate

回答 15

运行此代码,如果您在Windows计算机上,它将被激活
source venv/Scripts/activate

run this code it will get activated if you on a windows machine
source venv/Scripts/activate


回答 16

在这里发布我的答案可能有点晚了,但我仍然会发布,尽管这样做可能会使某人受益,

我曾经遇到过同样的问题,

主要原因是我以“ root”用户身份创建了virtualenv,但后来尝试使用另一个用户激活它。

chmod不能工作,因为您不是文件的所有者,因此替代方法是使用chown(更改所有权)

例如:

如果您在/ home / abc / ENV中创建了virtualenv

然后CD到/ home / abc

并运行以下命令:chown -Rv [您想要更改所有权的用户] [需要更改所有权的文件夹/文件名]

在此示例中,命令为:chown -Rv abc ENV

成功更改所有权后,您只需运行源/ENV/bin/./activate,您就应该能够正确激活virtualenv。

Probably a little late to post my answer here but still I’ll post, it might benefit someone though,

I had faced the same problem,

The main reason being that I created the virtualenv as a “root” user But later was trying to activate it using another user.

chmod won’t work as you’re not the owner of the file, hence the alternative is to use chown (to change the ownership)

For e.g. :

If you have your virtualenv created at /home/abc/ENV

Then CD to /home/abc

and run the command : chown -Rv [user-to-whom-you want-change-ownership] [folder/filename whose ownership needs to be changed]

In this example the commands would be : chown -Rv abc ENV

After the ownership is successfully changed you can simply run source /ENV/bin/./activate and your should be able to activate the virtualenv correctly.


回答 17

1-打开powershell并导航到您的应用程序文件夹2-输入您的virtualenv文件夹,例如:cd。\ venv \ Scripts \ 3-通过键入。\ activate激活virtualenv

1- open powershell and navigate to your application folder 2- enter your virtualenv folder ex : cd .\venv\Scripts\ 3- active virtualenv by type .\activate