bash:mkvirtualenv:找不到命令

Mik*_*ton 94 python bash centos virtualenv virtualenvwrapper

遵循Doug Hellman的virtualenvwrapper帖子后,我仍然无法启动测试环境.

[mpenning@tsunami ~]$ mkvirtualenv test
-bash: mkvirtualenv: command not found
[mpenning@tsunami ~]$
Run Code Online (Sandbox Code Playgroud)

应该注意的是,我使用WORKON_HOME的不是我的$HOME.我试图寻找/usr/local/bin/virtualenvwrapper.sh如图所示的virtualenvwrapper安装文件,但它不存在.

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


# File: ~/.bash_profile
# ...

export WORKON_HOME="/opt/virtual_env/"
source "/opt/virtual_env/bin/virtualenvwrapper_bashrc"
Run Code Online (Sandbox Code Playgroud)

Mik*_*ton 102

解决方案1:

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

以下是我的.bash_profile作品......

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"
Run Code Online (Sandbox Code Playgroud)

我的安装似乎没有采购工作正常 virtualenvwrapper_bashrc

解决方案2:

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

  • 将`WORKON_HOME`设置为"〜/ .virtualenvs"([默认值](http://virtualenvwrapper.readthedocs.org/en/latest/install.html))允许设置私有virtualenvs (4认同)
  • `WORKON_HOME` 是可选的,与解决方案无关。 (2认同)
  • 我在 `/usr/share` Ubuntu 20.04 中找到了 `virtualenvwrapper/virtualenvwrapper.sh` (2认同)

Eri*_*ich 52

尝试:

source `which virtualenvwrapper.sh`
Run Code Online (Sandbox Code Playgroud)

反引号是命令替换 - 它们接受程序打印出来的任何内容并将其放入表达式中.在这种情况下,"which"检查$ PATH以查找virtualenvwrapper.sh并输出路径.然后shell通过'source'读取脚本.

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

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

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

  • 虽然这可能是解决问题的一个有价值的提示,但答案确实需要更多细节.请[编辑]解释这将如何解决问题.或者,考虑将其写为注释. (4认同)

Nic*_*nes 43

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


Kes*_*hav 23

执行此命令的先决条件 -

1)pip(P ip I nstall P ython的递归首字母缩写)是一个包管理系统,用于安装和管理用Python编写的软件包.许多包都可以在Python包索引(PyPI)中找到.

sudo apt-get install python-pip
Run Code Online (Sandbox Code Playgroud)

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

sudo pip install virtualenv
Run Code Online (Sandbox Code Playgroud)

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

sudo pip install virtualenvwrapper
Run Code Online (Sandbox Code Playgroud)

安装必备组件后,您需要将虚拟环境包装器用于创建虚拟环境.以下是步骤 -

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脚本中的行.


giv*_*Job 6

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

步骤1 安装点子

   sudo apt-get install python-pip
Run Code Online (Sandbox Code Playgroud)

步骤2 安装virtualenv

   sudo pip install virtualenv
Run Code Online (Sandbox Code Playgroud)

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

   mkdir ~/.virtualenvs
Run Code Online (Sandbox Code Playgroud)

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

virtualenv -p /usr/bin/python3.6 venv
Run Code Online (Sandbox Code Playgroud)

SETP 4

   sudo pip install virtualenvwrapper
Run Code Online (Sandbox Code Playgroud)

第5步

   sudo nano ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

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

  export WORKON_HOME=~/.virtualenvs
  source /usr/local/bin/virtualenvwrapper.sh
Run Code Online (Sandbox Code Playgroud)

步骤7 打开新终端(推荐)

步骤8 创建一个新的virtualenv

  mkvirtualenv myawesomeproject
Run Code Online (Sandbox Code Playgroud)

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

  workon myawesomeproject
Run Code Online (Sandbox Code Playgroud)

步骤10 退出新的virtualenv,请使用

 deactivate
Run Code Online (Sandbox Code Playgroud)

并确保使用pip vs pip3

或按照以下步骤在python项目中安装虚拟环境

cd ~/demos
Run Code Online (Sandbox Code Playgroud)

安装环境

python3 -m venv my-project-env
Run Code Online (Sandbox Code Playgroud)

接下来,使用以下命令激活您的虚拟环境:

source my-project-env/bin/activate
Run Code Online (Sandbox Code Playgroud)


Xen*_*mar 5

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

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

sudo pip install virutalenv
sudo pip install virtualenvwrapper
Run Code Online (Sandbox Code Playgroud)

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

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper
Run Code Online (Sandbox Code Playgroud)

您已经成功安装了适用于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
Run Code Online (Sandbox Code Playgroud)

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

source /usr/local/bin/virtualenvwrapper.sh
Run Code Online (Sandbox Code Playgroud)

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

which virtualenvwrapper
Run Code Online (Sandbox Code Playgroud)

到您的终端。这将在您的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"
Run Code Online (Sandbox Code Playgroud)

至:

"/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh"
Run Code Online (Sandbox Code Playgroud)

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

source "virtualenvwrapper.sh"
Run Code Online (Sandbox Code Playgroud)

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