无法使用 Virtualenv 通过 pip 安装

发布于 2024-12-12 01:28:03 字数 1342 浏览 0 评论 0原文

以下是运行 pip 时出现的错误:

serkan$ rm -r mysite
serkan$ pwd
/Users/serkan/Desktop/Python Folder
serkan$ virtualenv mysite 
New python executable in mysite/bin/python
Installing setuptools............done.
Installing pip...............done.
serkan$ source mysite/bin/activate
(mysite)serkan$ pip install pinax
-bash: /Users/serkan/Desktop/Python Folder/mysite/bin/pip: "/Users/serkan/Desktop/Python: bad interpreter: No such file or directory
(mysite)serkan$ python pip install pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip install Pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip install Pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip install Pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip 
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ pip
-bash: /Users/serkan/Desktop/Python Folder/mysite/bin/pip: "/Users/serkan/Desktop/Python: bad interpreter: No such file or directory
(mysite)serkan$ pip install Pinax
-bash: /Users/serkan/Desktop/Python Folder/mysite/bin/pip: "/Users/serkan/Desktop/Python: bad interpreter: No such file or directory
(mysite)serkan$ 

Below is the error I get when I run pip:

serkan$ rm -r mysite
serkan$ pwd
/Users/serkan/Desktop/Python Folder
serkan$ virtualenv mysite 
New python executable in mysite/bin/python
Installing setuptools............done.
Installing pip...............done.
serkan$ source mysite/bin/activate
(mysite)serkan$ pip install pinax
-bash: /Users/serkan/Desktop/Python Folder/mysite/bin/pip: "/Users/serkan/Desktop/Python: bad interpreter: No such file or directory
(mysite)serkan$ python pip install pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip install Pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip install Pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip install Pinax
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ python pip 
python: can't open file 'pip': [Errno 2] No such file or directory
(mysite)serkan$ pip
-bash: /Users/serkan/Desktop/Python Folder/mysite/bin/pip: "/Users/serkan/Desktop/Python: bad interpreter: No such file or directory
(mysite)serkan$ pip install Pinax
-bash: /Users/serkan/Desktop/Python Folder/mysite/bin/pip: "/Users/serkan/Desktop/Python: bad interpreter: No such file or directory
(mysite)serkan$ 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

谈下烟灰 2024-12-19 01:28:03

在不带空格的路径中创建 virtualenv 环境。这就是发生这种情况的原因:

当您创建环境时,它会设置一个 bin 目录。该 bin 目录中包含与环境相关的所有可执行文件。有些是脚本。您可能知道,hashbang 用于告诉系统使用哪个解释器来运行脚本。您可能经常在脚本的顶部看到这一点:

#!/usr/bin/env python

如果脚本位于 /tmp/test.py,则告诉系统运行此命令来执行脚本:

/usr/bin/env python /tmp/test.py

在您的情况下,virtualenv 正在创建像这样的脚本:

#!/tmp/oh no/bin/python

当系统尝试执行该脚本时,它将尝试使用参数 no/bin/python/ 执行命令 /tmp/oh tmp/test.py/tmp/oh 不存在,因此失败。

Create your virtualenv environment within a path without spaces. This is why it is happening:

When you create an environment, it sets up a bin directory. In that bin directory are all the executables relating to the environment. Some are scripts. As you may know, hashbangs are used to tell the system what interpreter to use to run the script. You may see this at the top of scripts often:

#!/usr/bin/env python

If the script is at /tmp/test.py, that tells the system to run this command to execute the script:

/usr/bin/env python /tmp/test.py

In your case, virtualenv is creating scripts like this:

#!/tmp/oh no/bin/python

When the system tries to execute that, it will try to execute the command /tmp/oh with the arguments no/bin/python and /tmp/test.py. /tmp/oh does not exist, so it fails.

花想c 2024-12-19 01:28:03

如果出现以下情况,pip 命令将不起作用:

  • 您尚未在系统中安装 pip。(您必须先在系统中安装 pip,然后才能在 virtualenv 中使用它。在 Ubuntu 上安装 pip,使用命令 sudo apt-get install python-pipsudo apt-get install python3-pip)
  • 虚拟环境文件夹的路径包含空格(示例:/home/用户名/我的文件夹名称,带空格/newvirtualenv)
  • 虚拟环境文件夹的路径太长。 示例:/home/username/mytoobigpath/somefolder/anotherfolder/someanotherfolder/someanotherfolderagain/myvirtualenv。(尝试使用较小的名称重命名父文件夹)

