无法使用 Virtualenv 通过 pip 安装
以下是运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在不带空格的路径中创建 virtualenv 环境。这就是发生这种情况的原因:
当您创建环境时,它会设置一个
bin
目录。该bin
目录中包含与环境相关的所有可执行文件。有些是脚本。您可能知道,hashbang 用于告诉系统使用哪个解释器来运行脚本。您可能经常在脚本的顶部看到这一点:如果脚本位于
/tmp/test.py
,则告诉系统运行此命令来执行脚本:在您的情况下,virtualenv 正在创建像这样的脚本:
当系统尝试执行该脚本时,它将尝试使用参数
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 thatbin
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:If the script is at
/tmp/test.py
, that tells the system to run this command to execute the script:In your case, virtualenv is creating scripts like this:
When the system tries to execute that, it will try to execute the command
/tmp/oh
with the argumentsno/bin/python
and/tmp/test.py
./tmp/oh
does not exist, so it fails.如果出现以下情况,
pip
命令将不起作用:pip
,使用命令sudo apt-get install python-pip
或sudo apt-get install python3-pip
)如果由于某种原因,您无法重命名文件夹或更改路径,请转到
yourvirtualenvfolder/bin
(使用cd
命令),然后尝试./python pip install packagename.
pip
command won't work if:pip
on Ubuntu, use commandsudo apt-get install python-pip
orsudo apt-get install python3-pip
)If you can't rename folders or change path for some reason, goto
yourvirtualenvfolder/bin
(usingcd
command) and then try./python pip install packagename
.对于那些遇到这个问题的人,我发现路径的长度也可能会导致问题,而不使用任何空格(Ubuntu 12.04):
失败,虽然
工作得很好,请参阅下面亚历克斯的评论
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):
failed, while
worked just fine, see Alex's comment below
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 thepython
defined by the virtualenv.在 Python 3.7 上我没有遇到任何问题,但是当我不得不使用 Python 3.6 时我确实遇到了问题。我在 Github 上找到的最简单的解决方法是:
而不是:
我使用:
所以你实际上直接指向虚拟环境目录中的 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:
I use:
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!
我在 RedHat 中遇到了同样的错误。 Python 2.7.3是我自己配置和制作的。
解决方案:在
/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.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
我在我的 Windows 7 机器上遇到了非常类似的问题,并为此苦苦挣扎了几天。到我的 python 发行版和到我的 VE 的两条路径都有空格。几个月前,效果还不错。我在 virtualenv 网站上发现了以下注释:
以下步骤使我成功:
因此,至少 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:
The following steps lead me to success:
So at least the Anaconda (python) installation simple, non space-pollutted path was crucial. Maybe win32api installation was also important. Not sure.
当我遇到同样的问题时,我从谷歌搜索中发现了这个,并发现它非常有用。
virtualenv
现在有一个--relocatable
标志,它将把 shebang 命令重写为#!/usr/bin/env
。它确实有一些警告,因此请务必阅读文档以了解其含义:https://virtualenv.pypa.io/en/stable/userguide/#making-environments-relocatable
您需要使用与您相同的语法来重新定位 virtualenv创建时,否则可能会覆盖python版本。这将按预期工作...
而这将导致
#!/usr/bin/env python2.7
(至少在我的本地环境中)...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...
whereas this will result in
#!/usr/bin/env python2.7
(at least on my local environment)...对于我的情况,停用环境,
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 letvirtualenv
to update new information.如果是在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.