问题: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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。