标签归档:virtualenvwrapper

virtualenv和pyenv之间是什么关系?

问题:virtualenv和pyenv之间是什么关系?

我最近学习了如何在工作流程中使用virtualenv和virtualenvwrapper,但是我在一些指南中看到了pyenv,但是我似乎无法了解pyenv是什么以及它与virtualenv有何不同/相似。pyenv是virtualenv的更好/更新的替代品还是免费的工具?如果后者有什么不同之处,以及两者(以及适用的virtualenvwrapper)如何一起工作?

I recently learned how to use virtualenv and virtualenvwrapper in my workflow but I’ve seen pyenv mentioned in a few guides but I can’t seem to get an understanding of what pyenv is and how it is different/similar to virtualenv. Is pyenv a better/newer replacement for virtualenv or a complimentary tool? If the latter what does it do differently and how do the two (and virtualenvwrapper if applicable) work together?


回答 0

Pyenvvirtualenv是非常不同的工具,它们以不同的方式工作以执行不同的操作:

  • Pyenv是bash扩展- 不适用于Windows-会拦截您对python,pip等的调用,以将其定向到多个系统python工具链之一。因此,您始终具有在选定的python版本中安装的所有库,因此,这对于必须在不同版本的python之间进行切换的用户而言非常有用。

  • VirtualEnv是纯python,因此可在任何地方使用,它会在激活环境中本地复制python和pip 的副本,或者可选地复制特定版本,该环境可能包含也可能不包含指向当前系统工具链的链接,如果不能,则可以仅将已知的库子集安装到该环境中。这样一来,几乎可以肯定,对于测试和部署而言,要好得多,因为您确切知道使用哪个库,使用了哪个版本,并且全局更改不会影响您的模块。

venv python> 3.3

请注意,从Python 3.3开始,有一个名为venv的VirtualEnv内置实现(在某些安装中,有一个名为pyvenv的包装器- 在Python 3.6中已弃用该包装器),应该优先使用它。为避免包装程序可能出现问题,通常最好直接使用/path/to/python3 -m venv desired/env/path或使用pyWindows上的优秀python选择器来使用它py -3 -m venv desired/env/path。它将创建用desired/env/pathconfigure 指定的目录并适当地填充它。通常,这非常类似于使用VirtualEnv。

其他工具

有许多值得一提和考虑的工具,因为它们可以帮助使用上述一种或多种:

  • VirtualEnvWrapper管理和简化VirtualEnv- Cross平台的使用和管理。
  • pyenv-virtualenvpyenv-installer安装,为PyEnv工具提供了用于管理和与VirtualEnv交互的工具-通过此工具,您可以进行基本安装,包括多个版本的python,并在每个版本中创建隔离的环境-Linux / OS- XJohann Visagie建议
  • PyInstaller可以获取可能在VirtualEnv下开发和测试的python代码,并将其捆绑在一起,以便它可以运行未安装python 版本的平台-请注意,它不是交叉编译器,因此您需要Windows(虚拟) -)机器来构建Windows安装等,但是即使您可以确定将安装python但不能确定python的版本和所有库是否与您的代码兼容,它也可以派上用场。

Pyenv and virtualenv are very different tools that work in different ways to do different things:

  • Pyenv is a bash extension – will not work on Windows – that intercepts your calls to python, pip, etc., to direct them to one of several of the system python tool-chains. So you always have all the libraries that you have installed in the selected python version available – as such it is good for users who have to switch between different versions of python.

  • VirtualEnv, is pure python so works everywhere, it makes a copy of, optionally a specific version of, python and pip local to the activate environment which may or may not include links to the current system tool-chain, if it does not you can install just a known subset of libraries into that environment. As such it is almost certainly much better for testing and deployment as you know exactly which libraries, at which versions, are used and a global change will not impact your module.

venv python > 3.3

Note that from Python 3.3 onward there is a built in implementation of VirtualEnv called venv (with, on some installations a wrapper called pyvenv – this wrapper is deprecated in Python 3.6), which should probably be used in preference. To avoid possible issues with the wrapper it is often a good idea to use it directly by using /path/to/python3 -m venv desired/env/path or you can use the excellent py python selector on windows with py -3 -m venv desired/env/path. It will create the directory specified with desired/env/path configure and populate it appropriately. In general it is very much like using VirtualEnv.

Additional Tools

There are a number of tools that it is worth mentioning, and considering, as they can help with the use of one or more of the above:

  • VirtualEnvWrapper Manage and simplify the use and management of VirtualEnv – Cross Platform.
  • pyenv-virtualenv, installed by pyenv-installer, which gives PyEnv tools for managing and interfacing to VirtualEnv – with this you can have a base installation that includes more than one version of python and create isolated environments within each of them – Linux/OS-X. Suggested by Johann Visagie
  • PyInstaller can take your python code, possibly developed & tested under VirtualEnv, and bundle it up so that it can run one platforms that do not have your version of python installed – Note that it is not a cross compiler you will need a Windows (virtual-)machine to build Windows installs, etc., but it can be handy even where you can be sure that python will be installed but cannot be sure that the version of python and all the libraries will be compatible with your code.

回答 1

virtualenv允许您在项目的子目录中创建自定义Python安装。因此,您的每个项目python在其各自的virtualenv下都可以拥有自己的(甚至几个)项目。某些/所有virtualenv甚至具有相同版本python(例如2.7.16)而没有冲突是完全可以的-它们独立存在并且彼此不认识。如果要使用其中任何一个python,则必须使用activate它(通过运行一个脚本来临时修改您的脚本,PATH以确保virtualenv的bin/目录位于第一位)。从那时起,调用python(或其他方法pip)将调用该virtualenv的版本,直到您使用deactivate它(还原PATH)为止。

pyenv它的运行范围比virtualenv-拥有的Python安装寄存器(可用于安装新的),并允许您配置使用python命令时运行哪个版本的Python 。听起来很相似,但实际用法却有所不同。它的工作方式是(永久地)python在您的填充脚本之前添加PATH(永久),然后确定python要调用的“真实” 脚本。您甚至可以配置pyenv以调用您的virtualenv python之一(通过使用pyenv-virtualenv插件)。使用pyenv进行安装的Python版本进入其$(pyenv root)/versions/目录(默认情况下pyenv根目录为〜/ .pyenv),因此比virtualenv更“全局”。通常,您不能复制通过安装的Python版本pyenv,至少这样做不是主要思想。

要创建具有特定Python版本的virtualenv,您需要将该版本放置在系统中的某个位置(无论是否在该版本中PATH),并将其本质上克隆到新创建的virtualenv中。当然,获得特定版本的一种方法是通过安装它pyenv。完成此操作后,可以通过将不同的模块(或其版本)安装到各个虚拟环境中来自由进行区分。

简而言之:

  • virtualenv 允许您通过从现有安装中进行克隆来创建本地独立的python安装
  • pyenv 允许您同时安装不同版本的python(在系统范围内或仅针对本地用户),然后选择在任意给定时间运行哪些python(包括由virtualenv或Anaconda创建的)

virtualenv allows you to create a custom Python installation e.g. in a subdirectory of your project. Each of your projects can thus have their own python (or even several) under their respective virtualenv. It is perfectly fine for some/all virtualenvs to even have the same version of python (e.g. 2.7.16) without conflict – they live separately and don’t know of each other. If you want to use any of those pythons, you have to activate it (by running a script which will temporarily modify your PATH to ensure that that virtualenv’s bin/ directory comes first). From that point, calling python (or pip etc.) will invoke that virtualenv’s version until you deactivate it (which restores the PATH).

pyenv operates on a wider scale than virtualenv – it holds a register of Python installations (and can be used to install new ones) and allows you to configure which version of Python to run when you use the python command. Sounds similar but practical use is a bit different. It works by prepending its shim python script to your PATH (permanently) and then deciding which “real” python to invoke. You can even configure pyenv to call into one of your virtualenv pythons (by using the pyenv-virtualenv plugin). Python versions you install using pyenv go into its $(pyenv root)/versions/ directory (by default, pyenv root is ~/.pyenv) so are more ‘global’ than virtualenv. Ordinarily, you can’t duplicate Python versions installed through pyenv, at least doing so is not the main idea.

To create a virtualenv with a specific Python version, you need to have that version somewhere in your system (whether it’s on the PATH or not) and essentially clone it into your newly created virtualenv. Of course, one way to obtain a particular version is to install it via pyenv. Once that’s done, individual virtualenvs are free to diverge by having different modules (or versions thereof) installed into them.

In short:

  • virtualenv allows you to create local, independent python installations by cloning from existing ones
  • pyenv allows you to install different versions of python simultaneously (either system-wide or just for the local user) and then choose which of the multitude of pythons to run at any given time (including those created by virtualenv or Anaconda)

pip安装后,virtualenvwrapper.sh在哪里?

问题:pip安装后,virtualenvwrapper.sh在哪里?

我正在尝试在OSX上设置virtualenvwrapper,发现的所有说明和教程都告诉我向.profile添加源命令,指向virtualenvwrapper.sh。我已经检查了所有的python和site-packages目录,但找不到任何virtualenvwrapper.sh。这是我需要单独下载的东西吗?pip安装不正确吗?

这是/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/virtualenvwrapper的内容:

hook_loader.py      hook_loader.pyc     project.py      project.pyc     user_scripts.py     user_scripts.pyc

如您所见,没有virtualenvwrapper.sh。它在哪里?

I’m trying to setup virtualenvwrapper on OSX, and all the instructions and tutorials I’ve found tell me to add a source command to .profile, pointing towards virtualenvwrapper.sh. I’ve checked all the python and site-packages directories, and I can’t find any virtualenvwrapper.sh. Is this something I need to download separately? Is pip not installing correctly?

This is the contents of /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/virtualenvwrapper:

hook_loader.py      hook_loader.pyc     project.py      project.pyc     user_scripts.py     user_scripts.pyc

As you can see, no virtualenvwrapper.sh. Where is it?


回答 0

您可以使用以下find命令搜索文件:

find / -name virtualenvwrapper.sh

这将从根目录的所有目录中搜索该文件。


在ubuntu 12.04 LTS上,通过pip安装,它已安装到

/usr/local/bin/virtualenvwrapper.sh


在ubuntu 17.04上,以pip身份作为普通用户安装,它已安装到

~/.local/bin/virtualenvwrapper.sh

You can use the find command to search for a file:

find / -name virtualenvwrapper.sh

This will search all directories from the root for the file.


on ubuntu 12.04 LTS, installing through pip, it is installed to

/usr/local/bin/virtualenvwrapper.sh


on ubuntu 17.04, installing through pip as a normal user, it is installed to

~/.local/bin/virtualenvwrapper.sh


回答 1

您已经尝试过吗?

$ which virtualenvwrapper.sh

did you already try this ?

$ which virtualenvwrapper.sh

回答 2

我只是用pip重新安装了它。

sudo pip uninstall virtualenvwrapper
sudo pip install virtualenvwrapper

这次将其放在/ usr / local / bin中。

I just reinstalled it with pip.

sudo pip uninstall virtualenvwrapper
sudo pip install virtualenvwrapper

And this time it put it in /usr/local/bin.


回答 3

各个OS所存储/定位的virtualenvwrapper.sh 的确切路径 有所不同。即使在相同的操作系统中,它也因版本而异因此,我们需要适用于所有OS版本的通用解决方案。

我找到其路径的最简单方法是

pip uninstall virtualenvwrapper

这将提示您进行确认。说“否”,但确认的第一行显示virtualenvwrapper.sh的路径(提示提供了将删除的文件列表,如果您说是的话。此列表中的第一项包含您计算机中virtualenvwrapper.sh的路径)

The exact path where virtualenvwrapper.sh is stored/located varies from OS to OS. Even with in same OS, it varies from version to version. So we need a generic solution that works for all OS versions.

Easiest way I have found to find its path is: Do

pip uninstall virtualenvwrapper

This will prompt a confirmation. Say “No” But first line of confirmation shows the path of virtualenvwrapper.sh (Prompt gives a list of files it will delete, if you say Yes. First entry in this list contains path to virtualenvwrapper.sh in your machine)


回答 4

或者像我一样..只需卸载virtualenvwrapper

sudo pip卸载virtualenvwrapper

然后使用easy_install进行安装

sudo easy_install virtualenvwrapper

这次我发现已安装文件“ /usr/local/bin/virtualenvwrapper.sh ” …在此之前,即使通过此命令我也找不到任何位置的文件

查找/ -name virtualenvwrapper.sh

or, like I did..just uninstall virtualenvwrapper

sudo pip uninstall virtualenvwrapper

and then install it with easy_install

sudo easy_install virtualenvwrapper

this time I found the file “/usr/local/bin/virtualenvwrapper.sh” installed… Before that I weren’t finding that file anywhere even by this command

find / -name virtualenvwrapper.sh


回答 5

在Mac OS上

which virtualenvwrapper.sh

你懂得

/Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

而且你可以

sudo ln /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh /usr/local/bin/virtualenvwrapper.sh

在你的 .bash_profile

source /usr/local/bin/virtualenvwrapper.sh

或者你可以

source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

On Mac OS

which virtualenvwrapper.sh

u got

/Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

and u can

sudo ln /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh /usr/local/bin/virtualenvwrapper.sh

and in your .bash_profile

source /usr/local/bin/virtualenvwrapper.sh

or u can

source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

回答 6

在OS X 10.8.2中,使用Python 2.7:

/Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

In OS X 10.8.2, with Python 2.7:

/Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh


回答 7

在OSx EI机长中,我将virtualenvwrapper安装为

sudo pip3 install virtualenvwrapper

,但是我在中找不到virtualenvwrapper.sh /user/local/bin,它终于在找到了/Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh,您可以通过以下方式建立到/ usr / local / bin的软链接:

ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh /usr/local/bin/virtualenvwrapper.sh,您可以按照官方文档的说明按照安装指南进行操作。祝好运!

In OSx EI captain, I installed the virtualenvwrapper as

sudo pip3 install virtualenvwrapper

, however I cannot find the virtualenvwrapper.sh in /user/local/bin, it was finally found at /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh , and you can make an soft link to /usr/local/bin as

ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh /usr/local/bin/virtualenvwrapper.sh, and everything you can just follow the setup guide as the official document does. Good luck!


回答 8

对我来说是:

~/Library/Python/2.7/bin/virtualenvwrapper.sh

(对于OS X,带有pip install --user安装)

For me it was in :

~/Library/Python/2.7/bin/virtualenvwrapper.sh

(With OS X, with a pip install --user installation)


回答 9

