Can't install via pip with Virtualenv - python

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$

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.

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.

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

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.

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!

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

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:
Make sure I used pip to install virtualenv and it's the latest version (pip-7.1.0). Result: failure.
Install win32api. Result: failure (although there was some error at the very end of installation process).
Try to install my VE in a path without spaces. Result: failure.
Reinstall my Anaconda python distribution to the path that didn't contain the "[" and "]" brackets. VE had spaces in the path. Result: failure.
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.

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

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.

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.

Related

Failure to detect Python path to install packages on Ubuntu

I install a python package (like virtualenv and django )in my ubuntu and it cant find the location in my ubuntu path.
What can i do for it ?
for example i write pip install virtualenv
and then my terminal has answer
WARNING: The script virtualenv is installed in '/home/famirqfr/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
This has nothing to do with your Python path. (which you can find using which python)
The warning says that virtualenv binary is at that directory
is installed in '/home/famirqfr/.local/bin' which is not on PATH.
To fix this, edit your ~/.bashrc
Add/ensure you have a line like
export PATH="$PATH:$HOME/.local/bin"
or...
if you prefer to suppress this warning, use --no-warn-script-location
This is really only useful if you want to run virtualenv as its own command rather than like python -m virtualenv new_env
Alternatively, Python3 comes with venv pre-installed, so you don't need to separately install virtualenv
python -m venv new_env

how to run virtualenv python on mac

I am trying to use virtualenv to create a virtual python environment on my mac. I have downloaded virtualenv however I can't run it because it can't find the path to my installation of python3 even though I am supplying the correct path. Here is the command I have run and the response:
virtualenv --python=/usr/local/bin/python3 newfolder
zsh: /usr/local/bin/virtualenv: bad interpreter: /usr/local/opt/python3/bin/python3.6: no such file or directory
Also I have tried running the command with quotes like so:
virtualenv --python='/usr/local/bin/python3' newfolder
zsh: /usr/local/bin/virtualenv: bad interpreter: /usr/local/opt/python3/bin/python3.6: no such file or directory
Please note I am supplying the correct path to python3 as far as I can tell.
Here is what I get when I run which python3
which python3
/usr/local/bin/python3
Also virtualenv appears to be correctly installed. Here is evidence for this:
pip3 install virtualenv
Requirement already satisfied: virtualenv in /Users/mathewlewis/Library/Python/3.7/lib/python/site-packages (16.7.9)
Also, in case this is relevant, the software I have is currently mac os catalina 10.15.2
Not only would I like a solution (as has been given at this point) I would also like a reason why this didn't work.
Try:
python3 -m venv venv
source ./venv/bin/activate
Your virtualenv script uses bad interpreter /usr/local/opt/python3/bin/python3.6 which you have had installed and later removed. To fix the script reinstall virtualenv package using an existing Python:
pip3 install -U virtualenv

I set up a virtual env to download the pipenv package. How should I add the resulting directory to PATH?

I installed Python 3.7 in my MacBook Air (Mojave 10.14). First, I got “Command line tools”, then Homebrew and finally python 3.7. I followed the instructions I found on: https://docs.python-guide.org/starting/install3/osx/#install3-osx
The problem started when I wanted to install Pipenv. In my first attempt on Terminal, I received a message saying: “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied”. After reading, I discovered that the best solution was to create a virtual environment to download pipenv. So I wrote the commands:
python3 -m venv env
source ./env/bin/activate
python -m pip install -- user pipenv
It worked but seems that I need to add this directory to PATH.I received the following answers on Terminal.
The script virtualenv is installed in '/Users/marianasierra/Library/Python/3.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
The script virtualenv-clone is installed in '/Users/marianasierra/Library/Python/3.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
The scripts pipenv and pipenv-resolver are installed in '/Users/marianasierra/Library/Python/3.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
I tried to add the directory to PATH, but I don’t think is working, since I’m still seeing the (env) on Terminal. What should I do in this case? How this especific directory should be added to PATH?
My last commands were:
(env) Marianas-MacBook-Air:~ marianasierra$ python -m site --user-base /Users/marianasierra/Library/Python/3.7
(env) Marianas-MacBook-Air:~ marianasierra$ export PATH="$PATH:/Users/marianasierra/Library/Python/3.7/bin"
But why you should do such a thing? Then you may confront issues
export PATH=$(which pipenv):$PTTH

Why I can't install the dependencies for the Flask application? [duplicate]

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$
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.
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.
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
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.
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!
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
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:
Make sure I used pip to install virtualenv and it's the latest version (pip-7.1.0). Result: failure.
Install win32api. Result: failure (although there was some error at the very end of installation process).
Try to install my VE in a path without spaces. Result: failure.
Reinstall my Anaconda python distribution to the path that didn't contain the "[" and "]" brackets. VE had spaces in the path. Result: failure.
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.
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
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.
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.

Installing python packages with no installation directory acces and no pip/easy_install/virtual_env

At work we have python installed, but no additional modules. I want to import some scipy modules but I have no access to the python directory for installation.
Similar questions have been asked on StackOverflow, but the answers always assumed easy install, pip or virtualenv were installed. At my workplace, none of these packages are installed. It's just the plain python installation and nothing else.
Is there still an option for me for installing modules in my local folder and calling them from python? If so, how do I go about it?
Not exactly installing modules on your local folder, but a solution nonetheless:
I used to work for a company that used windows and didn't have admin access, so I ended up using Portable python.
It seems portable python is no longer mantained, but you can see some other portable python solutions on their site, most of which you can run straight from your usb.
You can download pip from here http://pip.readthedocs.org/en/stable/installing/ and install it without root privileges by typing:
python get-pip.py --user
This will install to directory with prefix $HOME/.local so the pip executable will be in the directory $HOME/.local/bin/pip, for your convenience you can add this directory to $PATH by adding to end of .bashrc file this string
export PATH=$HOME/.local/bin/:$PATH
After this you can install any packages by typing
pip install package --user
Or you can alternatively compile the python distribution from source code and install to your home directory to directory $HOME/.local or $HOME/opt or any subfolder of $HOME you prefer, let's call this path $PREFIX. For doing this you have to download python source code from official site, unpack it and then run
./configure --prefix=$PREFIX --enable-shared
make install
And then add python binary to $PATH, python libraries to $LD_LIBRARY_PATH, by adding to the end of $HOME/.bashrc file whit strings
export PATH=$PREFIX/bin:$PATH
export LD_LIBRARY_PATH=$PREFIX/lib
and when after restarting bash you can also run
python get-pip.py
and pip and will be installed automatically to your $PREFIX directory. And all other packages those you will install with pip will be automatically installed also to $PREFIX directory. This way is more involved, but it allows you to have the last version of python.

Categories