Python bash: mkvirtualenv: 命令未找到

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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:45:41  来源:igfitidea点击:

bash: mkvirtualenv: command not found

pythonbashcentosvirtualenvvirtualenvwrapper

提问by Mike Pennington

After following the instructions on Doug Hellman's virtualenvwrapperpost, I still could not fire up a test environment.

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

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

It should be noted that I'm using WORKON_HOMEthat is not in my $HOME. I tried looking for /usr/local/bin/virtualenvwrapper.shas shown in the virtualenvwrapperinstallation docs, but it does not exist.

应该注意的是,我使用WORKON_HOME的不是我的$HOME. 我尝试寻找安装文档中/usr/local/bin/virtualenvwrapper.sh所示的内容,但它不存在。virtualenvwrapper

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

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



# File: ~/.bash_profile
# ...

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

采纳答案by Mike Pennington

Solution 1:

解决方案1

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

出于某种原因,virtualenvwrapper.sh安装在/usr/bin/virtualenvwrapper.sh,而不是在/usr/local/bin.

The following in my .bash_profileworks...

以下在我的.bash_profile作品中...

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

My install seems to work fine without sourcing virtualenvwrapper_bashrc

我的安装在没有采购的情况下似乎可以正常工作 virtualenvwrapper_bashrc

Solution 2:

解决方案2

Alternatively as mentioned below, you could leverage the chance that virtualenvwrapper.shis already in your shell's PATHand just issue a source `which virtualenvwrapper.sh`

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

回答by Nick Benes

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

我在 OS X 10.9.1 和 python 2.7.5 上遇到了同样的问题。WORKON_HOME对我来说没有问题,但是我运行后确实必须手动添加source "/usr/local/bin/virtualenvwrapper.sh"~/.bash_profile(或~/.bashrc在 unix 中)pip install virtualenvwrapper

回答by Erich

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'.

反引号是命令替换——它们取出程序打印出来的任何东西并将其放入表达式中。在这种情况下,“which”检查 $PATH 以查找 virtualenvwrapper.sh 并输出它的路径。然后 shell 通过“源”读取脚本。

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:

如果您希望每次重新启动 shell 时都发生这种情况,最好先从“which”命令中获取输出,然后将“source”行放入 shell,如下所示:

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

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

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

回答by Keshav

Prerequisites to execute this command -

执行此命令的先决条件 -

1) pip (recursive acronym of Pip Install Python) 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).

1)PIP(递归缩写,PIPnstall Python)是用于安装和管理Python编写的软件包,软件包管理系统。许多包可以在 Python 包索引 (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.

2) 安装虚拟环境。用于创建虚拟环境,安装相互隔离的多个项目的包和依赖项。

sudo pip install virtualenv

3) Install virtual environment wrapper About virtual env wrapper

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

sudo pip install virtualenvwrapper

After Installing prerequisitesyou 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)

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

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

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

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

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

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

最好在 ~/.bashrc 中放置以上两行,以避免每次打开新shell时都执行上述命令。这就是使用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.
  • 在 Ubuntu 下,您可能需要以 root 身份安装 virtualenv 和 virtualenvwrapper。只需在上面的命令前面加上 sudo。
  • 根据用于安装 virtualenv 的过程,virtualenvwrapper.sh 的路径可能会有所不同。通过运行 $ find /usr -name virtualenvwrapper.sh 找到合适的路径。相应地调整 .bash_profile 或 .bashrc 脚本中的行。

回答by Eugene Zalivadnyi

On Windows 7 and Git Bash this helps me:

在 Windows 7 和 Git Bash 上,这对我有帮助:

  1. Create a ~/.bashrcfile (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)
  1. 创建一个~/.bashrc文件(在您的用户主文件夹下)
  2. 添加行export WORKON_HOME=$HOME/.virtualenvs(如果此文件夹不存在,则必须创建该文件夹)
  3. 添加行源“C:\Program Files (x86)\Python36-32\Scripts\virtualenvwrapper.sh”(更改 virtualenvwrapper.sh 的路径)

Restartyour git bash and mkvirtualenv command now will work nicely.

重新启动你的 git bash 和 mkvirtualenv 命令现在会很好地工作。

回答by leastgrebe

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

在 Windows 10 上使用 Git Bash 并在 Windows 上使用 Python36 我发现 virtualenvwrapper.sh 在一个稍微不同的地方,运行它解决了这个问题

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

回答by Mants

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.

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

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

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

export WORKON_HOME="/opt/virtual_env/"

导出 WORKON_HOME="/opt/virtual_env/"

And then executing both these lines onto the terminal.

然后在终端上执行这两行。

回答by Xen_mar

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:

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

sudo pip install virutalenv
sudo pip install virtualenvwrapper

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

如果你打算使用 python 3 安装相关的 python 3 版本

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 workoninto your terminal. Your terminal will not be able to find the command (workonis a command of virtualenvwrapper). Of course it won't. Workonis an executable that will only be available to you once you load/source the file virtualenvwrapper.sh. But the official installation guidehas you covered on this one, right?. Just open your .bash_profile and insert the following, it says in the documentation:

您已经成功安装了适用于您的 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

Especially the command source /usr/local/bin/virtualenvwrapper.shseems helpful since the command seems to load/source the desired file virtualenvwrapper.shthat contains all the commands you want to work with like workonand 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似乎很有帮助,因为该命令似乎加载/获取virtualenvwrapper.sh包含您想要使用的所有命令的所需文件,例如workonmkvirtualenv但是,是的,不。按照官方安装指南进行操作时,您很可能会收到初始帖子中的错误:mkvirtualenv: command not found. 仍然没有找到命令,您仍然感到沮丧。那么这里有什么问题呢?问题是 virtualenvwrapper.sh 不是您现在正在寻找的。简短的提醒……你正在看这里:

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 PATHfor the file, since it is very likely to be in some folder that is included in the PATH of your system.

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

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.shwith the shell command find / -name virtualenvwrapper.sh

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

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

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

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.shwith the command which virtualenvwrapper.shyou 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:

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

source "virtualenvwrapper.sh"

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

就是这样。你不再沮丧。你已经解决了你的问题。希望。

回答by giveJob

Use this procedure to create virtual env in ubuntu

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

step 1

第1步

Install pip

安装点

   sudo apt-get install python-pip

step 2

第2步

Install virtualenv

安装 virtualenv

   sudo pip install virtualenv

step 3

第 3 步

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

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

   mkdir ~/.virtualenvs

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

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

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

step 4

第四步

   sudo pip install virtualenvwrapper

step 5

第 5 步

   sudo nano ~/.bashrc

step 6

第 6 步

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

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

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

step 7

第 7 步

Open new terminal (recommended)

打开新终端(推荐)

step 8

第 8 步

Create a new virtualenv

创建一个新的虚拟环境

  mkvirtualenv myawesomeproject

step 9

步骤 9

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

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

  workon myawesomeproject

step 10

第 10 步

To exit your new virtualenv, use

要退出新的 virtualenv,请使用

 deactivate

and make sure using pip vs pip3

并确保使用 pip 与 pip3

OR follow the steps below to install virtual environment using python3

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

Install env

安装环境

python3 -m venv my-project-env

and activate your virtual environment using the following command:

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

source my-project-env/bin/activate

回答by Ayoub_DATA

On Windows 10, to create the virtual environment, I replace "pip mkvirtualenvmyproject" by "mkvirtualenvmyproject" and that works well.

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