我也有同样的问题。如果您使用的是virtualenvwrapper的旧版本,则pip无法正常工作。

http://pypi.python.org/pypi/virtualenvwrapper/3.6 和python setup.py安装文件下载src 。然后问题解决了。

I have the same problem. If you have older version of virtualenvwrapper, then pip wont work.

download src from http://pypi.python.org/pypi/virtualenvwrapper/3.6 and python setup.py install. Then the problem solved.


回答 10

对于基于RPM的发行版(如Fedora 19),运行sudo pip install virtualenvwrapper命令后,您可以在以下位置找到文件:

/usr/bin/virtualenvwrapper.sh

For RPM-based distributions(like Fedora 19), after running the sudo pip install virtualenvwrapper command, you may find the file at:

/usr/bin/virtualenvwrapper.sh

回答 11

使用普通用户在Ubuntu 15.10上使用pip安装了它,并~/.local/bin/virtualenvwrapper.sh通过运行找到了它:

$ find / -name virtualenvwrapper.sh 2>/dev/null

Installed it using pip on Ubuntu 15.10 using a normal user, it was put in ~/.local/bin/virtualenvwrapper.sh which I found by running:

$ find / -name virtualenvwrapper.sh 2>/dev/null


回答 12

使用

find / -name virtualenvwrapper.sh

我收到大量“拒绝权限”的信息,并且只有一个文件位置的打印输出。我错过了它,直到我用pip再次卸载/安装该文件时找到了该文件位置。

万一你好奇,那是在

/usr/local/share/python/virtualenvwrapper.sh

Using

find / -name virtualenvwrapper.sh

I got a TON of “permissions denied”s, and exactly one printout of the file location. I missed it until I found that file location when I uninstall/installed it again with pip.

In case you were curious, it was in

/usr/local/share/python/virtualenvwrapper.sh

回答 13

就我而言(OSX El Capitan,版本10.11.5),我需要按如下方式编辑.profile:

在终端中:

vim〜/ .profile

export WORKON_HOME=$HOME/.virtualenvs
export MSYS_HOME=C:\msys\1.0
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh                                                                                   

然后重新加载配置文件(它将在当前会话中可用。)

来源〜/ .profile

希望它会帮助某人。

In my case (OSX El Capitan, version 10.11.5) I needed to edit the .profile like so:

In the terminal:

vim ~/.profile

export WORKON_HOME=$HOME/.virtualenvs
export MSYS_HOME=C:\msys\1.0
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh                                                                                   

And then reload the profile (that it will be availuble in the current session.)

source ~/.profile

Hope it will help someone.


回答 14

我在玩virtualenvwrapper-4.8.4时可以在macOS Mojave(10.14)中找到一个

/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh

I can find one in macOS Mojave (10.14) while playing with virtualenvwrapper-4.8.4

/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh


回答 15

尽管这是OS X的问题,但这是在Linux(Red Hat)上对我有用的东西。

我的virtualwrapper.sh在

~/.local/bin/virtualenvwrapper.sh

这可能是因为我使用--user标记在本地安装了virtualenvwrapper …

pip install --user virtualenvwrapper

……作为替代冒险的做法使用sudo pip

Although this is an OS X question, here’s what worked for me on Linux (Red Hat).

My virtualwrapper.sh was in

~/.local/bin/virtualenvwrapper.sh

This is probably because I installed virtualenvwrapper locally, using the --user flag…

pip install --user virtualenvwrapper

…as an alternative to the risky practice of using sudo pip.


回答 16

/usr/share/virtualenvwrapper/virtualenvwrapper.sh

我已经在Ubuntu 16.04上安装了它,并找到了这个位置。

/usr/share/virtualenvwrapper/virtualenvwrapper.sh

I’ve installed it on Ubuntu 16.04 and it resulted in this location.


回答 17

/usr/local/bin/virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh

回答 18

小猎犬骨头黑(debian)也有同样的问题。

手动下载软件包并安装对我有用。

I had the same issue in with the beagle bone black(debian).

Manually downloading the package and installing worked for me.


回答 19

对于Ubuntu,
如果您刚刚安装了它,请检查Terminal上的输出,我在发布我的:

Running setup.py install for virtualenv-clone    
Installing virtualenv-clone script to /home/username/.local/bin
Successfully installed virtualenvwrapper virtualenv virtualenv-clone stevedore pbr six
Cleaning up...

第二行告诉您路径。对我来说/home/username/.local/bin

For Ubuntu
If you just installed it, check the output on Terminal, I’m posting mine :

Running setup.py install for virtualenv-clone    
Installing virtualenv-clone script to /home/username/.local/bin
Successfully installed virtualenvwrapper virtualenv virtualenv-clone stevedore pbr six
Cleaning up...

Here the second line tells you the path. For me it was at /home/username/.local/bin


回答 20

点子不会试图让您为难。

问题是基于命令的文件始终安装在/bin文件夹中,它们可以位于系统路径上的任何位置。

我遇到了同样的问题,发现我的文件中有这些文件

~/.local/bin/

文件夹而不是

/usr/loca/bin/

这是常见的情况,但我认为他们将默认路径更改为

~ 或$ HOME

目录,因为它对pip安装更隔离,并提供了apt-get软件包和pip软件包之间的区别。

所以说到这里,您有两种选择,要么转到 .bashrc,然后进行如下更改

# for virtualenv wrapper
export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/Devel
source $HOME/.local/bin/virtualenvwrapper.sh

然后在下面创建目录virtualenvwrapper/usr/share/然后像这样符号链接您的virtualwrapper_lazy.sh

sudo ln -s ~/.local/bin/virtualenvwrapper_lazy.sh /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh

并且您可以检查您的workon命令是否正在运行,该命令将列出您现有的virtualenv。

pip will not try to make things difficult for you on purpose.

The thing is commands based files are always installed in /bin folders they can be anywhere on the system path.

I had the same problem and I found that I have these files in my

~/.local/bin/

folder instead of

/usr/loca/bin/

which is the common case, but I think they changed the default path to

~ or $HOME

directory because its more isolate for the pip installations and provides a distinction between apt-get packages and pip packages.

So coming to the point you have two choices here either you go to your .bashrc and make changes like this

# for virtualenv wrapper
export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/Devel
source $HOME/.local/bin/virtualenvwrapper.sh

and than create a directory virtualenvwrapper under /usr/share/ and than symlink your virtualwrapper_lazy.sh like this

sudo ln -s ~/.local/bin/virtualenvwrapper_lazy.sh /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh

and you can check if your workon command is working which will list your existing virtualenv’s.


回答 21

如果您pip install virtualenvwrapper不使用sudo来执行,那么普通的用户pip将运行,但由于缺少权限而不会在所需位置复制文件

mortiz@florida:~# sudo pip3 install virtualenvwrapper

使用sudo,文件将在它们各自的路径下创建:

root@florida:/usr/local/bin# ls -ltr
total 8008
-rwxr-xr-x 1 root staff 8136192 Jun 11 17:45 chromedriver
-rwxr-xr-x 1 root staff   41697 Sep  5 16:06 virtualenvwrapper.sh
-rwxr-xr-x 1 root staff    2210 Sep  5 16:06 virtualenvwrapper_lazy.sh
-rwxr-xr-x 1 root staff     215 Sep  5 16:06 pbr
-rwxr-xr-x 1 root staff     218 Sep  5 16:06 virtualenv-clone
-rwxr-xr-x 1 root staff     213 Sep  5 16:06 virtualenv
root@florida:/usr/local/bin# 

在Debian GNU / Linux 9上为我工作

If you execute pip install virtualenvwrapper without sudo as a normal user pip will run but won’t copy the files in the required locations because the lack of permissions.

mortiz@florida:~# sudo pip3 install virtualenvwrapper

Use sudo and the files will be created under their respective paths:

root@florida:/usr/local/bin# ls -ltr
total 8008
-rwxr-xr-x 1 root staff 8136192 Jun 11 17:45 chromedriver
-rwxr-xr-x 1 root staff   41697 Sep  5 16:06 virtualenvwrapper.sh
-rwxr-xr-x 1 root staff    2210 Sep  5 16:06 virtualenvwrapper_lazy.sh
-rwxr-xr-x 1 root staff     215 Sep  5 16:06 pbr
-rwxr-xr-x 1 root staff     218 Sep  5 16:06 virtualenv-clone
-rwxr-xr-x 1 root staff     213 Sep  5 16:06 virtualenv
root@florida:/usr/local/bin# 

Worked for me on Debian GNU/Linux 9


回答 22

就我而言:/home/username/.local/bin/virtualenvwrapper.sh

in my case: /home/username/.local/bin/virtualenvwrapper.sh


回答 23

您是否使用sudo安装了它?在我的情况下是错误。

Have you installed it using sudo? Was the error in my case.


virtualenvwrapper和Python 3

问题:virtualenvwrapper和Python 3

我在ubuntu lucid上安装了python 3.3.1并成功创建了virtualenv,如下所示

virtualenv envpy331 --python=/usr/local/bin/python3.3

envpy331在我的主目录上创建了一个文件夹。

我也已经virtualenvwrapper安装了。但是在文档中仅支持的2.4-2.7版本。python是否有人试图组织python3virtualenv?如果是这样,您能告诉我如何吗?

I installed python 3.3.1 on ubuntu lucid and successfully created a virtualenv as below

virtualenv envpy331 --python=/usr/local/bin/python3.3

this created a folder envpy331 on my home dir.

I also have virtualenvwrapper installed.But in the docs only 2.4-2.7 versions of python are supported..Has anyone tried to organize the python3 virtualenv ? If so, can you tell me how ?


回答 0

virtualenvwrapper的最新版本是Python3.2下进行测试。很有可能它也可以与Python3.3一起使用。

The latest version of virtualenvwrapper is tested under Python3.2. Chances are good it will work with Python3.3 too.


回答 1

如果您已经安装了python3以及virtualenvwrapper,那么在虚拟环境中使用python3的唯一操作就是使用以下命令创建环境:

which python3 #Output: /usr/bin/python3
mkvirtualenv --python=/usr/bin/python3 nameOfEnvironment

或者,(至少在使用brew的OSX上):

mkvirtualenv --python=`which python3` nameOfEnvironment

开始使用环境,您将看到在键入python后立即开始使用python3

If you already have python3 installed as well virtualenvwrapper the only thing you would need to do to use python3 with the virtual environment is creating an environment using:

which python3 #Output: /usr/bin/python3
mkvirtualenv --python=/usr/bin/python3 nameOfEnvironment

Or, (at least on OSX using brew):

mkvirtualenv --python=`which python3` nameOfEnvironment

Start using the environment and you’ll see that as soon as you type python you’ll start using python3


回答 2

您可以使virtualenvwrapper使用自定义的Python二进制文件,而不是运行一个virtualenvwrapper。为此,您需要使用virtualenv使用的VIRTUALENV_PYTHON变量:

$ export VIRTUALENV_PYTHON=/usr/bin/python3
$ mkvirtualenv -a myproject myenv
Running virtualenv with interpreter /usr/bin/python3
New python executable in myenv/bin/python3
Also creating executable in myenv/bin/python
(myenv)$ python
Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

You can make virtualenvwrapper use a custom Python binary instead of the one virtualenvwrapper is run with. To do that you need to use VIRTUALENV_PYTHON variable which is utilized by virtualenv:

$ export VIRTUALENV_PYTHON=/usr/bin/python3
$ mkvirtualenv -a myproject myenv
Running virtualenv with interpreter /usr/bin/python3
New python executable in myenv/bin/python3
Also creating executable in myenv/bin/python
(myenv)$ python
Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

回答 3

virtualenvwrapper现在允许您指定不带路径的python可执行文件。

因此(至少在OSX上)mkvirtualenv --python=python3 nameOfEnvironment就足够了。

virtualenvwrapper now lets you specify the python executable without the path.

So (on OSX at least)mkvirtualenv --python=python3 nameOfEnvironment will suffice.


回答 4

在Ubuntu上;使用使用mkvirtualenv -p python3 env_namepython3加载virtualenv。

在环境内部,用于python --version验证。

On Ubuntu; using mkvirtualenv -p python3 env_name loads the virtualenv with python3.

Inside the env, use python --version to verify.


回答 5

您可以将其添加到您的.bash_profile或类似文件中:

alias mkvirtualenv3='mkvirtualenv --python=`which python3`'

然后在要创建python 3环境时使用mkvirtualenv3代替mkvirtualenv

You can add this to your .bash_profile or similar:

alias mkvirtualenv3='mkvirtualenv --python=`which python3`'

Then use mkvirtualenv3 instead of mkvirtualenv when you want to create a python 3 environment.


回答 6

我发现跑步

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv-3.4

在Ubuntu上的命令行中,强制mkvirtualenv使用python3和virtualenv-3.4。仍然要做

mkvirtualenv --python=/usr/bin/python3 nameOfEnvironment

创造环境。假设您在/ usr / bin / python3中有python3,在/usr/local/bin/virtualenv-3.4中有virtualenv-3.4。

I find that running

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

and

export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv-3.4

in the command line on Ubuntu forces mkvirtualenv to use python3 and virtualenv-3.4. One still has to do

mkvirtualenv --python=/usr/bin/python3 nameOfEnvironment

to create the environment. This is assuming that you have python3 in /usr/bin/python3 and virtualenv-3.4 in /usr/local/bin/virtualenv-3.4.


回答 7

关于virtualenvwrapper的bitbucket问题跟踪器的这篇文章可能很有趣。在那里提到,大多数virtualenvwrapper的功能都可以在Python 3.3中的venv虚拟环境中使用。

This post on the bitbucket issue tracker of virtualenvwrapper may be of interest. It is mentioned there that most of virtualenvwrapper’s functions work with the venv virtual environments in Python 3.3.


回答 8

我这样添加export VIRTUALENV_PYTHON=/usr/bin/python3到我的~/.bashrc

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

然后跑 source .bashrc

您可以为每个新环境指定python版本 mkvirtualenv --python=python2 env_name

I added export VIRTUALENV_PYTHON=/usr/bin/python3 to my ~/.bashrc like this:

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

then run source .bashrc

and you can specify the python version for each new env mkvirtualenv --python=python2 env_name


使用virtualenvwrapper重命名环境

问题:使用virtualenvwrapper重命名环境

我有一个名为的环境doors,我想将其重命名djangovirtualenvwrapper

我注意到,如果仅将文件夹重命名~/.virtualenvs/doorsdjango,我现在可以呼叫workon django,但是环境仍然提示(doors)hobbes3@hobbes3

