I have both python 2.7 and 3.8 and wanted to run virtualenv on 3.8. As the result of which python3 was /usr/local/bin/python3 I did virtualenv -p /usr/local/bin/python3 virEnv and then source virEnv/bin/activate to start the virtualenv. Now I switched to another terminal (on visual studio code) and ran workon virEnv and it gave workon:command not found. I searched for the solution on stackoverflow and found this thread workon:command not found and did
export WORKON_HOME=~/.virtualenvs
VIRTUALENVWRAPPER_PYTHON='/usr/local/bin/python3'
source /usr/local/bin/virtualenvwrapper.sh
But the third line is resulting in
/usr/local/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 and that PATH is
set properly
I want to work on my virtualenv on any terminal by running workon name. Please help.
New Python programmer here! 😊 Following the answer, I tried to make an executable .py file on MacOS following these steps:
Installed Homebrew via MacOS Terminal ($ /usr/bin/ruby-e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)")
Installed Python3 from Homebrew (brew install python3)
Installed required modules (e.g. python3.7 -m pip install pyfiglet)
Then, I created a simple hello_world.py file:
#!/usr/bin/env python
python import pyfiglet
print(pyfiglet.figlet_format("Hello world!"))
input("Press any key to exit.")
Next, following these instructions,
I renamed the file from hello_world.py to hello_world.command
From terminal, I entered $ cd Desktop/python to my python projects folder
Granted permission chmod +x hello_world.command to the file.
This made the python script executable, and double-clicking it opened a terminal window. However, I get the traceback ImportError: No module named pyfiglet.
I have confirmed pyfiglet is installed and works successfully when I paste the code directly into terminal using python3.7. Anyone know why the .command executable doesn't work? I followed these steps to the letter as described in this answer. Thanks for your help!
I have a issue where if a python 3 script's shebang line is /usr/bin/env python3 then script will be interpreted with /usr/bin/local/python3
type -P python3
/usr/local/bin/python3
So PATH python3 is /usr/local/bin/python3 but this creates problems where some packages are not available for /usr/local/bin/python3 and I would instead like to use /usr/bin/python3 as default env python3.
Using a alias to set python3 to /usr/bin/python3 can be done but this does not solve the issue. Using Virtualenv if fine but one can not create a virtualenv for every single litte script out there.
type -a shows that there are two python3 defined on my system so there must be a way to change the prefered one:
type -a python3
python3 is aliased to `/usr/bin/python3'
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
How can I change so that env python 3 is /usr/bin/python3?
Best regards
I solved the issue by editing /etc/environment. The change that I did was to specify to read /usr/bin before /usr/local/bin as was configured on my computer. After that I restarted my PC and now /usr/bin/python3 is the default env python.
How to install and configure virtualenv and virtualenvwrapper for Python on MacOS?
To install virtualenv and virtualenvwrapper for repetitive use you need a correctly configured Python (this example uses Python 3.x but process is identical for Python 2.x).
Although you can get python installer from Python website I strongly advice against it. The most convenient and future-proof method to install Python on MacOS is brew.
Main difference between installer from Python website and brew is that installer puts python packages to:
/Library/Frameworks/Python.framework/Versions/3.x
Brew on the other hand installs Python, Pip & Setuptools and puts everything to:
/usr/local/bin/python3.x/site-packages
And though it may not make any difference to you now – it will later on.
Configuration steps
Install brew
Check out brew installation page or simply run this in your terminal:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install Python
To install python with brew run:
brew install python3
Now your system needs to know where to look for freshly installed Python packages. Add this line to youre ~/.zshrc (or ~/.bash_profile if you're using bash):
export PATH=/usr/local/share/python:$PATH
Restart your terminal.
To make sure you've done everything correctly run which python3 and in return you should receive /usr/local/bin/python.
Install virtualenv & virtualenvwrapper
Now it's time to install virtualenv and virtualenvwrapper to be able to use workon command and switch between virtual environments. This is done using pip:
pip3 install virtualenv virtualenvwrapper
Set up virtualenv variables
Define a default path for your virtual environments. For example you can create a hidden directory inside ~ and called it .virtualenvs with mkdir ~/.virtualenvs. Add virtualenv variables to .zshrc (or .bash_profile).
Final version of your .zshrc (or .bash_profile) should contain this information to work properly with installed packages:
# Setting PATH for Python 3 installed by brew
export PATH=/usr/local/share/python:$PATH
# Configuration for virtualenv
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
Restart your terminal. You should be able to use mkvirtualenv and workon commands including autocompletion.
Here's a little tip on how to create virtualenv with specific
version of Python.
In case you are using MacOS Mojave and you are installing Python3.6
from brew bottle you might have a problem with pip, here's a
solution that might help.
With time some of you may want to install multiple Python versions with multiple virtual environments per version. When this moment comes I strongly recommend swithing to pyenv and pyenv-virtualenv .
I'm running macOS 10.15.7
I followed official docs until here
and change it to
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
# export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV=/Library/Frameworks/Python.framework/Versions/3.8/bin/virtualenv
# source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/3.8/bin/virtualenvwrapper.sh
in your case try to run which virtualenv or which python to get exact paths
Mac Big Sur
Python 3.8
installation
pip3 install virtualenv virtualenvwrapper
or
pip3 install virtualenv virtualenvwrapper --user
create directoty in your Home
mkdir .virtualenvs
edit profile
vi .bash_profile
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=/Users/{your_user}/Library/Python/3.8/bin/virtualenv
source /Users/{home_directory}/Library/Python/3.8/bin/virtualenvwrapper.sh
check path with 'which' command or 'find':
Reload .bash_profile
source ~/.bash_profile
If you are using MacOS
1.
Install virtualenvwrapper
pip3 install virtualenvwrapper
2.
Create the "virtualenvs" folder
mkdir ~/.virtualenvs
3.
Before you export this, make sure you are doing it in your project folder,
because that's where you will activate the virtualenv
Export
export WORKON_HOME=$HOME/.virtualenvs
next, write down in terminal which python3, to find the path and add it after the "=" sign
export VIRTUALENVWRAPPER_PYTHON=
in my case:
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
to find the virtualenv path write in the terminal which virtualenv
add the path after the "=" sign
export VIRTUALENVWRAPPER_VIRTUALENV=
in my case:
export VIRTUALENVWRAPPER_VIRTUALENV=/Library/Frameworks/Python.framework/Versions/3.9/bin/virtualenv
4.
Last step is to add the source in the terminal
almost the same as the path you got when writing which virtualenv
The difference is the "wrapper.sh" in the end.
in my case
source /Library/Frameworks/Python.framework/Versions/3.9/bin/virtualenvwrapper.sh
5.
Now you can create the virtualenv name by doing the following:
mkvirtualenv nameOfTheVirtualEnviroment
in my case
mkvirtualenv venv
On this Mozila page you can learn how to use it
I had many problems to install the virtualenvwrapper in my mac os (big sur) but after I spent my whole sunday on that I finally got it. What I did I do:
I reinstalled python3 using the homebrew. For some reason homebrew
put your files in a non conventional directories (for exemple:
/opt/homebrew/bin/python3 or /opt/homebrew/bin/virtualenv)
Than I installed the virtualenv (even its already installed in python3
by default) and I installed virtualenvwrapper in sudo mode: sudo pip3
install virtualenvwrapper (without sudo, I could't find the
virtualenvwrapper.sh using whichvirtualenvwrapper).
After that, in my home directory (/˜), I created the .bashrc file using
touch .bashrc because this file didn't existed in my home directory.
I used the command "which python3", "which virtualenv" and "which
virtualenvwrapper.sh" to get the localization of them (it's important to
get these location to fill the .bashrc file later). The result was:
/opt/homebrew/bin/python3
/opt/homebrew/bin/virtualenv
/opt/homebrew/bin/virtualenvwrapper.sh
Than, using vim editor, I wrote in this file like this:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_PYTHON=/opt/homebrew/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/opt/homebrew/bin/virtualenv
source /opt/homebrew/bin/virtualenvwrapper.sh
Than, I saved this file using :wq command and run it using this command:
source .bashrc
Than I testes using the writing "workon" command. It showed nothing so
it worked
Finally I create a virtual environment. It worked.
I made a lot of mistakes and I really hope I did't damage my OS.
The following solution worked for me. I have checked for the python path using which python3.8 in my MAC laptop with OS Big Sur (11.4)
/Users/apple/opt/anaconda3/bin/python3.8
Also checked for which virtualenvwrapper.sh
/Users/apple/opt/anaconda3/bin/virtualenvwrapper.sh
Updated my ~/.bash_profile with the above mentioned paths as shown below.
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/Users/apple/opt/anaconda3/bin/python3.8
source /Users/apple/opt/anaconda3/bin/virtualenvwrapper.sh
Then I was successfully able to source ~/.bash_profile.
this solution also works for version 11.1
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
# export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV=/Library/Frameworks/Python.framework/Versions/3.8/bin/virtualenv
# source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/3.8/bin/virtualenvwrapper.sh
Stan's solution required different .bash_profile modification for me:
(macOS Catalina & Python 3.6) Please make sure you change the version path from "..../3.6/..." to your version. You can use "which python3" command to find out python path and "which virtualenv" for virtualenv path
### BEGIN: for virtualenvwrapper #########################
# Setting PATH for Python 3 installed by brew
# commented the Python path because I have already had that, you might have too.
# PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin/python3:${PATH}"
# export PATH
# Configuration for virtualenv
WORKON_HOME="${HOME}/.virtualenvs"
export WORKON_HOME
VIRTUALENVWRAPPER_PYTHON="/Library/Frameworks/Python.framework/Versions/3.6/bin/python3"
export VIRTUALENVWRAPPER_PYTHON
VIRTUALENVWRAPPER_VIRTUALENV="/Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenv"
export VIRTUALENVWRAPPER_VIRTUALENV
source /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh
### END: for virtualenvwrapper #########################
I'm stuck trying to create a virtual environment. The instructions for this step are:
Create a new VirtualEnv in the directory for your Selenium files:
virtualenv -p /usr/bin/python3.0 venv
After checking my version of python I get: "Python 3.6.0 :: Anaconda 4.3.1 (x86_64)".
I tried replacing '3.0' with '3.6':
Wills-MacBook-Pro-2:my_selenium_project willdudek$ virtualenv -p /usr/bin/python3.6 venv
and get this error message:
The path /usr/bin/python3.6 (from --python=/usr/bin/python3.6) does
not exist
I also tried quitting terminal and reopening (so I'm not in my_selenium_project), importing virtualenv in Python, then trying to create a virtual environment using virtualenv my_selenium_project and that didn't work.
Do I need a different command since I'm running Python through Anaconda?