Python 找不到 Virtualenv 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31133050/
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-19 09:31:58  来源:igfitidea点击:

Virtualenv Command Not Found

pythonmacosvirtualenv

提问by Arial

I couldn't get virtualenvto work despite various attempts. I installed virtualenvon MAC OS X using:

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

pip install virtualenv

and have also added the PATHinto my .bash_profile. Every time I try to run the virtualenvcommand, it returns:

并且还添加PATH到我的.bash_profile. 每次我尝试运行virtualenv命令时,它都会返回:

-bash: virtualenv: command not found

Every time I run pip install virtualenv, it returns:

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

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

I understand that in mac, the virtualenvshould be correctly installed in

我知道在 mac 中,virtualenv应该正确安装

/usr/local/bin

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

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

-bash: virtualenv: command not found

These are the PATHs I added to my .bash_profile

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

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

Any workarounds for this? Why is this the case?

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

回答by VertigoRay

Ensure that virtualenvis executable.

确保它virtualenv是可执行的。

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

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

回答by Bouramas

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

你说每次运行pip install你都会得到Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages。您需要做的是:

  1. Change Directory (go to to the one where the virtualenv.py) cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
  2. If you do an lsyou will see that the script is there virtualenv.py
  3. Run the script like this: python virtualenv.py --distribute /the/path/at/which/you/want/the/new/venv/at theNameOfTheNewVirtualEnv
  1. 更改目录(转到 virtualenv.py 所在的目录) cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
  2. 如果你这样做,ls你会看到脚本在那里virtualenv.py
  3. 像这样运行脚本: python virtualenv.py --distribute /the/path/at/which/you/want/the/new/venv/at theNameOfTheNewVirtualEnv

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

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

回答by vettipayyan

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

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

  1. The issue occurred to me because I installed virtualenv via pip as a regular user (not root). pip installed the packages into the directory ~/.local/lib/pythonX.X/site-packages
  2. When I ran pip as root or with admin privileges (sudo), it installed packages in /usr/lib/pythonX.X/dist-packages. This path might be different for you.
  3. virtualenv command gets recognized only in the second scenario
  4. So, to solve the issue, do pip uninstall virtualenvand then reinstall it with sudo pip install virtualenv(or install as root)
  1. 这个问题发生在我身上,因为我作为普通用户(不是 root)通过 pip 安装了 virtualenv。pip 将软件包安装到目录中~/.local/lib/pythonX.X/site-packages
  2. 当我以 root 用户或管理员权限 (sudo) 运行 pip 时,它会在/usr/lib/pythonX.X/dist-packages. 这条路对你来说可能会有所不同。
  3. 仅在第二种情况下才能识别 virtualenv 命令
  4. 因此,要解决该问题,请执行pip uninstall virtualenv并重新安装sudo pip install virtualenv(或以 root 身份安装)

回答by stray.leone

sudo apt-get install python-virtualenv

回答by Krystian Sakowski

I had same problem on Mac OS X El Capitan.

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

When I installed virtualenvlike that sudo pip3 install virtualenvI didn't have virtualenvunder my command line.

当我这样安装时virtualenv,我的命令行下sudo pip3 install virtualenv没有virtualenv

I solved this problem by following those steps:

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

  1. Uninstall previous installations.
  2. Switch to super user account prior to virtualenvinstallation by calling sudo su
  3. Install virtualenvby calling pip3 install virtualenv
  4. Finally you should be able to access virtualenvfrom both userand super useraccount.
  1. 卸载以前的安装。
  2. virtualenv安装前通过调用切换到超级用户帐户sudo su
  3. virtualenv通过调用安装pip3 install virtualenv
  4. 最后,您应该能够virtualenvusersuper user帐户访问。

回答by Tal Avissar

If you installed it with

如果你安装它

pip install virtualenv

You need to run

你需要跑

sudo /usr/bin/easy_install virtualenv

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

把它放在/usr/local/bin/.

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

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

回答by Pithikos

Figure out the problem

找出问题所在

Try installing with the --verboseflag

尝试使用--verbose标志安装

pip install virtualenv --verbose

Output will look something like this

输出看起来像这样

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

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

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

从输出中我们可以看到它安装在/home/manos/.local/bin/virtualenv所以让我们确保 PATH 包含它。

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

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

就我而言,我们可以清楚地看到它/home/manos/.local/bin完全丢失了,这就是 shell 找不到程序的原因。

Solutions

解决方案

We can solve this in many ways:

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

  1. We can install directly to a specific directory by fiddling with pip options (not recomended).
  2. Create appropriate symlinks at /usr/local/binor similar.
  3. Append /home/manos/.local/binto PATH.
  4. Install as sudo to install directly to /usr/local/bin
  1. 我们可以通过摆弄 pip 选项(不推荐)直接安装到特定目录。
  2. /usr/local/bin或类似处创建适当的符号链接。
  3. 附加/home/manos/.local/bin到 PATH。
  4. 安装为 sudo 直接安装到 /usr/local/bin

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

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

Add this to ~/.profile:

将此添加到 ~/.profile:

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

Logout out and in again and it should work.

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

回答by Bobby Wan-Kenobi

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

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

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

回答by Boris Davidov

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

我遇到了麻烦,因为我使用 apt 来安装 python-virtualenv 包。为了让它工作,我必须删除这个包apt-get remove python-virtualenv并用pip install virtualenv.

回答by user8224289

Follow these basic steps to setup the virtual env

按照这些基本步骤设置虚拟环境

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

we need to update our ~/.bashrc

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

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

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

~/.bashrc文件只是一个 shell 脚本,每当您启动新终端时,Bash 都会运行该脚本。您通常使用此文件来设置各种配置。在本例中,我们设置了一个环境变量WORKON_HOME,该变量被调用以指向我们的 Python 虚拟环境所在的目录。然后我们从 virtualenvwrapper 加载任何必要的配置。

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

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

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

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

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

source ~/.bashrc

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

现在我们已经安装了 virtualenv 和 virtualenvwrapper ,下一步是实际创建 Python 虚拟环境——我们使用 mkvirtualenv 命令来完成。

mkvirtualenv YOURENV