63

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$ 
jb.
  • 23,300
  • 18
  • 98
  • 136
shaytac
  • 3,789
  • 9
  • 36
  • 44

10 Answers10

129

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.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • 4
    I had the same error but no spaces, turns out what bit me is that you cannot move a virtualenv once created (which is utter madness…). – Alper Jun 25 '12 at 19:07
  • 1
    @alper: It [kind of supports](http://www.virtualenv.org/en/latest/index.html#making-environments-relocatable) moving them around. – icktoofay Jun 30 '12 at 05:09
  • 1
    Thanks, this explained everything for me! I guess this means you can't set up virtualenvs inside your Google Drive folder. – GChorn Mar 08 '13 at 07:41
  • 1
    @GChorn: It would be a bit of a hack, but you could set up a symbolic link in a path without spaces to the Google Drive folder. – icktoofay Mar 09 '13 at 06:14
  • @icktoofay, thanks for the suggestion. It's a small hack so I may go ahead and use it for the convenience of backing up my projects automatically. – GChorn Mar 12 '13 at 02:43
  • 1
    @GChorn symlink didn't work with iCloud (/Mobile documents/) :( – Paul Jan 25 '17 at 22:23
  • I am getting the same error but I don't have spaces in my path and I did not move the venv. Edit: fixed as per @Hedde van der Heide's answer, shortening the path to the venv. It was very long. – user5359531 Feb 27 '17 at 20:47
  • In my case, the path that was holding the virtualenv had spaces, so I fixed renaming the folder which had spaces in its name. – lerp90 Jun 28 '17 at 02:13
  • "Create your virtualenv environment within a path without spaces." Very very trivial but important point explained... I would have never worried to check if this was a "space in the path" issue. Saved me so much time and frustration... – Jayant Shelke Mar 01 '18 at 22:24
22

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.

Mohammed Shareef C
  • 3,829
  • 25
  • 35
  • 5
    You're a hero. It's kind of ridiculous that pip can't handle spaces AND doesn't specifically throw that error. – ProGirlXOXO Jul 08 '17 at 18:07
  • 3
    Ran into a long path issue. Thanks. – Igal Karlinsky Feb 05 '18 at 08:56
  • 1
    Does anyone know the `virtualenv` length limit? – Greg Hilston Apr 07 '18 at 15:39
  • 2
    Also ran into a length limit due to Jenkins workspace names. Character limit seemed to be 79 characters. CentOS7 AWS-hosted VM with kernel version 3.10.0-693.11.6.el7.x86_64 – Connor Jun 06 '18 at 04:08
  • I have to replace `python pip install packagename` with `./python pip install packagename` or it will end up using system python not the one in the virtualenv. I can't edit the answer because the edit is less than 6 characters. – kkpattern Jun 12 '19 at 07:07
  • @kkpattern If I got it right, it is the case you can't change or rename the virtualenv folder. I have edited the answer – Mohammed Shareef C Jun 13 '19 at 16:33
21

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

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • 9
    This isn't a bash constraint and in fact has nothing to do with the shells: it's a kernel constraint in the exec call on the length allowed to specify the interpreter (and args) following the "#!" magic number. Historically it was about 30 characters in older Unixen, in Linux it's closer to 80. – Alex North-Keys Jan 05 '16 at 21:47
  • :-) Now if we can just get people to Stop Putting .sh On Command Names. It's soooo wrong for so many reasons. – Alex North-Keys Jan 06 '16 at 16:19
  • 1
    Just to clarify, the kernel constraint is BINPRM_BUF_SIZE and the limit length is 127 bytes. More info: https://www.in-ulm.de/~mascheck/various/shebang/#length – Flat May 22 '17 at 14:21
14

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.

Bryan Head
  • 12,360
  • 5
  • 32
  • 50
  • @icktoofay, virtualenv changes the `PATH` defined by `env`. Thus, `/usr/bin/env python` will refer to the `python` defined by the virtualenv. – Bryan Head Mar 01 '14 at 03:24
  • You're right. That seems obvious now, but I missed that for some reason. – icktoofay Mar 01 '14 at 03:26
  • No worries. I've changed my answer to make it clearer. – Bryan Head Mar 01 '14 at 03:29
  • Thanks! Somewhere along a project setup process, I was told to change the folders' names, but I had already created the virtualenv. It was now no longer allowing me to use pip to install packages. Editing the bin/pip executable and fixing the path to the new name did the trick! :) – Luca Bezerra Dec 01 '16 at 20:11
2

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!

Bob de Graaf
  • 2,630
  • 1
  • 25
  • 43
1

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

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
JackChen255
  • 355
  • 2
  • 4
  • 12
0

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.

ellockie
  • 3,730
  • 6
  • 42
  • 44
0

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
0

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.

Yushan ZHANG
  • 523
  • 5
  • 18
-1

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.

Logan3629
  • 11
  • 1
  • 1
    Error clearly shows it's due to error in hashbang (it's been resolved). Please read thoroughly before posting a quick answer. – richardev May 23 '21 at 01:00