I have an environment called doors and I would like to rename it to django for the virtualenvwrapper.

I’ve noticed that if I just rename the folder ~/.virtualenvs/doors to django, I can now call workon django, but the environment still says (doors)hobbes3@hobbes3.


回答 0

您可以使用:

cpvirtualenv oldenv newenv
rmvirtualenv oldenv

因此,在您的情况下:

cpvirtualenv doors django
rmvirtualenv doors

You can use:

cpvirtualenv oldenv newenv
rmvirtualenv oldenv

So in your case:

cpvirtualenv doors django
rmvirtualenv doors

回答 1

如果您这样做:

$ ack-grep -ai doors ~/.virtualenvs/django/bin

您会注意到,该文件将doors作为位置而不是django,您将使用新位置更改每个文件。

解决方案:重命名文件夹后,执行以下命令。

$ sed -i "s/doors/django/g" ~/.virtualenvs/django/bin/*

现在,如果您这样做:

$ workon django
(django)hobbes3@hobbes3

if you do:

$ ack-grep -ai doors ~/.virtualenvs/django/bin

you’ll notice that will have doors as location and not django, you’ll to change each file with the new location.

solution: after renamed the folder execute the command below.

$ sed -i "s/doors/django/g" ~/.virtualenvs/django/bin/*

now if you do:

$ workon django
(django)hobbes3@hobbes3

应该在哪里创建virtualenvs?

问题:应该在哪里创建virtualenvs?

我对应该在哪里放置我的虚拟环境感到困惑。

在我的第一个django项目中,我使用以下命令创建了该项目

django-admin.py startproject djangoproject

然后,我进入djangoproject目录并运行命令

virtualenv env

在与内部djangoproject目录相同级别上创建了虚拟环境目录。

这是为特定项目创建virtualenv的错误位置吗?

我给人的印象是,大多数人将所有virtualenv放在一个完全不同的目录中,例如~/virtualenvs,然后使用virtualenvwrapper在它们之间来回切换。

有正确的方法吗?

I’m confused as to where I should put my virtualenvs.

With my first django project, I created the project with the command

django-admin.py startproject djangoproject

I then cd’d into the djangoproject directory and ran the command

virtualenv env

which created the virtual environment directory at the same level as the inner djangoproject directory.

Is this the wrong place in which to create the virtualenv for this particular project?

I’m getting the impression that most people keep all their virtualenvs together in an entirely different directory, e.g. ~/virtualenvs, and then use virtualenvwrapper to switch back and forth between them.

Is there a correct way to do this?


回答 0

许多人使用virtualenvwrapper工具,该工具将所有virtualenvs保留在同一位置(~/.virtualenvs目录),并允许在其中创建和保存它们的快捷方式。例如,您可以这样做:

mkvirtualenv djangoproject

然后再:

workon djangoproject

将virtualenv目录保留在项目本身中可能是一个坏主意,因为您不想分发它(它可能特定于您的计算机或操作系统)。而是使用pip保留一个requirements.txt文件:

pip freeze > requirements.txt

并分发。这将允许使用您的项目的其他人使用以下命令将所有相同的要求重新安装到他们的virtualenv中:

pip install -r requirements.txt

Many people use the virtualenvwrapper tool, which keeps all virtualenvs in the same place (the ~/.virtualenvs directory) and allows shortcuts for creating and keeping them there. For example, you might do:

mkvirtualenv djangoproject

and then later:

workon djangoproject

It’s probably a bad idea to keep the virtualenv directory in the project itself, since you don’t want to distribute it (it might be specific to your computer or operating system). Instead, keep a requirements.txt file using pip:

pip freeze > requirements.txt

and distribute that. This will allow others using your project to reinstall all the same requirements into their virtualenv with:

pip install -r requirements.txt

回答 1

更改virtualenv目录的位置会破坏它

这是把仓库树,例如目录以外的下一个优势~/.virtualenvsvirutalenvwrapper

否则,如果将其保留在项目树中,则移动项目位置将破坏virtualenv。

请参阅:重命名virtualenv文件夹而不破坏它

有,--relocatable但是众所周知它并不完美。

另一个次要优势:您不必这样做.gitignore

将其忽略到项目树本身的好处是:

  • 使相关的东西保持在一起。
  • 您可能永远不会在项目之间重用给定的virtualenv,因此将其放置在其他位置不会带来太多优势

Changing the location of the virtualenv directory breaks it

This is one advantage of putting the directory outside of the repository tree, e.g. under ~/.virtualenvs with virutalenvwrapper.

Otherwise, if you keep it in the project tree, moving the project location will break the virtualenv.

See: Renaming a virtualenv folder without breaking it

There is --relocatable but it is known to not be perfect.

Another minor advantage: you don’t have to .gitignore it.

The advantages of putting it gitignored in the project tree itself are:

  • keeps related stuff close together.
  • you will likely never reuse a given virtualenv across projects, so putting it somewhere else does not give much advantage

回答 2

通常放置它们的位置与virtualenvwrapper的默认安装放置它们的位置相同: ~/.virtualenvs

相关:virtualenvwrapper是一个出色的工具,可为常用的virtualenv命令提供简写。http://www.doughellmann.com/projects/virtualenvwrapper/

The generally accepted place to put them is the same place that the default installation of virtualenvwrapper puts them: ~/.virtualenvs

Related: virtualenvwrapper is an excellent tool that provides shorthands for the common virtualenv commands. http://www.doughellmann.com/projects/virtualenvwrapper/


回答 3

如果使用pyenv install Python,则pyenv-virtualenv将是最佳做法。如果设置了.python-version文件,则可以在更改工作文件夹时自动激活或停用虚拟环境。Pyenv-virtualenv还将所有虚拟环境放入$HOME/.pyenv/versions文件夹中。

If you use pyenv install Python, then pyenv-virtualenv will be a best practice. If set .python-version file, it can auto activate or deactivate virtual env when you change work folder. Pyenv-virtualenv also put all virtual env into $HOME/.pyenv/versions folder.


回答 4

根据我的个人经验,我建议将所有虚拟环境组织在一个目录中。除非有人具有非常敏锐的内存并且可以记住分散在整个文件系统中的文件/文件夹。不太喜欢仅使用其他工具来管理虚拟环境。在VSCode中,如果我的configure(python.venvPath)目录包含所有虚拟环境,则它可以自动识别所有虚拟环境。

From my personal experience, I would recommend to organize all virtual environments in one single directory. Unless someone has extremely sharp memory and can remember files/folders scattered across file system. Not a big fan of using other tools just to mange virtual environments. In VSCode if I configure(python.venvPath) directory containing all virtual environments, it can automatically recognize all of them.


bash:mkvirtualenv:找不到命令

问题:bash:mkvirtualenv:找不到命令

按照Doug Hellman的virtualenvwrapper帖子中的说明进行操作,我仍然无法启动测试环境。

[mpenning@tsunami ~]$ mkvirtualenv test
-bash: mkvirtualenv: command not found
[mpenning@tsunami ~]$

请注意,我使用WORKON_HOME的不在我的$HOME。我尝试/usr/local/bin/virtualenvwrapper.shvirtualenvwrapper安装文档中所示进行查找,但是它不存在。

如果这很重要,我正在运行CentOS 6和python 2.6.6。


# File: ~/.bash_profile
# ...

export WORKON_HOME="/opt/virtual_env/"
source "/opt/virtual_env/bin/virtualenvwrapper_bashrc"

After following the instructions on Doug Hellman’s virtualenvwrapper post, I still could not fire up a test environment.

[mpenning@tsunami ~]$ mkvirtualenv test
-bash: mkvirtualenv: command not found
[mpenning@tsunami ~]$

It should be noted that I’m using WORKON_HOME that is not in my $HOME. I tried looking for /usr/local/bin/virtualenvwrapper.sh as shown in the virtualenvwrapper installation docs, but it does not exist.

I’m running CentOS 6 and python 2.6.6, if this matters.


# File: ~/.bash_profile
# ...

export WORKON_HOME="/opt/virtual_env/"
source "/opt/virtual_env/bin/virtualenvwrapper_bashrc"

回答 0

解决方案1

由于某种原因,请virtualenvwrapper.sh安装在中/usr/bin/virtualenvwrapper.sh,而不是在下/usr/local/bin

.bash_profile作品中的以下内容…

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"

我的安装看起来不错,无需采购 virtualenvwrapper_bashrc

解决方案2

或者,如下所述,您可以利用virtualenvwrapper.sh外壳中已经存在的机会,PATH然后发出一个source `which virtualenvwrapper.sh`

Solution 1:

For some reason, virtualenvwrapper.sh installed in /usr/bin/virtualenvwrapper.sh, instead of under /usr/local/bin.

The following in my .bash_profile works…

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"

My install seems to work fine without sourcing virtualenvwrapper_bashrc

Solution 2:

Alternatively as mentioned below, you could leverage the chance that virtualenvwrapper.sh is already in your shell’s PATH and just issue a source `which virtualenvwrapper.sh`


回答 1

尝试:

source `which virtualenvwrapper.sh`

反引号是命令替换-它们将程序打印出的所有内容放入表达式中。在这种情况下,“哪个”检查$ PATH以找到virtualenvwrapper.sh并输出路径。然后,shell通过“源”读取脚本。

如果您希望每次重新启动外壳程序时都会发生这种情况,最好先从“哪个”命令中获取输出,然后将“源代码”行放入外壳程序中,如下所示:

echo "source /path/to/virtualenvwrapper.sh" >> ~/.profile

^这可能因您的外壳而略有不同。另外,请注意不要使用单个>,因为这会截断〜/ .profile:-o

Try:

source `which virtualenvwrapper.sh`

The backticks are command substitution – they take whatever the program prints out and put it in the expression. In this case “which” checks the $PATH to find virtualenvwrapper.sh and outputs the path to it. The script is then read by the shell via ‘source’.

If you want this to happen every time you restart your shell, it’s probably better to grab the output from the “which” command first, and then put the “source” line in your shell, something like this:

echo "source /path/to/virtualenvwrapper.sh" >> ~/.profile

^ This may differ slightly based on your shell. Also, be careful not to use the a single > as this will truncate your ~/.profile :-o


回答 2

我在OS X 10.9.1和python 2.7.5上遇到了相同的问题。没有问题WORKON_HOME的我,但我确实有手动添加source "/usr/local/bin/virtualenvwrapper.sh"~/.bash_profile(或~/.bashrc后我跑UNIX)pip install virtualenvwrapper

I had the same issue on OS X 10.9.1 with python 2.7.5. No issues with WORKON_HOME for me, but I did have to manually add source "/usr/local/bin/virtualenvwrapper.sh" to ~/.bash_profile (or ~/.bashrc in unix) after I ran pip install virtualenvwrapper


回答 3

执行此命令的先决条件-

  1. PIP(递归缩写,P IP nstalls P ackages)是用于安装和管理Python编写的软件包,软件包管理系统。在Python软件包索引(PyPI)中可以找到许多软件包。

    须藤apt-get install python-pip

  2. 安装虚拟环境。用于创建虚拟环境,安装彼此隔离的多个项目的软件包和依赖项。

    sudo pip安装virtualenv

  3. 安装虚拟环境包装器关于虚拟环境包装

    sudo pip安装virtualenvwrapper

安装必备组件后,您需要使虚拟环境包装器生效以创建虚拟环境。以下是步骤-

  1. 在路径变量中设置虚拟环境目录 export WORKON_HOME=(directory you need to save envs)

  2. source /usr/local/bin/virtualenvwrapper.sh -p $WORKON_HOME

如@Mike所提到的,来源`哪个virtualenvwrapper.sh`或which virtualenvwrapper.sh可用于定位virtualenvwrapper.sh文件。

最好在〜/ .bashrc中放置两行,以免每次打开新的Shell时都执行上述命令。这就是使用mkvirtualenv创建环境所需的全部

注意事项-

  • 在Ubuntu下,您可能需要以root用户身份安装virtualenv和virtualenvwrapper。只需在上面的命令前加上sudo前缀即可。
  • 根据用于安装virtualenv的过程,virtualenvwrapper.sh的路径可能会有所不同。通过运行$ find / usr -name virtualenvwrapper.sh查找合适的路径。相应地调整.bash_profile或.bashrc脚本中的行。

Prerequisites to execute this command –

  1. pip (recursive acronym of Pip Installs Packages) is a package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI).

    sudo apt-get install python-pip

  2. Install Virtual Environment. Used to create virtual environment, to install packages and dependencies of multiple projects isolated from each other.

    sudo pip install virtualenv

  3. Install virtual environment wrapper About virtual env wrapper

    sudo pip install virtualenvwrapper

After Installing prerequisites you need to bring virtual environment wrapper into action to create virtual environment. Following are the steps –

  1. set virtual environment directory in path variable- export WORKON_HOME=(directory you need to save envs)

  2. source /usr/local/bin/virtualenvwrapper.sh -p $WORKON_HOME

As mentioned by @Mike, source `which virtualenvwrapper.sh` or which virtualenvwrapper.sh can used to locate virtualenvwrapper.sh file.

It’s best to put above two lines in ~/.bashrc to avoid executing the above commands every time you open new shell. That’s all you need to create environment using mkvirtualenv

Points to keep in mind –

  • Under Ubuntu, you may need install virtualenv and virtualenvwrapper as root. Simply prefix the command above with sudo.
  • Depending on the process used to install virtualenv, the path to virtualenvwrapper.sh may vary. Find the appropriate path by running $ find /usr -name virtualenvwrapper.sh. Adjust the line in your .bash_profile or .bashrc script accordingly.

回答 4

使用此过程在ubuntu中创建虚拟环境

第1步

安装点子

   sudo apt-get install python-pip

第2步

安装virtualenv

   sudo pip install virtualenv

第三步

创建一个目录来存储您的virtualenvs(我使用〜/ .virtualenvs)

   mkdir ~/.virtualenvs

或使用此命令在env中安装特定版本的python

virtualenv -p /usr/bin/python3.6 venv

第4步

   sudo pip install virtualenvwrapper

第5步

   sudo nano ~/.bashrc

步骤6

在bashrc文件的末尾添加这两行代码

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

步骤7

打开新终端(推荐)

步骤8

创建一个新的virtualenv

  mkvirtualenv myawesomeproject

步骤9

要在virtualenvs之间加载或切换,请使用workon命令:

  workon myawesomeproject

步骤10

要退出新的virtualenv,请使用

 deactivate

并确保使用pip vs pip3

或按照以下步骤使用python3安装虚拟环境

安装环境

python3 -m venv my-project-env

并使用以下命令激活您的虚拟环境:

source my-project-env/bin/activate

Use this procedure to create virtual env in ubuntu

step 1

Install pip

   sudo apt-get install python-pip

step 2

Install virtualenv

   sudo pip install virtualenv

step 3

Create a dir to store your virtualenvs (I use ~/.virtualenvs)

   mkdir ~/.virtualenvs

or use this command to install specific version of python in env

virtualenv -p /usr/bin/python3.6 venv

step 4

   sudo pip install virtualenvwrapper

step 5

   sudo nano ~/.bashrc

step 6

Add this two line code at the end of the bashrc file

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

step 7

Open new terminal (recommended)

step 8

Create a new virtualenv

  mkvirtualenv myawesomeproject

step 9

To load or switch between virtualenvs, use the workon command:

  workon myawesomeproject

step 10

To exit your new virtualenv, use

 deactivate

and make sure using pip vs pip3

OR follow the steps below to install virtual environment using python3

Install env

python3 -m venv my-project-env

and activate your virtual environment using the following command:

source my-project-env/bin/activate

回答 5

由于我刚经历了一次阻力,所以我将尝试写两个小时前希望得到的答案。这适用于不只是想要复制粘贴解决方案的人

第一:您是否想知道为什么复制和粘贴路径对某些人有用,而对其他人却无效?**主要原因是,解决方案不同是因为Python版本2.x或3.x不同。实际上,存在与python 2或3一起工作的virtualenv和virtualenvwrapper的不同版本。如果您使用的是python 2,请像这样安装:

sudo pip install virutalenv
sudo pip install virtualenvwrapper

如果您打算使用python 3,请安装相关的python 3版本

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

您已经成功安装了适用于python版本的软件包,并且已经全部安装好吗?好吧,尝试一下。输入workon到你的终端。您的终端将无法找到命令(workon是virtualenvwrapper的命令)。当然不会。Workon是可执行文件,只有在您加载/提供文件后才能使用virtualenvwrapper.sh。但是正式安装指南已经涵盖了这一点,对吧?在文档中说,只需打开.bash_profile并插入以下内容:

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

尤其是,该命令source /usr/local/bin/virtualenvwrapper.sh似乎很有帮助,因为该命令似乎可以加载/提供所需的文件virtualenvwrapper.sh,该文件包含您要使用的所有命令,如like workonmkvirtualenv但是,是的。按照官方安装指南进行操作时,您很可能会从初始帖子中收到错误消息:mkvirtualenv: command not found。仍然找不到命令,您仍然感到沮丧。那么,这里出了什么问题?问题在于,如果您正在寻找它,则不是virtualenvwrapper.sh。简短提醒…您在这里看:

source /usr/local/bin/virtualenvwrapper.sh

但是,找到所需文件的方法非常简单。只需输入

which virtualenvwrapper

到您的终端。这将在您的PATH中搜索该文件,因为该文件很可能位于系统PATH所包含的某个文件夹中。

如果您的系统非常陌生,则所需的文件将隐藏在PATH文件夹之外。在这种情况下,您可以virtalenvwrapper.sh使用shell命令找到路径find / -name virtualenvwrapper.sh

您的结果可能看起来像这样:/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh 恭喜。You have found your missing file!。现在,您要做的就是更改.bash_profile中的一个命令。只是改变:

source "/usr/local/bin/virtualenvwrapper.sh"

至:

"/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh"

恭喜你 Virtualenvwrapper现在可以在您的系统上运行。但是您可以做一件事来增强您的解决方案。如果virtualenvwrapper.sh使用命令找到了文件,则which virtualenvwrapper.sh知道该文件位于PATH文件夹中。因此,如果只写文件名,则文件系统将假定该文件位于PATH文件夹内。因此,您不必写出完整的路径。只需输入:

source "virtualenvwrapper.sh"

而已。您不再沮丧。您已经解决了问题。希望。

Since I just went though a drag, I’ll try to write the answer I’d have wished for two hours ago. This is for people who don’t just want the copy&paste solution

First: Do you wonder why copying and pasting paths works for some people while it doesn’t work for others?** The main reason, solutions differ are different python versions, 2.x or 3.x. There are actually distinct versions of virtualenv and virtualenvwrapper that work with either python 2 or 3. If you are on python 2 install like so:

sudo pip install virutalenv
sudo pip install virtualenvwrapper

If you are planning to use python 3 install the related python 3 versions

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

You’ve successfully installed the packages for your python version and are all set, right? Well, try it. Type workon into your terminal. Your terminal will not be able to find the command (workon is a command of virtualenvwrapper). Of course it won’t. Workon is an executable that will only be available to you once you load/source the file virtualenvwrapper.sh. But the official installation guide has you covered on this one, right?. Just open your .bash_profile and insert the following, it says in the documentation:

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

Especially the command source /usr/local/bin/virtualenvwrapper.sh seems helpful since the command seems to load/source the desired file virtualenvwrapper.sh that contains all the commands you want to work with like workon and mkvirtualenv. But yeah, no. When following the official installation guide, you are very likely to receive the error from the initial post: mkvirtualenv: command not found. Still no command is being found and you are still frustrated. So whats the problem here? The problem is that virtualenvwrapper.sh is not were you are looking for it right now. Short reminder … you are looking here:

source /usr/local/bin/virtualenvwrapper.sh

But there is a pretty straight forward way to finding the desired file. Just type

which virtualenvwrapper

to your terminal. This will search your PATH for the file, since it is very likely to be in some folder that is included in the PATH of your system.

If your system is very exotic, the desired file will hide outside of a PATH folder. In that case you can find the path to virtalenvwrapper.sh with the shell command find / -name virtualenvwrapper.sh

Your result may look something like this: /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh Congratulations. You have found your missing file!. Now all you have to do is changing one command in your .bash_profile. Just change:

source "/usr/local/bin/virtualenvwrapper.sh"

to:

"/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh"

Congratulations. Virtualenvwrapper does now work on your system. But you can do one more thing to enhance your solution. If you’ve found the file virtualenvwrapper.sh with the command which virtualenvwrapper.sh you know that it is inside of a folder of the PATH. So if you just write the filename, your file system will assume the file is inside of a PATH folder. So you you don’t have to write out the full path. Just type:

source "virtualenvwrapper.sh"

Thats it. You are no longer frustrated. You have solved your problem. Hopefully.


回答 6

为了virtualenvwrapper在Ubuntu 18.04.3上成功安装,您需要执行以下操作:

  1. 安装 virtualenv

    sudo apt install virtualenv
  2. 安装 virtualenvwrapper

    sudo pip install virtualenv
    sudo pip install virtualenvwrapper
  3. 将以下内容添加到.bashrc文件末尾

    export WORKON_HOME=~/virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    source ~/.local/bin/virtualenvwrapper.sh
  4. 执行.bashrc文件

    source ~/.bashrc
  5. 创建您的virtualenv

    mkvirtualenv your_virtualenv

In order to successfully install the virtualenvwrapper on Ubuntu 18.04.3 you need to do the following:

  1. Install virtualenv

    sudo apt install virtualenv
    
  2. Install virtualenvwrapper

    sudo pip install virtualenv
    sudo pip install virtualenvwrapper
    
  3. Add the following to the end of the .bashrc file

    export WORKON_HOME=~/virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    source ~/.local/bin/virtualenvwrapper.sh
    
  4. Execute the .bashrc file

    source ~/.bashrc
    
  5. Create your virtualenv

    mkvirtualenv your_virtualenv
    

回答 7

在Windows 7和Git Bash上,这可以帮助我:

  1. 创建一个〜/ .bashrc文件(在用户主文件夹下)
  2. 添加行导出WORKON_HOME = $ HOME / .virtualenvs(如果此文件夹不存在,则必须创建此文件夹)
  3. 添加行源“ C:\ Program Files(x86)\ Python36-32 \ Scripts \ virtualenvwrapper.sh”(更改virtualenvwrapper.sh的路径)

现在重新启动 git bash和mkvirtualenv命令将可以正常工作。

On Windows 7 and Git Bash this helps me:

  1. Create a ~/.bashrc file (under your user home folder)
  2. Add line export WORKON_HOME=$HOME/.virtualenvs (you must create this folder if it doesn’t exist)
  3. Add line source “C:\Program Files (x86)\Python36-32\Scripts\virtualenvwrapper.sh” (change path for your virtualenvwrapper.sh)

Restart your git bash and mkvirtualenv command now will work nicely.


回答 8

在Windows 10和Windows的Python36上使用Git Bash,我在稍微不同的位置找到了virtualenvwrapper.sh,运行此命令解决了该问题

source virtualenvwrapper.sh 
/c/users/[myUserName]/AppData/Local/Programs/Python36/Scripts

Using Git Bash on Windows 10 and Python36 for Windows I found the virtualenvwrapper.sh in a slightly different place and running this resolved the issue

source virtualenvwrapper.sh 
/c/users/[myUserName]/AppData/Local/Programs/Python36/Scripts

回答 9

通过在〜/ .bash_profile(或unix中的〜/ .bashrc)文件中添加以下两行,解决了我在python 2.7.6的Ubuntu 14.04 OS中的问题。

源“ /usr/local/bin/virtualenvwrapper.sh”

导出WORKON_HOME =“ / opt / virtual_env /”

然后将这两行都执行到终端上。

Solved my issue in Ubuntu 14.04 OS with python 2.7.6, by adding below two lines into ~/.bash_profile (or ~/.bashrc in unix) files.

source “/usr/local/bin/virtualenvwrapper.sh”

export WORKON_HOME=”/opt/virtual_env/”

And then executing both these lines onto the terminal.


回答 10

在Windows 10上,要创建虚拟环境,我将“ pip mkvirtualenv myproject” 替换为“ mkvirtualenv myproject”,并且效果很好。

On Windows 10, to create the virtual environment, I replace “pip mkvirtualenv myproject” by “mkvirtualenv myproject” and that works well.


如何在OSX 10.6中将MySQLdb与Python和Django一起使用?

问题:如何在OSX 10.6中将MySQLdb与Python和Django一起使用?

对于OSX 10.6用户,这是一个讨论很多的问题,但是我一直找不到能够解决问题的解决方案。这是我的设置:

Python 2.6.1 64位Django 1.2.1 MySQL 5.1.47 osx10.6 64位

我使用–no-site-packages创建了一个virtualenvwrapper,然后安装了Django。当我激活virtualenv并运行python manage.py syncdb时,出现以下错误:

Traceback (most recent call last):
File "manage.py", line 11, in <module>
  execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
  utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
  klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
  module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/sql.py", line 5, in <module>
from django.contrib.contenttypes import generic
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/contrib/contenttypes/generic.py", line 6, in <module>
  from django.db import connection
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in <module>
  connection = connections[DEFAULT_DB_ALIAS]
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
  backend = load_backend(db['ENGINE'])
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
  return import_module('.base', backend_name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
  raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

我还安装了MySQL for Python适配器,但无济于事(也许我安装不正确?)。

有人处理过吗?

This is a much discussed issue for OSX 10.6 users, but I haven’t been able to find a solution that works. Here’s my setup:

Python 2.6.1 64bit Django 1.2.1 MySQL 5.1.47 osx10.6 64bit

I create a virtualenvwrapper with –no-site-packages, then installed Django. When I activate the virtualenv and run python manage.py syncdb, I get this error:

Traceback (most recent call last):
File "manage.py", line 11, in <module>
  execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
  utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
  klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
  module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/sql.py", line 5, in <module>
from django.contrib.contenttypes import generic
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/contrib/contenttypes/generic.py", line 6, in <module>
  from django.db import connection
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in <module>
  connection = connections[DEFAULT_DB_ALIAS]
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
  backend = load_backend(db['ENGINE'])
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
  return import_module('.base', backend_name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
  raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

I’ve also installed the MySQL for Python adapter, but to no avail (maybe I installed it improperly?).

Anyone dealt with this before?


回答 0

我有同样的错误,pip install MySQL-python并为我解决了。

备用安装:

  • 如果您没有点子,easy_install MySQL-python应该可以。
  • 如果您的python是由打包系统管理的,则可能必须使用该系统(例如sudo apt-get install ...

下面,Soli指出如果收到以下错误:

EnvironmentError: mysql_config not found

…然后您还有另一个系统依赖性问题。解决方案因系统而异,但对于Debian衍生的系统:

sudo apt-get install python-mysqldb

I had the same error and pip install MySQL-python solved it for me.

Alternate installs:

  • If you don’t have pip, easy_install MySQL-python should work.
  • If your python is managed by a packaging system, you might have to use that system (e.g. sudo apt-get install ...)

Below, Soli notes that if you receive the following error:

EnvironmentError: mysql_config not found

… then you have a further system dependency issue. Solving this will vary from system to system, but for Debian-derived systems:

sudo apt-get install python-mysqldb


回答 1

运行Ubuntu,我必须做:

sudo apt-get install python-mysqldb

Running Ubuntu, I had to do:

sudo apt-get install python-mysqldb

回答 2

除了其他答案,以下内容还帮助我完成了mysql-python的安装:

virtualenv,mysql-python,pip:有人知道吗?

在Ubuntu上…

apt-get install libmysqlclient-dev
apt-get install python-dev
pip install mysql-python

如果您没有适当的权限,请不要忘记在命令开头添加“ sudo”。

Adding to other answers, the following helped me finish the installation mysql-python:

virtualenv, mysql-python, pip: anyone know how?

On Ubuntu…

apt-get install libmysqlclient-dev
apt-get install python-dev
pip install mysql-python

Don’t forget to add ‘sudo’ to the beginning of commands if you don’t have the proper permissions.


回答 3

试试下面的命令。他们为我工作:

brew install mysql-connector-c 
pip install MySQL-python

Try this the commands below. They work for me:

brew install mysql-connector-c 
pip install MySQL-python

回答 4

mysql_config必须在路上。在Mac上,执行

export PATH=$PATH:/usr/local/mysql/bin/
pip install MySQL-python

mysql_config must be on the path. On Mac, do

export PATH=$PATH:/usr/local/mysql/bin/
pip install MySQL-python

回答 5

pip install mysql-python

提出了一个错误:

EnvironmentError:找不到mysql_config

sudo apt-get install python-mysqldb

解决了问题。

pip install mysql-python

raised an error:

EnvironmentError: mysql_config not found

sudo apt-get install python-mysqldb

fixed the problem.


回答 6

我如何工作的:

virtualenv -p python3.5 env/test

在采购我的环境后:

pip install pymysql
pip install django

然后,我运行了startproject并在manage.py中添加了以下内容:

+ try:
+     import pymysql
+     pymysql.install_as_MySQLdb()
+ except:
+     pass

此外,还更新了此内部设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'foobar_db',
        'USER': 'foobaruser',
        'PASSWORD': 'foobarpwd',
    }
}

我也已经configparser==3.5.0在我的virtualenv中安装了,不确定是否需要…

希望能帮助到你,

How I got it working:

virtualenv -p python3.5 env/test

After sourcing my env:

pip install pymysql
pip install django

Then, I ran the startproject and inside the manage.py, I added this:

+ try:
+     import pymysql
+     pymysql.install_as_MySQLdb()
+ except:
+     pass

Also, updated this inside settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'foobar_db',
        'USER': 'foobaruser',
        'PASSWORD': 'foobarpwd',
    }
}

I also have configparser==3.5.0 installed in my virtualenv, not sure if that was required or not…

Hope it helps,


回答 7

以下对我来说运行64位Ubuntu 13.10的完美工作:

sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev

现在,导航到您的virtualenv(例如env文件夹)并执行以下操作:

sudo ./bin/pip install mysql-python

实际上,我在另一个问题中找到了解决方案,并在下面引用了它:

如果使用–no-site-packages开关(默认设置)创建了virtualenv,则虚拟环境软件包中不包括系统范围内已安装的附加内容,例如MySQLdb。

您需要使用随virtualenv一起安装的pip命令安装MySQLdb。使用bin / activate脚本激活virtualenv,或者在virtualenv中使用bin / pip在本地安装MySQLdb库。

或者,使用–system-site-package开关创建包含系统站点包的新virtualenv。

我认为这也适用于OSX。唯一的问题是获得等效的安装命令libmysqlclient-devpython-dev因为mysql-python我猜它们是编译所必需的 。

希望这可以帮助。

The following worked perfectly for me, running Ubuntu 13.10 64-bit:

sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev

Now, navigate to your virtualenv (such as env folder) and execute the following:

sudo ./bin/pip install mysql-python

I actually found the solution in a separate question and I am quoting it below:

If you have created the virtualenv with the –no-site-packages switch (the default), then system-wide installed additions such as MySQLdb are not included in the virtual environment packages.

You need to install MySQLdb with the pip command installed with the virtualenv. Either activate the virtualenv with the bin/activate script, or use bin/pip from within the virtualenv to install the MySQLdb library locally as well.

Alternatively, create a new virtualenv with system site-packages included by using the –system-site-package switch.

I think this should also work with OSX. The only problem would be getting an equivalent command for installing libmysqlclient-dev and python-dev as they are needed to compile mysql-python I guess.

Hope this helps.


回答 8

试试这个:这为我解决了这个问题。

pip安装MySQL-python

Try this: This solved the issue for me .

pip install MySQL-python


回答 9

此问题是由于MySQL for Python适配器安装不完整/错误导致的。具体来说,我必须编辑mysql_config文件的路径以指向/ usr / local / mysql / bin / mysql_config-在本文中进行了详细讨论:http : //dakrauth.com/blog/entry/python-and- django-setup-mac-os-x-豹/

This issue was the result of an incomplete / incorrect installation of the MySQL for Python adapter. Specifically, I had to edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config – discussed in greater detail in this article: http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/


回答 10

sudo apt-get install python-mysqldb 在ubuntu中完美工作

pip install mysql-python引发环境错误

sudo apt-get install python-mysqldb works perfectly in ubuntu

pip install mysql-python raises an Environment Error


回答 11

这适用于Red Hat Enterprise Linux Server 6.4版

sudo yum install mysql-devel
sudo yum install python-devel
pip install mysql-python

This worked for Red Hat Enterprise Linux Server release 6.4

sudo yum install mysql-devel
sudo yum install python-devel
pip install mysql-python

回答 12

您可以安装为 pip install mysqlclient

You can install as pip install mysqlclient


回答 13

我进行了OSX Mavericks和Pycharm 3的升级,并开始出现此错误,我使用了pip并易于安装,并得到了以下错误:

命令’/ usr / bin / clang’失败,退出状态为1。

所以我需要更新到Xcode 5,然后再次尝试使用pip进行安装。

pip install mysql-python

那解决了所有问题。

I made the upgrade to OSX Mavericks and Pycharm 3 and start to get this error, i used pip and easy install and got the error:

command’/usr/bin/clang’ failed with exit status 1.

So i need to update to Xcode 5 and tried again to install using pip.

pip install mysql-python

That fix all the problems.


回答 14

此处引发的错误是在导入python模块中。可以通过将python site-packages文件夹添加到OS X上的环境变量$ PYTHONPATH来解决。因此,我们可以将以下命令添加到.bash_profile文件中:

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/pythonx.x/site-packages/"

*用您正在使用的python版本替换xx

The error raised here is in importing the python module. This can be solved by adding the python site-packages folder to the environment variable $PYTHONPATH on OS X. So we can add the following command to the .bash_profile file:

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/pythonx.x/site-packages/"

*replace x.x with the python version you are using


回答 15

如果您使用的是python3,请尝试以下操作(我的操作系统是Ubuntu 16.04):

sudo apt-get install python3-mysqldb

If you are using python3, then try this(My OS is Ubuntu 16.04):

sudo apt-get install python3-mysqldb

回答 16

pip在Windows 8 64位系统上对我不起作用。 easy_install mysql-python为我工作。easy_install如果pip不起作用,您可以用来避免在Windows上生成二进制文件。

pip did not work for me on windows 8 64 bits system. easy_install mysql-python works for me. You can use easy_install to avoid building binaries on windows if pip does not work.


回答 17

我在OSX 10.6.6上遇到了相同的问题。但是,只是一个简单easy_install mysql-python的终端无法解决它,因为随之而来的另一个麻烦是:

error: command 'gcc-4.2' failed with exit status 1

显然,从XCode3(OSX 10.6附带提供)升级到XCode4后,就会出现此问题。此较新版本删除了对构建ppc拱的支持。如果相同,请尝试以下操作easy_install mysql-python

sudo bash
export ARCHFLAGS='-arch i386 -arch x86_64'
rm -r build
python setup.py build
python setup.py install

非常感谢Ned Deily的解决方案。在这里检查

I had the same problem on OSX 10.6.6. But just a simple easy_install mysql-python on terminal did not solve it as another hiccup followed:

error: command 'gcc-4.2' failed with exit status 1.

Apparently, this issue arises after upgrading from XCode3 (which is natively shipped with OSX 10.6) to XCode4. This newer ver removes support for building ppc arch. If its the same case, try doing as follows before easy_install mysql-python

sudo bash
export ARCHFLAGS='-arch i386 -arch x86_64'
rm -r build
python setup.py build
python setup.py install

Many thanks to Ned Deily for this solution. Check here


回答 18

对我来说,只需重新安装mysql-python即可解决问题

pip uninstall mysql-python
pip install mysql-python

For me the problem got solved by simply reinstalling mysql-python

pip uninstall mysql-python
pip install mysql-python

回答 19

安装命令行工具对我有用:

xcode-select --install

Install Command Line Tools Works for me:

xcode-select --install

回答 20

我通过MySQL-python使用pip安装库克服了相同的问题。当我第一次在settings.py中更改数据库设置并执行makemigrations命令时,可以看到控制台上显示的消息(解决方案遵循以下消息,请看一下)。

  (vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

最后,我克服了以下问题:

(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQLdb
Collecting MySQLdb
  Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb
(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQL-python
Collecting MySQL-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 112kB 364kB/s 
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python ... done
  Stored in directory: /Users/admin/Library/Caches/pip/wheels/38/a3/89/ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
No changes detected
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, rest_framework, messages, crispy_forms
  Apply all migrations: admin, contenttypes, sessions, auth, PyApp
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying PyApp.0001_initial... OK
  Applying PyApp.0002_auto_20170310_0936... OK
  Applying PyApp.0003_auto_20170310_0953... OK
  Applying PyApp.0004_auto_20170310_0954... OK
  Applying PyApp.0005_auto_20170311_0619... OK
  Applying PyApp.0006_auto_20170311_0622... OK
  Applying PyApp.0007_loraevksensor... OK
  Applying PyApp.0008_auto_20170315_0752... OK
  Applying PyApp.0009_auto_20170315_0753... OK
  Applying PyApp.0010_auto_20170315_0806... OK
  Applying PyApp.0011_auto_20170315_0814... OK
  Applying PyApp.0012_auto_20170315_0820... OK
  Applying PyApp.0013_auto_20170315_0822... OK
  Applying PyApp.0014_auto_20170315_0907... OK
  Applying PyApp.0015_auto_20170315_1041... OK
  Applying PyApp.0016_auto_20170315_1355... OK
  Applying PyApp.0017_auto_20170315_1401... OK
  Applying PyApp.0018_auto_20170331_1348... OK
  Applying PyApp.0019_auto_20170331_1349... OK
  Applying PyApp.0020_auto_20170331_1350... OK
  Applying PyApp.0021_auto_20170331_1458... OK
  Applying PyApp.0022_delete_postoffice... OK
  Applying PyApp.0023_posoffice... OK
  Applying PyApp.0024_auto_20170331_1504... OK
  Applying PyApp.0025_auto_20170331_1511... OK
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK
(vir_env) admins-MacBook-Pro-3:src admin$ 

I overcame the same problem by installing MySQL-python library using pip. You can see the message displayed on my console when I first changed my database settings in settings.py and executed makemigrations command(The solution is following the below message, just see that).

  (vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

Finally I overcame this problem as follows:

(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQLdb
Collecting MySQLdb
  Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb
(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQL-python
Collecting MySQL-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 112kB 364kB/s 
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python ... done
  Stored in directory: /Users/admin/Library/Caches/pip/wheels/38/a3/89/ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
No changes detected
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, rest_framework, messages, crispy_forms
  Apply all migrations: admin, contenttypes, sessions, auth, PyApp
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying PyApp.0001_initial... OK
  Applying PyApp.0002_auto_20170310_0936... OK
  Applying PyApp.0003_auto_20170310_0953... OK
  Applying PyApp.0004_auto_20170310_0954... OK
  Applying PyApp.0005_auto_20170311_0619... OK
  Applying PyApp.0006_auto_20170311_0622... OK
  Applying PyApp.0007_loraevksensor... OK
  Applying PyApp.0008_auto_20170315_0752... OK
  Applying PyApp.0009_auto_20170315_0753... OK
  Applying PyApp.0010_auto_20170315_0806... OK
  Applying PyApp.0011_auto_20170315_0814... OK
  Applying PyApp.0012_auto_20170315_0820... OK
  Applying PyApp.0013_auto_20170315_0822... OK
  Applying PyApp.0014_auto_20170315_0907... OK
  Applying PyApp.0015_auto_20170315_1041... OK
  Applying PyApp.0016_auto_20170315_1355... OK
  Applying PyApp.0017_auto_20170315_1401... OK
  Applying PyApp.0018_auto_20170331_1348... OK
  Applying PyApp.0019_auto_20170331_1349... OK
  Applying PyApp.0020_auto_20170331_1350... OK
  Applying PyApp.0021_auto_20170331_1458... OK
  Applying PyApp.0022_delete_postoffice... OK
  Applying PyApp.0023_posoffice... OK
  Applying PyApp.0024_auto_20170331_1504... OK
  Applying PyApp.0025_auto_20170331_1511... OK
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK
(vir_env) admins-MacBook-Pro-3:src admin$ 

回答 21

运行此命令

sudo pip install mysql-python;

现在您可以运行命令了。

python manage.py startapp filename;

Run this command

sudo pip install mysql-python;

now you can run your command.

python manage.py startapp filename;

回答 22

我遇到了类似的情况,例如您在Mac OS X上的virtualenv中使用python3.7和django 2.1。尝试运行命令:

pip install mysql-python
pip install pymysql

__init__.py在您的项目文件夹中编辑文件,并添加以下内容:

import pymysql

pymysql.install_as_MySQLdb()

然后运行:python3 manage.py runserverpython manage.py runserver

I encountered similar situations like yours that I am using python3.7 and django 2.1 in virtualenv on mac osx. Try to run command:

pip install mysql-python
pip install pymysql

And edit __init__.py file in your project folder and add following:

import pymysql

pymysql.install_as_MySQLdb()

Then run: python3 manage.py runserver or python manage.py runserver


如何删除/删除virtualenv?

问题:如何删除/删除virtualenv?

我使用以下命令创建了一个环境: virtualenv venv --distribute

:我无法用下面的命令将其删除rmvirtualenv venv这是部分virtualenvwrapper中提到下面virtualenvwrapper答案

ls在当前目录上执行了,但仍然看到venv

我可以删除它的唯一方法似乎是: sudo rm -rf venv

请注意该环境处于非活动状态。我正在运行Ubuntu 11.10。有任何想法吗?我尝试重新启动系统无济于事。

I created an environment with the following command: virtualenv venv --distribute

I cannot remove it with the following command: rmvirtualenv venvThis is part of virtualenvwrapper as mentioned in answer below for virtualenvwrapper

I do an lson my current directory and I still see venv

The only way I can remove it seems to be: sudo rm -rf venv

Note that the environment is not active. I’m running Ubuntu 11.10. Any ideas? I’ve tried rebooting my system to no avail.


回答 0

而已!没有用于删除虚拟环境的命令。只需停用它,然后通过递归删除它即可消除应用程序的工件。

请注意,无论您使用哪种虚拟环境,都一样。virtualenvvenv,Python环境pyenvpipenv都在这里基于同样的原则。

That’s it! There is no command for deleting your virtual environment. Simply deactivate it and rid your application of its artifacts by recursively removing it.

Note that this is the same regardless of what kind of virtual environment you are using. virtualenv, venv, Anaconda environment, pyenv, pipenv are all based the same principle here.


回答 1

只是为了回应@skytreader先前评论的内容,它rmvirtualenv是由virtualenvwrapper而不是提供的命令virtualenv。也许您没有virtualenvwrapper安装?

有关更多详细信息,请参见VirtualEnvWrapper命令参考

Just to echo what @skytreader had previously commented, rmvirtualenv is a command provided by virtualenvwrapper, not virtualenv. Maybe you didn’t have virtualenvwrapper installed?

See VirtualEnvWrapper Command Reference for more details.


回答 2

采用 rmvirtualenv

在中删除环境$WORKON_HOME

句法:

rmvirtualenv ENVNAME

在删除当前环境之前,必须使用停用功能。

$ rmvirtualenv my_env

参考:http : //virtualenvwrapper.readthedocs.io/en/latest/command_ref.html

Use rmvirtualenv

Remove an environment, in the $WORKON_HOME.

Syntax:

rmvirtualenv ENVNAME

You must use deactivate before removing the current environment.

$ rmvirtualenv my_env

Reference: http://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html


回答 3

您可以通过递归卸载所有依赖项来删除所有依赖项,然后删除venv。

编辑,包括艾萨克·特纳评论

source venv/bin/activate
pip freeze > requirements.txt
pip uninstall -r requirements.txt -y
deactivate
rm -r venv/

You can remove all the dependencies by recursively uninstalling all of them and then delete the venv.

Edit including Isaac Turner commentary

source venv/bin/activate
pip freeze > requirements.txt
pip uninstall -r requirements.txt -y
deactivate
rm -r venv/

回答 4

只需从系统中删除虚拟环境即可,无需特殊命令

rm -rf venv

Simply remove the virtual environment from the system.There’s no special command for it

rm -rf venv

回答 5

来自virtualenv的官方文档https://virtualenv.pypa.io/en/stable/userguide/

删除环境

只需删除虚拟环境,然后将其所有内容删除即可,只需删除虚拟环境即可:

(ENV)$ deactivate
$ rm -r /path/to/ENV

from virtualenv’s official document https://virtualenv.pypa.io/en/stable/userguide/

Removing an Environment

Removing a virtual environment is simply done by deactivating it and deleting the environment folder with all its contents:

(ENV)$ deactivate
$ rm -r /path/to/ENV

回答 6

如果您使用的是pyenv,则可以删除您的虚拟环境:

$ pyenv virtualenv-delete <name>

If you are using pyenv, it is possible to delete your virtual environment:

$ pyenv virtualenv-delete <name>

回答 7

以下命令对我有用。

rm -rf /path/to/virtualenv

The following command works for me.

rm -rf /path/to/virtualenv

回答 8

我曾经pyenv uninstall my_virt_env_name删除虚拟环境。

注意:我使用的是通过安装脚本安装的pyenv-virtualenv。

I used pyenv uninstall my_virt_env_name to delete the virual environment.

Note: I’m using pyenv-virtualenv installed through the install script.


回答 9

如果您是Windows用户并且正在使用conda在Anaconda提示符下管理环境,则可以执行以下操作:

确保停用虚拟环境或重新启动Anaconda Prompt。使用以下命令删除虚拟环境:

$ conda env remove --name $MyEnvironmentName

或者,您可以转到

C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\envs\MYENVIRONMENTNAME

(这是默认文件路径)并手动删除文件夹。

If you are a Windows user and you are using conda to manage the environment in Anaconda prompt, you can do the following:

Make sure you deactivate the virtual environment or restart Anaconda Prompt. Use the following command to remove virtual environment:

$ conda env remove --name $MyEnvironmentName

Alternatively, you can go to the

C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\envs\MYENVIRONMENTNAME

(that’s the default file path) and delete the folder manually.


回答 10

如果您是Windows用户,则位于C:\ Users \您的用户名\ Envs中。您可以从那里删除它。

也可以在命令提示符下输入rmvirtualenv环境名称。

我尝试使用命令提示符,所以它说已删除,但它仍然存在。所以我手动将其删除。

if you are windows user, then it’s in C:\Users\your_user_name\Envs. You can delete it from there.

Also try in command prompt rmvirtualenv environment name.

I tried with command prompt so it said deleted but it was still existed. So i manually delete it.


回答 11

deactivate是您要查找的命令。就像已经说过的一样,没有删除虚拟环境的命令。只需停用它!

deactivate is the command you are looking for. Like what has already been said, there is no command for deleting your virtual environment. Simply deactivate it!


回答 12

如果您是Windows用户,还可以通过以下步骤删除环境:C:/Users/username/Anaconda3/envs 在这里,您可以看到虚拟环境列表,并删除不再需要的环境。

If you’re a windows user, you can also delete the environment by going to: C:/Users/username/Anaconda3/envs Here you can see a list of virtual environment and delete the one that you no longer need.


回答 13

您可以按照以下步骤删除与virtualenv关联的所有文件,然后再次重新安装virtualenv并使用它

cd {python virtualenv folder}

find {broken virtualenv}/ -type l                             ## to list out all the links

deactivate                                           ## deactivate if virtualenv is active

find {broken virtualenv}/ -type l -delete                    ## to delete the broken links

virtualenv {broken virtualenv} --python=python3           ## recreate links to OS's python

workon {broken virtualenv}                       ## activate & workon the fixed virtualenv

pip3 install  ... {other packages required for the project}

You can follow these steps to remove all the files associated with virtualenv and then reinstall the virtualenv again and using it

cd {python virtualenv folder}

find {broken virtualenv}/ -type l                             ## to list out all the links

deactivate                                           ## deactivate if virtualenv is active

find {broken virtualenv}/ -type l -delete                    ## to delete the broken links

virtualenv {broken virtualenv} --python=python3           ## recreate links to OS's python

workon {broken virtualenv}                       ## activate & workon the fixed virtualenv

pip3 install  ... {other packages required for the project}


回答 14

步骤1:通过复制并粘贴以下命令来删除virtualenv virtualenvwrapper:

$ sudo pip uninstall virtualenv virtualenvwrapper

步骤2:转到.bashrc并删除所有virtualenv和virtualenvwrapper

打开终端:

$ sudo nano .bashrc

向下滚动,您将看到下面的代码,然后将其删除。

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

接下来,获取.bashrc:

$ source ~/.bashrc

最后步骤:在没有终端/外壳的情况下,转到/ home并查找.virtualenv(我忘记了名称,因此,如果您发现与之相似.virtualenv.venv只是删除它。这将起作用。

step 1: delete virtualenv virtualenvwrapper by copy and paste the following command below:

$ sudo pip uninstall virtualenv virtualenvwrapper

step 2: go to .bashrc and delete all virtualenv and virtualenvwrapper

open terminal:

$ sudo nano .bashrc

scroll down and you will see the code bellow then delete it.

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

next, source the .bashrc:

$ source ~/.bashrc

FINAL steps: without terminal/shell go to /home and find .virtualenv (I forgot the name so if your find similar to .virtualenv or .venv just delete it. That will work.


如何离开/退出/停用Python virtualenv

问题:如何离开/退出/停用Python virtualenv

我正在使用virtualenv和virtualenvwrapper。我可以使用workon命令在virtualenv之间切换。

me@mymachine:~$ workon env1
(env1)me@mymachine:~$ workon env2
(env2)me@mymachine:~$ workon env1
(env1)me@mymachine:~$ 

如何退出所有虚拟机并再次在真实计算机上工作?现在,我唯一要回到的方法me@mymachine:~$是退出外壳并启动一个新外壳。真烦人。有没有什么要执行的命令,如果是的话,这是什么?如果这样的命令不存在,我将如何创建它?

I’m using virtualenv and the virtualenvwrapper. I can switch between virtualenv’s just fine using the workon command.

me@mymachine:~$ workon env1
(env1)me@mymachine:~$ workon env2
(env2)me@mymachine:~$ workon env1
(env1)me@mymachine:~$ 

How do I exit all virtual machines and work on my real machine again? Right now, the only way I have of getting back to me@mymachine:~$ is to exit the shell and start a new one. That’s kind of annoying. Is there a command to work on “nothing”, and if so, what is it? If such a command does not exist, how would I go about creating it?


回答 0

通常,激活virtualenv会给您提供一个名为:

$ deactivate

这使情况恢复正常。

我只是再次专门查看的代码virtualenvwrapper,是的,它也支持deactivate从所有virtualenvs逃脱的方式。

如果您要离开Anaconda环境,则该命令取决于您的的版本conda。最新版本(如4.6)conda直接在您的shell中安装一个函数,在这种情况下,您可以运行:

conda deactivate

较旧的conda版本改为使用独立脚本实现停用:

source deactivate

Usually, activating a virtualenv gives you a shell function named:

$ deactivate

which puts things back to normal.

I have just looked specifically again at the code for virtualenvwrapper, and, yes, it too supports deactivate as the way to escape from all virtualenvs.

If you are trying to leave an Anaconda environment, the command depends upon your version of conda. Recent versions (like 4.6) install a conda function directly in your shell, in which case you run:

conda deactivate

Older conda versions instead implement deactivation using a stand-alone script:

source deactivate

回答 1

我所定义的别名workoff,作为相反workon

alias workoff='deactivate'

很容易记住:

[bobstein@host ~]$ workon django_project
(django_project)[bobstein@host ~]$ workoff
[bobstein@host ~]$

I defined an alias, workoff, as the opposite of workon:

alias workoff='deactivate'

It is easy to remember:

[bobstein@host ~]$ workon django_project
(django_project)[bobstein@host ~]$ workoff
[bobstein@host ~]$

回答 2

采用:

$ deactivate 

如果这不起作用,请尝试

$ source deactivate

任何知道Bash的source工作原理的人都会认为这很奇怪,但是围绕virtualenv的一些包装器/工作流将其实现为对Bash的补充/对应source activate。你的旅费可能会改变。

Use:

$ deactivate 

If this doesn’t work, try

$ source deactivate

Anyone who knows how Bash source works will think that’s odd, but some wrappers/workflows around virtualenv implement it as a complement/counterpart to source activate. Your mileage may vary.


回答 3

要激活Python虚拟环境:

$cd ~/python-venv/
$./bin/activate

停用:

$deactivate

To activate a Python virtual environment:

$cd ~/python-venv/
$./bin/activate

To deactivate:

$deactivate

回答 4

我发现在Miniconda3环境中时,我必须运行:

conda deactivate

deactivate没有source deactivate为我工作,也没有为我工作。

I found that when within a Miniconda3 environment I had to run:

conda deactivate

Neither deactivate nor source deactivate worked for me.


回答 5

您可以使用virtualenvwrapper来简化您的使用方式virtualenv

安装virtualenvwrapper

pip install virtualenvwrapper

如果您使用的是标准外壳,请打开~/.bashrc~/.zshrc使用Oh My Zsh。添加这两行:

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

要激活现有的virtualenv,请使用命令workon

$ workon myenv
(myenv)$

为了停用您的virtualenv:

(myenv)$ deactivate

这是我的教程,逐步介绍了如何安装virtualenv和virtualenvwrapper。

You can use virtualenvwrapper in order to ease the way you work with virtualenv.

Installing virtualenvwrapper:

pip install virtualenvwrapper

If you are using a standard shell, open your ~/.bashrc or ~/.zshrc if you use Oh My Zsh. Add these two lines:

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

To activate an existing virtualenv, use command workon:

$ workon myenv
(myenv)$

In order to deactivate your virtualenv:

(myenv)$ deactivate

Here is my tutorial, step by step on how to install virtualenv and virtualenvwrapper.


回答 6

由于无法通过寻找这种命令的常用方法来发现deactivate通过源创建的功能,因此您可能希望创建一个仅执行该功能的命令。~/bin/activate~/bindeactivate

问题是,如果一个脚本名叫deactivate包含单个命令,则该脚本deactivate如果不在venv中意外执行,将导致无限循环。一个常见的错误。

通过仅deactivate在函数存在时执行(即已通过source创建activate),可以避免这种情况。

#!/bin/bash

declare -Ff deactivate  && deactivate

Since the deactivate function created by sourcing ~/bin/activate cannot be discovered by the usual means of looking for such a command in ~/bin, you may wish to create one that just executes the function deactivate.

The problem is that a script named deactivate containing a single command deactivate will cause an endless loop if accidentally executed while not in the venv. A common mistake.

This can be avoided by only executing deactivate if the function exists (i.e. has been created by sourcing activate).

#!/bin/bash

declare -Ff deactivate  && deactivate

回答 7

使用deactivate

(my_env) user@user:~/my_env$ deactivate
user@user-Lenovo-E40-80:~/my_env$ 

注意,(my_env)不见了。

Use deactivate.

(my_env) user@user:~/my_env$ deactivate
user@user-Lenovo-E40-80:~/my_env$ 

Note, (my_env) is gone.


回答 8

我使用基于autoenv的zsh- autoenv

zsh-autoenv自动获取(已知/列入白名单的).autoenv.zsh文件,通常在项目根目录中使用。它处理“进入”和“离开”事件,变量的嵌套和隐藏(覆盖和还原)。

这是一个例子:

; cd dtree 
Switching to virtual environment: Development tree utiles
;dtree(feature/task24|✓); cat .autoenv.zsh       
# Autoenv.
echo -n "Switching to virtual environment: "
printf "\e[38;5;93m%s\e[0m\n" "Development tree utiles"
workon dtree
# eof
dtree(feature/task24|✓); cat .autoenv_leave.zsh 
deactivate

因此,当我离开dtree目录时,虚拟环境将自动退出。

"Development tree utiles" 只是一个名字而已。

I use zsh-autoenv which is based off autoenv.

zsh-autoenv automatically sources (known/whitelisted) .autoenv.zsh files, typically used in project root directories. It handles “enter” and leave” events, nesting, and stashing of variables (overwriting and restoring).

Here is an example:

; cd dtree 
Switching to virtual environment: Development tree utiles
;dtree(feature/task24|✓); cat .autoenv.zsh       
# Autoenv.
echo -n "Switching to virtual environment: "
printf "\e[38;5;93m%s\e[0m\n" "Development tree utiles"
workon dtree
# eof
dtree(feature/task24|✓); cat .autoenv_leave.zsh 
deactivate

So when I leave the dtree directory, the virtual environment is automatically exited.

"Development tree utiles" is just a name… No hidden mean linking to the Illuminati in here.


回答 9

使用deactivatevenv activate脚本提供的功能,您需要信任正确激活了禁用功能的代码,才能将所有环境变量完全重置为以前的状态-不仅要考虑原始激活,还要考虑所有开关配置其他在此期间您可能已经完成的工作

可能很好,但是确实会带来一种新的,非零的风险,即事后修改环境。

但是,从工艺上讲,直接更改其父级的环境变量在技术上是不可能的,因此我们可以使用单独的子外壳来确保我们venv的进程不会留下任何残留更改:


激活:

$ bash --init-file PythonVenv/bin/activate

  • 这将在周围启动一个新的shell venv。您原来的bash外壳保持不变。

停用:

$ exit[CTRL]+[D]

  • 这将退出整个外壳venv,并使您回到激活脚本对环境进行任何更改之前的原始外壳。

例:

[user@computer ~]$ echo $VIRTUAL_ENV
No virtualenv!

[user@computer ~]$ bash --init-file PythonVenv/bin/activate

(PythonVenv) [user@computer ~]$ echo $VIRTUAL_ENV
/home/user/PythonVenv

(PythonVenv) [user@computer ~]$ exit
exit

[user@computer ~]$ echo $VIRTUAL_ENV
No virtualenv!

Using the deactivate feature provided by the venv’s activate script requires you to trust the deactivation function to be properly coded to cleanly reset all environment variables back to how they were before— taking into account not only the original activation, but also any switches, configuration, or other work you may have done in the meantime.

It’s probably fine, but it does introduce a new, non-zero risk of leaving your environment modified afterwards.

However, it’s not technically possible for a process to directly alter the environment variables of its parent, so we can use a separate sub-shell to be absolutely sure our venvs don’t leave any residual changes behind:


To activate:

$ bash --init-file PythonVenv/bin/activate

  • This starts a new shell around the venv. Your original bash shell remains unmodified.

To deactivate:

$ exit OR [CTRL]+[D]

  • This exits the entire shell the venv is in, and drops you back to the original shell from before the activation script made any changes to the environment.

Example:

[user@computer ~]$ echo $VIRTUAL_ENV
No virtualenv!

[user@computer ~]$ bash --init-file PythonVenv/bin/activate

(PythonVenv) [user@computer ~]$ echo $VIRTUAL_ENV
/home/user/PythonVenv

(PythonVenv) [user@computer ~]$ exit
exit

[user@computer ~]$ echo $VIRTUAL_ENV
No virtualenv!

回答 10

在处理安装程序脚本时,我遇到了同样的问题。我看了一下bin / activate_this.py做了什么,并将其反转了。

例:

#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys

# Path to virtualenv
venv_path = os.path.join('/home', 'sixdays', '.virtualenvs', 'test32')

# Save old values
old_os_path = os.environ['PATH']
old_sys_path = list(sys.path)
old_sys_prefix = sys.prefix


def deactivate():
    # Change back by setting values to starting values
    os.environ['PATH'] = old_os_path
    sys.prefix = old_sys_prefix
    sys.path[:0] = old_sys_path


# Activate the virtualenvironment
activate_this = os.path.join(venv_path, 'bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))


# Print list of pip packages for virtualenv for example purpose
import pip
print str(pip.get_installed_distributions())

# Unload pip module
del pip

# Deactivate/switch back to initial interpreter
deactivate()

# Print list of initial environment pip packages for example purpose
import pip
print str(pip.get_installed_distributions())

我不确定100%是否能按预期工作。我可能完全错过了一些东西。

I had the same problem while working on an installer script. I took a look at what the bin/activate_this.py did and reversed it.

Example:

#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys

# Path to virtualenv
venv_path = os.path.join('/home', 'sixdays', '.virtualenvs', 'test32')

# Save old values
old_os_path = os.environ['PATH']
old_sys_path = list(sys.path)
old_sys_prefix = sys.prefix


def deactivate():
    # Change back by setting values to starting values
    os.environ['PATH'] = old_os_path
    sys.prefix = old_sys_prefix
    sys.path[:0] = old_sys_path


# Activate the virtualenvironment
activate_this = os.path.join(venv_path, 'bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))


# Print list of pip packages for virtualenv for example purpose
import pip
print str(pip.get_installed_distributions())

# Unload pip module
del pip

# Deactivate/switch back to initial interpreter
deactivate()

# Print list of initial environment pip packages for example purpose
import pip
print str(pip.get_installed_distributions())

I am not 100% sure if it works as intended. I may have missed something completely.


在virtualenv中使用不同的Python版本

问题:在virtualenv中使用不同的Python版本

我有一个目前使用python 2.5.4运行的Debian系统。我正确安装了virtualenv,一切正常。我是否可以将virtualenv与其他版本的Python一起使用?

我编译了Python 2.6.2,并希望将其与一些virtualenv一起使用。覆盖二进制文件是否足够?还是我必须更改有关库的某些内容?

I have a Debian system currently running with python 2.5.4. I got virtualenv properly installed, everything is working fine. Is there a possibility that I can use a virtualenv with a different version of Python?

I compiled Python 2.6.2 and would like to use it with some virtualenv. Is it enough to overwrite the binary file? Or do I have to change something in respect to the libraries?


回答 0

在创建virtualenv实例时,只需使用--python(或short -p)选项来指定要使用的Python可执行文件,例如:

virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv/>

注意:对于Python 3.3或更高版本,请参阅下面的Aelfinn 答案

Just use the --python (or short -p) option when creating your virtualenv instance to specify the Python executable you want to use, e.g.:

virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv/>

N.B. For Python 3.3 or later, refer to The Aelfinn’s answer below.


回答 1

从Python 3开始,Python Docs建议使用以下命令创建虚拟环境:

python3 -m venv <myenvname>

请注意,venv不允许使用其他版本的Python创建虚拟环境。为此,请安装并使用virtualenv 软件包


过时的信息

pyvenv 脚本可用于创建虚拟环境

pyvenv /path/to/new/virtual/environment

但自Python 3.6起已弃用。

Since Python 3, the Python Docs suggest creating the virtual environment with the following command:

python3 -m venv <myenvname>

Please note that venv does not permit creating virtual environments with other versions of Python. For that, install and use the virtualenv package.


Obsolete information

The pyvenv script can be used to create a virtual environment

pyvenv /path/to/new/virtual/environment

but it has been deprecated since Python 3.6.


回答 2

这些是您在共享托管环境上时需要执行的步骤,需要从源代码安装和编译Python,然后venv从您的Python版本创建。对于Python 2.7.9。您将按照以下方式进行操作:

mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install

虚拟环境

cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate   

自然,这可以适用于您想要复制工作和部署的确切环境的任何情况。

These are the steps you can follow when you are on a shared hosting environment and need to install & compile Python from source and then create venv from your Python version. For Python 2.7.9. you would do something along these lines:

mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install

virtual env

cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate   

Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.


回答 3

virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>
virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>

回答 4

有一个更简单的方法,

virtualenv venv --python=python2.7

感谢您的评论,这仅在系统级别安装了python2.7的情况下才有效(例如/usr/bin/python2.7)。

否则,如果您使用自制软件,则可以使用该路径提供所需的内容。

virtualenv venv --python=/usr/local/bin/python

您可以使用以下命令找到python安装的路径

which python

这也适用于python 3。

which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3

最终凝结为:

virtualenv venv -p `which python`
virtualenv venv -p `which python3`

There is an easier way,

virtualenv venv --python=python2.7

Thanks to a comment, this only works if you have python2.7 installed at the system level (e.g. /usr/bin/python2.7).

Otherwise, if you are using homebrew you can use the path to give you what you want.

virtualenv venv --python=/usr/local/bin/python

You can find the path to your python installation with

which python

This will also work with python 3.

which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3

Ultimately condensing to:

virtualenv venv -p `which python`
virtualenv venv -p `which python3`

回答 5

在Windows下对我来说这有效:

virtualenv --python=c:\Python25\python.exe envname

没有python.exeWindowsError: [Error 5] Access is denied 我有安装virtualenv 1.6.1的Python2.7.1,我想要python 2.5.2。

Under Windows for me this works:

virtualenv --python=c:\Python25\python.exe envname

without the python.exe I got WindowsError: [Error 5] Access is denied I have Python2.7.1 installed with virtualenv 1.6.1, and I wanted python 2.5.2.


回答 6

Mac OSX 10.6.8(Snow Leopard):

1)当您这样做时pip install virtualenv,pip命令与您的一个python版本相关联,virtualenv并被安装到该python版本中。你可以做

 $ which pip   

看看是什么版本的python。如果您看到类似以下内容:

 $ which pip
 /usr/local/bin/pip

然后做:

$ ls -al /usr/local/bin/pip
lrwxrwxr-x  1 root  admin  65 Apr 10  2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip

您可以在输出中看到python版本。

默认情况下,它将是用于您创建的任何新环境的python版本。但是,您可以使用以下命令指定计算机上安装的任何版本的python,以便在新环境中使用-p flag

$ virtualenv -p python3.2 my_env  
Running virtualenv with interpreter /usr/local/bin/python3.2  
New python executable in my_env/bin/python  
Installing setuptools, pip...done.  

virtualenv my_env 将在当前目录中创建一个文件夹,其中将包含Python可执行文件以及pip [command]的副本,可用于安装其他软件包。

http://docs.python-guide.org/en/latest/dev/virtualenvs/

virtualenv 只需将python从您计算机上的某个位置复制到新创建的my_env / bin /目录中。

2)系统python在中/usr/bin,而我安装的各种python版本默认情况下安装在:

 /usr/local/bin

3)我安装的各种python的名称都类似于python2.7python3.2,我可以使用这些名称而不是完整路径。

========虚拟环境=========

1)我在使virtualenvwrapper正常工作时遇到一些问题。这就是我最后输入的内容~/.bash_profile

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects  #Not very important -- mkproject command uses this
#Added the following based on: 
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

2)-p option与virtualenvwrapper 的工作原理不同:我必须指定要在新环境中使用的python解释器的完整路径(当我不想使用默认的python版本时):

$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate

removes the 'bin' directory of the environment activated with 'source
activate' from PATH. 

与virtualenv不同,virtualenvwrapper将在$ WORKON_HOME环境变量指定的位置创建环境。这样可以将您所有的环境都放在一个地方。

Mac OSX 10.6.8 (Snow Leopard):

1) When you do pip install virtualenv, the pip command is associated with one of your python versions, and virtualenv gets installed into that version of python. You can do

 $ which pip   

to see what version of python that is. If you see something like:

 $ which pip
 /usr/local/bin/pip

then do:

$ ls -al /usr/local/bin/pip
lrwxrwxr-x  1 root  admin  65 Apr 10  2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip

You can see the python version in the output.

By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag:

$ virtualenv -p python3.2 my_env  
Running virtualenv with interpreter /usr/local/bin/python3.2  
New python executable in my_env/bin/python  
Installing setuptools, pip...done.  

virtualenv my_env will create a folder in the current directory which will contain the Python executable files, and a copy of the pip [command] which you can use to install other packages.

http://docs.python-guide.org/en/latest/dev/virtualenvs/

virtualenv just copies python from a location on your computer into the newly created my_env/bin/ directory.

2) The system python is in /usr/bin, while the various python versions I installed were, by default, installed into:

 /usr/local/bin

3) The various pythons I installed have names like python2.7 or python3.2, and I can use those names rather than full paths.

========VIRTUALENVWRAPPER=========

1) I had some problems getting virtualenvwrapper to work. This is what I ended up putting in ~/.bash_profile:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects  #Not very important -- mkproject command uses this
#Added the following based on: 
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

2) The -p option works differently with virtualenvwrapper: I have to specify the full path to the python interpreter to be used in the new environment(when I do not want to use the default python version):

$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate

removes the 'bin' directory of the environment activated with 'source
activate' from PATH. 

Unlike virtualenv, virtualenvwrapper will create the environment at the location specified by the $WORKON_HOME environment variable. That keeps all your environments in one place.


回答 7

这两个命令应该可以正常工作。

virtualenv -p python2 myenv (对于python2)

virtualenv -p python3 myenv (对于python3)

These two commands should work fine.

virtualenv -p python2 myenv (For python2)

virtualenv -p python3 myenv (For python3)


回答 8

假设您当前在virtualenv中安装了python 2.7。但是要使用python3.2,您必须使用以下命令进行更新:

$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv

然后通过以下方式激活您的virtualenv

$ source activate name_of_your_virtualenv

然后python --version在shell中执行:检查您的版本是否已更新。

Suppose you currently have python 2.7 installed in your virtualenv. But want to make use of python3.2, You would have to update this with:

$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv

Then activate your virtualenv by:

$ source activate name_of_your_virtualenv

and then do: python --version in shell to check whether your version is now updated.


回答 9

您可以virtualenv使用所需的python版本进行调用。例如:

python3 -m virtualenv venv

或者,直接指向您的virtualenv路径。例如,对于Windows:

c:\Python34\Scripts\virtualenv.exe venv

并通过运行:

venv/bin/python

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

您可以看到虚拟环境中安装的python版本

You can call virtualenv with python version you want. For example:

python3 -m virtualenv venv

Or alternatively directly point to your virtualenv path. e.g. for windows:

c:\Python34\Scripts\virtualenv.exe venv

And by running:

venv/bin/python

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

you can see the python version installed in virtual environment


回答 10

-p方法效果很好,但是您必须记住每次都使用它。如果您的目标是通常切换到较新版本的Python,那很痛苦,而且还可能导致错误。

您的另一种选择是设置一个环境变量,其作用与相同-p。通过~/.bashrc文件或在登录会话中管理环境变量的任何位置进行设置:

export VIRTUALENV_PYTHON=/path/to/desired/version

然后virtualenv,只要您未-p在命令行上指定,就可以使用它。

The -p approach works well, but you do have to remember to use it every time. If your goal is to switch to a newer version of Python generally, that’s a pain and can also lead to mistakes.

Your other option is to set an environment variable that does the same thing as -p. Set this via your ~/.bashrc file or wherever you manage environment variables for your login sessions:

export VIRTUALENV_PYTHON=/path/to/desired/version

Then virtualenv will use that any time you don’t specify -p on the command line.


回答 11

通过使用命令替换为您找到python2甚至更加容易:

virtualenv -p $(which python2) <path/to/new/virtualenv/>

或者在使用virtualenvwrapper时:

mkvirtualenv -p $(which python2) <env_name>

Even easier, by using command substitution to find python2 for you:

virtualenv -p $(which python2) <path/to/new/virtualenv/>

Or when using virtualenvwrapper :

mkvirtualenv -p $(which python2) <env_name>


回答 12

对于Mac(High Sierra),在python3上安装virtualenv并为python2创建virtualenv:

 $ python3 -m pip install virtualenv
 $ python3 -m virtualenv --python=python2 vp27
 $ source vp27/bin/activate
 (vp27)$ python --version
 Python 2.7.14

For Mac(High Sierra), install the virtualenv on python3 and create a virtualenv for python2:

 $ python3 -m pip install virtualenv
 $ python3 -m virtualenv --python=python2 vp27
 $ source vp27/bin/activate
 (vp27)$ python --version
 Python 2.7.14

回答 13

在Mac上,我使用pyenv和virtualenvwrapper。我必须创建一个新的virtualenv。您需要自制软件,如果您使用的是Mac,我假设您已经安装了自制软件,但这只是为了好玩:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python  {virtual_env_name}

我也首先冻结了我的需求,因此我可以使用以下命令重新安装在新的virtualenv中:

pip install -r requirements.txt

On the mac I use pyenv and virtualenvwrapper. I had to create a new virtualenv. You need homebrew which I’ll assume you’ve installed if you’re on a mac, but just for fun:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python  {virtual_env_name}

I also froze my requirements first so i could simply reinstall in the new virtualenv with:

pip install -r requirements.txt

回答 14

[2019年11月]我需要在基于Python 3.8的Arch Linux系统上安装Python 3.7环境(env)。Python 3.7不再存在于系统上,因此我无法降级Python以安装所需的软件包。

此外,我想在虚拟环境(venv)中使用该软件包/ Python 3.7。这就是我做的。


下载Python版本的源文件:

我从下载了Python 3.7.4源文件

https://www.python.org/downloads/source/

/mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz

然后,我将该存档(源文件)提取到

/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/


安装:

[注意:在我的系统中是env,不是venv。]

cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure                 ## 17 sec
time make                        ## 1 min 51 sec
time sudo make install           ## 18 sec
time make clean                  ## 0.3 sec

检查已安装的Python版本:

$ which python
/usr/bin/python

$ python --version
Python 3.8.0

$ which python3.7
/usr/local/bin/python3.7

$ python    ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

$ python3.7    ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0]
>>>

$ python3.7 --version                                                                                                 
Python 3.7.4

如何为特定的Python版本创建venv:

https://docs.python.org/3/tutorial/venv.html

12.2。创建虚拟环境

用于创建和管理虚拟环境的模块称为venvvenv通常会安装可用的最新版本的Python。如果您的系统上有多个Python版本,则可以通过运行python3或所需的任意版本来选择特定的Python版本。

要创建虚拟环境,请确定要放置目录的目录,然后使用带有目录路径的脚本运行venv模块:

python3 -m venv tutorial-env

tutorial-env如果该目录不存在,它将创建一个目录,并在其中创建包含Python解释器,标准库和各种支持文件的副本的目录。…


在[Python 3.8作业环境/系统上]建立Python 3.7 venv:

python3.7 -m venv ~/venv/py3.7      ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate    ## activate that venv
deactivate                          ## deactivate that venv (when done, there)

添加到~/.bashrc

alias p37='echo "   [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'

测试Python 3.7 venv:

$ p37                                                                                                                 
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]

(py3.7)$ python --version
Python 3.7.4

(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] 
>>>

[November 2019] I needed to install a Python 3.7 environment (env) on my Python 3.8-based Arch Linux system. Python 3.7 was no longer on the system, so I could not downgrade Python, to install a package that I needed.

Furthermore, I wanted to use that package / Python 3.7 inside a virtual environment (venv). This is how I did it.


Download Python version source files:

I downloaded the Python 3.7.4 source files from

https://www.python.org/downloads/source/

to

/mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz

I then extracted that archive (source files) to

/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/


Installation:

[Note: in my system env, not a venv.]

cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure                 ## 17 sec
time make                        ## 1 min 51 sec
time sudo make install           ## 18 sec
time make clean                  ## 0.3 sec

Examine installed Python versions:

$ which python
/usr/bin/python

$ python --version
Python 3.8.0

$ which python3.7
/usr/local/bin/python3.7

$ python    ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

$ python3.7    ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0]
>>>

$ python3.7 --version                                                                                                 
Python 3.7.4

How to create a venv for a specific Python version:

https://docs.python.org/3/tutorial/venv.html

12.2. CREATING VIRTUAL ENVIRONMENTS

The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

python3 -m venv tutorial-env

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. …


Create Python 3.7 venv [on a Python 3.8 operating env / system]:

python3.7 -m venv ~/venv/py3.7      ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate    ## activate that venv
deactivate                          ## deactivate that venv (when done, there)

Added to ~/.bashrc:

alias p37='echo "   [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'

Test Python 3.7 venv:

$ p37                                                                                                                 
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]

(py3.7)$ python --version
Python 3.7.4

(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] 
>>>

回答 15

在Linux的Windows子系统中:

  1. 为python3创建环境:

    virtualenv --python=/usr/bin/python3 env
  2. 激活它:

    source env/bin/activate

In windows subsystem for linux:

  1. Create environment for python3:

    virtualenv --python=/usr/bin/python3 env
    
  2. Activate it:

    source env/bin/activate
    

回答 16

在Windows上:

py -3.4x32 -m venv venv34

要么

py -2.6.2 -m venv venv26

这将使用py启动器,它将为您找到正确的python可执行文件(假设您已安装了它)。

On windows:

py -3.4x32 -m venv venv34

or

py -2.6.2 -m venv venv26

This uses the py launcher which will find the right python executable for you (assuming you have it installed).


回答 17

是的,以上答案是正确的,并且在基于LinuxMAC OS X的基于Unix的系统上可以正常工作

我试图为Python2Python3创建virtualenv使用以下命令。

在这里,我已经使用venv2venv3作为其名称Python2Python3分别。

Python2»

MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate        easy_install        pip2.7          python2.7
activate.csh        easy_install-2.7    python          wheel
activate.fish       pip         python-config
activate_this.py    pip2            python2
MacBook-Pro-2:~ admin$ 

Python3»

MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate        easy_install        pip3.6          python3.6
activate.csh        easy_install-3.6    python          wheel
activate.fish       pip         python-config
activate_this.py    pip3            python3
MacBook-Pro-2:~ admin$ 

检查Python安装位置

MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$ 

Yes, the above answers are correct and works fine on Unix based systems like Linux & MAC OS X.

I tried to create virtualenv for Python2 & Python3 with the following commands.

Here I have used venv2 & venv3 as their names for Python2 & Python3 respectively.

Python2 »

MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate        easy_install        pip2.7          python2.7
activate.csh        easy_install-2.7    python          wheel
activate.fish       pip         python-config
activate_this.py    pip2            python2
MacBook-Pro-2:~ admin$ 

Python3 »

MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate        easy_install        pip3.6          python3.6
activate.csh        easy_install-3.6    python          wheel
activate.fish       pip         python-config
activate_this.py    pip3            python3
MacBook-Pro-2:~ admin$ 

Checking Python installation locations

MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$ 

回答 18

对我有用

sudo apt-get install python3-minimal

virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3

It worked for me

sudo apt-get install python3-minimal

virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3

回答 19

我使用pyenv来管理我的python版本。

pyenv install 3.7.3
pyenv local 3.7.3

检查您的python版本:

$ python --version
Python 3.7.3

使用venv创建虚拟环境:

python -m venv .

然后激活虚拟环境:

source bin/activate

检查您的python版本:

$ python --version
Python 3.7.3

您可能需要删除以前的虚拟环境

rm -rf bin

I use pyenv to manage my python version.

pyenv install 3.7.3
pyenv local 3.7.3

Check your python version:

$ python --version
Python 3.7.3

Create the virtual environment with venv:

python -m venv .

Then activate the Virtual Environment:

source bin/activate

Check your python version:

$ python --version
Python 3.7.3

You may need to remove the previous virtual environment

rm -rf bin

回答 20

对于Windows,这些似乎有些复杂。如果您使用的是运行python 3.3或更高版本的Windows,则可以使用python启动器py更加轻松地执行此操作。只需安装不同的python版本,然后运行:

py -[my version] -m venv env

这将env使用python在当前目录中创建一个虚拟环境[my version]。举个例子:

py -3.7 -m venv env
./env/Scripts/activate

这将创建一个env使用python3.7 的虚拟环境并将其激活。不需要路径或其他复杂的东西。

These seem a little overcomplicated for Windows. If you’re on Windows running python 3.3 or later, you can use the python launcher py to do this much more easily. Simply install the different python version, then run:

py -[my version] -m venv env

This will create a virtual environment called env in your current directory, using python [my version]. As an example:

py -3.7 -m venv env
./env/Scripts/activate

This creates a virtual environment called env using python3.7 and activates it. No paths or other complex stuff required.


回答 21

virtualenv -p python3 myenv

链接到创建virtualenv


回答 22

对于2019年的Debian(debian 9)系统,我发现了一个简单的解决方案,可以从虚拟环境中解决问题。

假设虚拟环境是通过以下方式创建的:

python3.7 -m venv myenv

但只有python2和的版本python2.7,因此您需要python3.7的最新功能。

然后,只需运行以下命令:

(myvenv) $ python3.7 -m venv --upgrade /home/username/path/to/myvenv/

如果系统上已经提供python3.7软件包,则会添加这些软件包。

For Debian (debian 9) Systems in 2019, I discovered a simple solution that may solve the problem from within the virtual environment.

Suppose the virtual environment were created via:

python3.7 -m venv myenv

but only has versions of python2 and python2.7, and you need the recent features of python3.7.

Then, simply running the command:

(myvenv) $ python3.7 -m venv --upgrade /home/username/path/to/myvenv/

will add python3.7 packages if they are already available on your system.


回答 23

这是virtualenv的错误。只需升级您的点子即可解决。

pip install --upgrade virtualenv

This was a bug with virtualenv. Just upgrading your pip should be the fix.

pip install --upgrade virtualenv


回答 24

正如在多个答案中已经提到的那样,使用virtualenv是一个干净的解决方案。但是,每个人都应该注意的一个小陷阱是,如果在bash_aliases中设置了python的别名,例如:

python=python3.6

该别名也将在虚拟环境中使用。因此,在这种情况下,无论使用什么解释器创建环境,python -V在虚拟环境中运行的始终将输出3.6

virtualenv venv --python=pythonX.X

As already mentioned in multiple answers, using virtualenv is a clean solution. However a small pitfall that everyone should be aware of is that if an alias for python is set in bash_aliases like:

python=python3.6

this alias will also be used inside the virtual environment. So in this scenario running python -V inside the virtual env will always output 3.6 regardless of what interpreter is used to create the environment:

virtualenv venv --python=pythonX.X

回答 25

它在Windows上使用python 2安装对我有用:

  1. 第1步:安装python 3版本。
  2. 步骤2:为虚拟环境创建一个env文件夹。
  3. 第3步:c:\ Python37 \ python -m venv c:\ path \ to \ env。

这就是我在现有python 2安装上创建Python 3虚拟环境的方式。

It worked for me on windows with python 2 installation :

  1. Step 1: Install python 3 version .
  2. Step 2: create a env folder for the virtual environment.
  3. Step 3 : c:\Python37\python -m venv c:\path\to\env.

This is how i created Python 3 virtual environment on my existing python 2 installation.


回答 26

您可以这样做:

virtualenv -p python3 .

You can do it by doing this:

virtualenv -p python3 .

回答 27

是的,您只需要安装其他版本的python,并在命令中定义其他版本的python的位置,例如:

virtualenv / home / payroll / Documents / env -p / usr / bin / python3

Yes you just need to install the other version of python, and define the location of your other version of python in your command like :

virtualenv /home/payroll/Documents/env -p /usr/bin/python3


回答 28

这是如何在Visual Studio Code文件夹中创建虚拟环境
的分步说明:我使用了Powershell(管理员模式):1.我在要创建虚拟环境的地方创建了VSCode文件夹-“ D:\ Code_Python_VE”。
2.接下来,我键入命令-“ pip3 install virtualenv”。(D:\ Code_Python_VE> pip3安装virtualenv)3. D:\ Code_Python_VE> python3 -m venv project_env
4. D:\ Code_Python_VE> project_env \ Scripts \ activate.bat
5. D:\ Code_Python_VE> ls-这将列出一个新的目录“ project_env”。
6. D:\ Code_Python_VE>代码。这将启动Visual Studio代码。确保命令是(代码。)。
7.创建具有以下内容的launch.jason:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "python",
            "request": "launch",
            "name": "Python: Current File (Integrated Terminal 1)",
            "program": "${file}"
        },
        {
            "name": "Python: Current File (Integrated Terminal 2)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

(请搜索如何转到“调试”窗口并在VS Code中添加新配置)。

  1. 在Visual Studio代码中按F1键,命令托盘将打开-选择Python Interpreter,然后选择虚拟环境project_env。
  2. 使用一条语句print(“ Hello World”)添加test.py文件。
  3. 运行该程序。
  4. 在Visual Studio Code终端-
    (project_env)d:\ Code_Python_VE> python -m pip install –upgrade
    希望对您有所帮助。

Here is the stepbystep how to create the Virtual environment in Visual Studio Code folder: I used Powershell (Administrator mode):
1. I create a VSCode folder – “D:\Code_Python_VE” where I want to create Virtual environment.
2. Next I type the command – “pip3 install virtualenv”. (D:\Code_Python_VE> pip3 install virtualenv) 3. D:\Code_Python_VE> python3 -m venv project_env
4. D:\Code_Python_VE>project_env\Scripts\activate.bat
5. D:\Code_Python_VE> ls – This will list a new directory “project_env”.
6. D:\Code_Python_VE> code . This will start Visual Studio Code. Make sure the command is (code .).
7. Create launch.jason with following content:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "python",
            "request": "launch",
            "name": "Python: Current File (Integrated Terminal 1)",
            "program": "${file}"
        },
        {
            "name": "Python: Current File (Integrated Terminal 2)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

(Please search how to go to Debug window and Add new Configuration in VS Code).

  1. Press F1 in Visual studio code and the command pallet will open – Select Python Interpreter and select the virtual environment project_env.
  2. Add test.py file with one statement print(“Hello World”).
  3. Run this program.
  4. In Visual studio Code terminal –
    (project_env) d:\Code_Python_VE>python -m pip install –upgrade
    I hope this helps.

回答 29

UBUNTU 19.04 /全球Python 3.7。

这对我有用,使用推荐的venv进行python 3开发,从而启用了python 3.8环境。

安装3.8和3.8 venv模块

$ sudo apt install python3.8 python3.8-venv ##以及您需要的任何其他模块

使用您要在该环境中创建的python版本创建虚拟环境

$ /usr/bin/python3.8 -m venv python38-env

切换到您的虚拟环境

$源python38-env / bin / activate

python -V = python 3.8

UBUNTU 19.04 / Global Python 3.7.

This worked for me, enabling a Python 3.8 environment using the recommended venv for python 3 development.

Install 3.8 and 3.8 venv module

$ sudo apt install python3.8 python3.8-venv ## plus any other modules you need

Create your Virtual Env using the python version you want in that env

$ /usr/bin/python3.8 -m venv python38-env

switch into your virtual env

$ source python38-env/bin/activate

python -V = python 3.8