如果由于某种原因,您无法重命名文件夹或更改路径,请转到 yourvirtualenvfolder/bin (使用 cd 命令),然后尝试 ./python pip install packagename.

pip command won't work if:

  • You have not installed pip in your system. (you have to install pip first in your system before you can use it in virtualenv. To install pip on Ubuntu, use command sudo apt-get install python-pip or sudo apt-get install python3-pip)
  • The path to your virtual environment folder contains space(s).(Example: /home/username/my folder name with spaces/newvirtualenv)
  • The path to your virtual environment folder is too long. Example: /home/username/mytoobigpath/somefolder/anotherfolder/someanotherfolder/someanotherfolderagain/myvirtualenv. (Try renaming parent folders with smaller names)

If you can't rename folders or change path for some reason, goto yourvirtualenvfolder/bin (using cd command) and then try ./python pip install packagename.

高速公鹿 2024-12-19 01:28:03

对于那些遇到这个问题的人,我发现路径的长度也可能会导致问题,而不使用任何空格(Ubuntu 12.04):

virtualenv /home/user/some/very/longer/path/without/spaces/etc/venv

失败,虽然

virtualenv /home/user/some/very/long/path/without/spaces/etc/venv

工作得很好,请参阅下面亚历克斯的评论

For those running into this issue, I discovered that the length of the path could cause issues as well, without using any spaces (Ubuntu 12.04):

virtualenv /home/user/some/very/longer/path/without/spaces/etc/venv

failed, while

virtualenv /home/user/some/very/long/path/without/spaces/etc/venv

worked just fine, see Alex's comment below

舞袖。长 2024-12-19 01:28:03

icktoofay 关于原因的描述是正确的。

要在带有空格的目录中将 pip 与 virtualenv 一起使用,请编辑 /path/to/env/bin/pip,将顶部的 shebang 替换为 #!/usr/bin/env python< /code> (或者 #!/usr/bin/env pypy 如果您使用的是 pypy)。

请注意,virtualenv 会更改您的环境,使得 /usr/bin/env python 引用 virtualenv 定义的 python

icktoofay is correct about the cause.

To use pip with virtualenv in a directory with spaces, edit /path/to/env/bin/pip, replacing the shebang at the top with #!/usr/bin/env python (or #!/usr/bin/env pypy if you're using pypy).

Note that virtualenv changes your environment such that /usr/bin/env python refers to the python defined by the virtualenv.

自控 2024-12-19 01:28:03

在 Python 3.7 上我没有遇到任何问题,但是当我不得不使用 Python 3.6 时我确实遇到了问题。我在 Github 上找到的最简单的解决方法是:

而不是:

pip install -r requirements.txt

我使用:

python env/bin/pip install -r requirements.txt

所以你实际上直接指向虚拟环境目录中的 pip 文件。当然,在尝试此操作之前,您需要先激活它。希望这对来这里的人有所帮助!

On Python 3.7 I didn't have any issues with this but when I had to use Python 3.6 I did have an issue. The most easy work-around I found on Github was this:

Instead of:

pip install -r requirements.txt

I use:

python env/bin/pip install -r requirements.txt

So you actually directly point to the pip file within your virtual environment directory. Of course you need to activate it first before trying this. Hope this helps someone who comes here!

您的好友蓝忘机已上羡 2024-12-19 01:28:03

我在 RedHat 中遇到了同样的错误。 Python 2.7.3是我自己配置​​和制作的。

[root@Ifx installer]# pip install Django
-bash: /usr/local/bin/pip: /usr/local/bin/python2.7: bad interpreter: Permission denied

解决方案:在 /usr/local/bin/pip 中,将第一行 #!/usr/local/bin/python2.7 替换为您实际的 Python 路径 # !/root/installer/Python-2.7.5/python

I got the same error in RedHat. Python 2.7.3 is configured and made by myself.

[root@Ifx installer]# pip install Django
-bash: /usr/local/bin/pip: /usr/local/bin/python2.7: bad interpreter: Permission denied

Solution: In /usr/local/bin/pip, replace first line #!/usr/local/bin/python2.7 with your actual Python path #!/root/installer/Python-2.7.5/python

一向肩并 2024-12-19 01:28:03

我在我的 Windows 7 机器上遇到了非常类似的问题,并为此苦苦挣扎了几天。到我的 python 发行版和到我的 VE 的两条路径都有空格。几个月前,效果还不错。我在 virtualenv 网站上发现了以下注释:

