how to run virtualenv python on mac - python

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

Related

problems comes while creating virtualenv

I have installed python 3.10.2, while creating virtualenv
I did pip install virtualenv. then I create myenv (virtualenv mypython), but I got error like this. could you please help me what do I do
the error is :
C:\Users\ARROWIN PHOTOGRAPHY\Felix\djangoProject>virtualenv mypython
'virtualenv' is not recognized as an internal or external command,
operable program or batch file.
In python3 is better to use venv instead. Here are the steps to create your environment:
In your project directory, open the terminal:
python3 -m venv venv
Then:
source venv/bin/activate
Make sure that you have set up the python PATH variables correctly and that in your path e.g. ..\AppData\Local\Programs\Python\Python39\Scripts you can locate the python executable virtualenv.exe
If you are using multiple python versions on your local machine be careful while using PIP install. It is better to use the command pip3.x install ... (x is meant to be a specific version of pip e.g. pip3.9) so you can be sure you installed the virtualenv in the correct version of Python, and afterward, you can also select a specific python version when using virtualenv by virtualenv env -p pythonxx the xx is meant as a python version.

Using pip in virtualenv with a space in the filepath?

I'm working on OSX 10.13.3 with Python 2.7 installed. I want to make a virtualenv with Python 3 and Django 2.0. I have made a virtualenv as follows:
virtualenv -p /usr/local/bin/python3 .venv
It says Installing setuptools, pip, wheel...done..
Then I enter the virtualenv with:
source .venv/bin/activate
But when I try pip install Django I get:
-bash: /Users/me/Dropbox (Personal)/Projects/landapp/.venv/bin/pip: "/Users/me/Dropbox: bad interpreter: No such file or directory
I think this might be something to do with having a space in the path?
I need to use Dropbox for both business and personal, and I'd rather not rename the folders, as I suspect it'll cause other problems. Is there any way around this?

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.

virtualenv does not include pip

I am trying to create a virtual environment using virtualenv on Mac OS X El Capitan. I have installed Python 2.7.11 with brew, which includes pip, wheel and setuptools by default.
Hovewer, when I try to install virtualenv following instructions in the documentation or from any other resource, I get several problems:
virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
After I run virtualenv venv, it creates new environment, catches Python 2.7.11 from brew-installation, but: there is no pip inside bin folder. That means, that if I try which pip, having venv activated, it returns a global position of pip — /usr/local/bin/pip, not /path/to/venv/bin/pip.
As a consequence, installing packages inside venv uses global pip and installs them to a global sites-packages, not that inside venv, and it's quite the opposite of what environment should do.
Could you please suggest what may be wrong and how to fix it?
EDIT: The thing to mention is that I used to have other versions of Python installed on my computer, which I have recently deleted as it is described in this answer. Maybe it causes the issue, and some more thorough cleaning is needed.
Try removing or renaming the .pydistutils.cfg file in your home directory, e.g. by renaming with mv ~/.pydistutils.cfg ~/oldpydistutils.cfg
I'm putting a detailed answer here to help others, but the original credit goes to this answer. If you know what specifically in .pydistutils.cfg was causing the problem, let me know!
I was having the same issue: my virtual environments were created without a local copy of pip, although they had a local copy of python. This meant that using $ pip from within the virtual environment installed to the global package location, and was not visible to the environment's python.
How I diagnosed this on my machine:
I create a virtualenvironment with $ virtualenv env
Activated the virtual environment with $ source env/bin/activate
Checked python location: run (env)$ which python with output /Users/<username>/env/bin/python (as expected)
Checked pip location: run (env)$ which pip with output /usr/local/bin/pip (NOT expected)
To check where our packages are going, we can try to install a package in the virtual environment:
Try to install a package: (env)$ pip install HTTPServer which succeeds
Try to run the package: (env)$ python -m HTTPServer which fails with error /Users/emunsing/env/bin/python: No module named HTTPServer
To double-check, try to install again: (env)$ pip install HTTPServer which produces Requirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages
Double-checking, we see that there's no Pip in the environment's /bin folder:
$ ls env/bin
activate activate.fish python python2
activate.csh activate_this.py python-config python2.7
And so while the system finds the local python version, it can't find a local pip to use and traverses the $PATH. It ended up using pip from /usr/local/bin, leaving me unable to install packages locally to the virtual environment.
Here's what I tried:
- Reinstalling python brew uninstall python followed by brew upgrade and brew install python --build-from-source
- Installing pip using the get-pip.py command as described in the Pip documentation
Here's what I ruled out:
- I was not using sudo pip ... which caused similar problems in this other question and haven't done so at any time on this Python/pip install
- My virtual environment didn't show a local installation of pip, as was the case in these similar questions: This one for Windows, This one for Mac OS X.
Ultimately, I found that eliminating the ~/.pydistutils.cfg file fixed the problem, allowing for fresh virtual environments that had their own local pip. The contents of my ~/.pydistutils.cfg file were:
[global]
verbose=1
[install]
install-scripts=$HOME/bin
[easy_install]
install-scripts=$HOME/bin
Simply renaming the ~/.pydistutils.cfg file appears to fix the problem: it seems that although this file was created by the homebrew installation, some settings in this file may be incompatible with virtualenv. While removing this file hasn't had any bad effects on my system, you may need to use the --user flag when installing packages with pip to the global environment (e.g. $ pip install --user HTTPServer). Here are more details on .pydistutils.cfg if you want to work on tailoring it for your needs.
virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
Don't do that. That will only hide the bug and not solve the problem. Here's a short guide how to debug this kind of issues:
Start with which -a python. The first path you see should be /usr/local/bin/python, if not check your PATH variable.
Next, check which -a pip. Again the first path should be /usr/local/bin/pip. If not, run python -m ensurepip and recheck.
Now install virtualenv using pip install virtualenv, after that check the output of which -a virtualenv. The first path should be /usr/local/bin/virtualenv, if not check the output of env |grep PYTHON for unexpected environment variables.
Finally check the output of virtualenv --version to make sure you have the latest version.
I had the issue when running virtualenv: "ImportError: No module named pip."
My solution was to downgrade virtualenv. I had 16.2.0.
pip uninstall virtualenv
pip install virtualenv==15.1.0
Just hit same issue on Linux. Seems like there are multiple causes of this issue, but for me answer was to remove ~/.pip/.
Details: I had this in my .pip/pip.conf for some reason I can't remember:
$ cat ~/.pip/pip.conf
[global]
use_https = True
index = https://pypi.python.org/pypi
prefix = /home/sam/local/
and was using local versions on Python, Pip installed at ~/local/. For some reason virtualenv installed pip must pick up prefix = /home/sam/local/ setting and pip was being installed there.
Try this: sudo apt install pythonV.v-distutils.
In my case V.v == 3.8.
This worked for me.

Can't install via pip with Virtualenv

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.

Categories