**Windows Notes**
[...] To create a virtualenv under a path with spaces in it on Windows, you’ll need the win32api library installed.

以下步骤使我成功:

  1. 确保我使用 pip 来安装 virtualenv 并且它是最新版本(pip-7.1.0)。结果:失败
  2. 安装 win32api。结果:失败(尽管在安装过程的最后出现了一些错误)。
  3. 尝试将我的 VE 安装在没有空格的路径中。结果:失败
  4. 将我的 Anaconda python 发行版重新安装到不包含“[”和“]”括号的路径。 VE 路径中有空格。结果:失败
  5. 将我的 Anaconda python 发行版重新安装到也不包含任何空格的路径。 VE 文件夹的路径中仍然有空格。结果:成功!

因此,至少 Anaconda (python) 安装简单,无空间污染的路径至关重要。也许win32api安装也很重要。没有把握。

I had a very similar issue on my Windows 7 machine and struggled couple of days with that. Both paths, to my python distribution and to my VE had spaces in it. Couple of months before it worked fine. I found the following note on virtualenv website:

**Windows Notes**
[...] To create a virtualenv under a path with spaces in it on Windows, you’ll need the win32api library installed.

The following steps lead me to success:

  1. Make sure I used pip to install virtualenv and it's the latest version (pip-7.1.0). Result: failure.
  2. Install win32api. Result: failure (although there was some error at the very end of installation process).
  3. Try to install my VE in a path without spaces. Result: failure.
  4. Reinstall my Anaconda python distribution to the path that didn't contain the "[" and "]" brackets. VE had spaces in the path. Result: failure.
  5. Reinstall my Anaconda python distribution to the path that also didn't contain any spaces. The VE folder still had spaces in the path. Result: success!

So at least the Anaconda (python) installation simple, non space-pollutted path was crucial. Maybe win32api installation was also important. Not sure.

薄荷→糖丶微凉 2024-12-19 01:28:03

当我遇到同样的问题时,我从谷歌搜索中发现了这个,并发现它非常有用。 virtualenv 现在有一个 --relocatable 标志,它将把 shebang 命令重写为 #!/usr/bin/env。它确实有一些警告,因此请务必阅读文档以了解其含义:

https://virtualenv.pypa.io/en/stable/userguide/#making-environments-relocatable

您需要使用与您相同的语法来重新定位 virtualenv创建时,否则可能会覆盖python版本。这将按预期工作...

virtualenv --python=python3.5 env-test
virtualenv --relocatable --python=python3.5 env-test

而这将导致 #!/usr/bin/env python2.7 (至少在我的本地环境中)...

virtualenv --python==python3.5 env-test
virtualenv --relocatable env-test

I found this from a Google search while experiencing the same problem and found this to be very useful. virtualenv now has a --relocatable flag that will rewrite the shebang command to #!/usr/bin/env <the_python_version_you_used_to_create_the_virtualenv>. It does come with some caveats, so be sure to read the documentation to understand the implications:

https://virtualenv.pypa.io/en/stable/userguide/#making-environments-relocatable

You need to use the same syntax to relocate the virtualenv as you did when creating it, otherwise the python version may be overwritten. This will work as expected...

virtualenv --python=python3.5 env-test
virtualenv --relocatable --python=python3.5 env-test

whereas this will result in #!/usr/bin/env python2.7 (at least on my local environment)...

virtualenv --python==python3.5 env-test
virtualenv --relocatable env-test
戒ㄋ 2024-12-19 01:28:03

对于我的情况,停用环境,source bin/activate 再次起作用。

看来我的文件夹内容与 virtualenv 生成的子文件夹名称相同,例如 bin、lib 等。复制到我的文件后,重新激活环境让 virtualenv 更新新的信息。

For my case, deactivate the environment and source bin/activate again works.

It seems my folder content has same subfolder names as generated by virtualenv, such as bin, lib etc. And after copy in my files, reactivate the environment let virtualenv to update new information.

ˇ宁静的妩媚 2024-12-19 01:28:03

如果是在windows上,则可能是由于dll更改(由其他软件)引起的
安装 openSSL 可以解决这个问题。
https://slproweb.com/products/Win32OpenSSL.html

它会自动将 dll 更新为其较新的版本。

If it's on windows, it can be caused due to dll changes(by other software)
Installing openSSL would fix it.
https://slproweb.com/products/Win32OpenSSL.html

It will automatically renew dll to its newer versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文