I added this to my .bash_profile
export WORKON_HOME= sudo $HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
Then it gives me this message
NOTE: Virtual environments directory /Users/rawad/.virtualenvs does not exist. Creating...
mkdir: /Users/rawad/.virtualenvs: Permission denied
What should I do to fix it? I tried a lot of things online, but nothing work.
As others pointed out, the code for .bashrc seems to be invalid. Please refer to the official virtualenvwrapper documentation. WORKON_HOME should refer to the base directory you want to store environments in (not a command). e.g.:
export WORKON_HOME=~/virtualenvs
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh
Note: I wouldn't recommend using virtualenvwrapper anymore. Just create relative virtual environments and activate them. e.g.:
$cd /path/to/python-project
$python -m venv .venv
$source .venv/bin/activate
Related
Pipenv virtual environnements (venv) will be shared with children folders.
So for example if you have installed an venv in ~/foo/, it will be accessible in ~/foo/baz/
But what if you want to share the same venv between ~/foo/bob/ and ~/baz/alice/ ?
The following kind of worked for me. I hope it can help.
There's a undocumented feature in pipenv: if you create a file named .venv in the project root with a path to a virtualenv, pipenv will use that instead of an autogenerated path.
(You'll still need to keep Pipfile's and Pipfile.locks synchronized. Making them symlinks as #MalikKoné suggests may do, but not if Pipfiles are under version control as they are supposed to.)
This, however, is more fit for cases when you already have an established set of environments that you wish to reuse. Otherwise, placing environments in arbitrary places is prone to create a mess eventually.
To share virutal env with pipenv
Create a directory ~/foo/bob/
mkdir -p ~/foo/bob/ ; cd ~/foo/bob/
create a virtual env in ~/foo/bob/
pipenv --three
This will create ~/.local/share/virtualenvs/bob-signature/
Install whatever packages you need. For example
pipenv install jupyter
This will create a Pipfile.lock in ~/foo/bob/
Create another directory, say ~/baz/alice/ and create a venv there
mkdir -p ~/baz/alice ; cd ~/baz/alice/ ; pipenv --three
As before pipenv will have created alice-signature/ in ~/.local/share/virtualenvs/.
Remove that folder and replace it by a link to bob-signature
cd ~/.local/share/virtualenvs/
rm -r alice-signature/
ln -s bob-signature/ alice-signature
In ~/baz/alice/, link Pipfile and Pipfile.lock to the ones in ~/baz/bob/
cd ~/baz/alice/ ;
rm Pipfile ; rm Pipfile.lock
ln -s ~/foo/bob/Pipfile . ; ln -s ~/foo/bob/Pipfile.lock .
Now, You should have a venv accessible from alice/ or bob/, and packages installed from any of those directories will be shared.
I would like to use virtualenvwrapper with Python 3.6, however, I am working on a Mac which defaults to using Python 2.7 and I am having issues. Here's what I have done so far. Using the following commands, I have found the locations of where each Python version is saved:
>> which python
>> /usr/bin/python
>> which python3
>> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
I have successfully installed virtualenv and virtualenvwrapper using:
>> pip3 install virtualenv
>> pip3 install virtualenvwrapper
I then search for the location of virtualenv and virtualenv wrapper to confirm their locations:
>> pip3 show virtualenv
>> Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
>> pip3 show virtualenvwrapper
>> Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
If I go to this site-packages directory there is the following:
virtualenvwrapper
|--- __pycache__
|--- hook_loader.py
|--- project.py
|--- user_scripts.py
virtualenvwrapper-4.8.2-py2.7-nspkg.pth
virtualenvwrapper-4.8.2.dist-info
According to virtualenvwrapper's documentation, I should add the following to my shell startup file to ensure initialization, changing virtualenvwrapper's path to the one set on my machine:
export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
I then searched for where virtualenvwrapper.sh is actually located (for some reason, it is not located where the virtualenvwrapper module is installed):
>> which virtualenvwrapper.sh
>> /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh
I changed my .bashrc file using:
>> nano ~./bashrc
Copied in the following:
# script for virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh
I then opened up a new terminal and ran:
>> source `which virtualenvwrapper.sh`
Which gave me:
/usr/bin/python3: No such file or directory
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/bin/python3 and that PATH is
set properly.
What I don't understand is why virtualenvwrapper needs to be located at /usr/bin/python3 which is not a directory. Since it is in bin, it is also impossible for me to make it a directory. Additionally, the virtualenvwrapper.hook_loader is not where virtualenvwrapper.sh is which I think may be causing an issue.
I also tried creating a symbolic link to /usr/local/bin because some people stated that it solved their issue but this didn't solve mine:
sudo ln /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh /usr/local/bin/virtualenvwrapper.sh
Another issue altogether is whether or not I need to install it using pip3 if I want to use virtualenvwrapper for Python3. For example, this post states that you only need to pass a flag, however, you should also be wary of installing it on your base Python installation.
Add
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
to .bashrc and execute the command in the terminal. That sets the path to Python interpreter for virtualenvwrapper.
I am trying to setup a new virtual env to work with django and flask.
installed
sudo pip install virtualenv
sudo pip install virtualenvwrapper
for some raisons, overlapping with anaconda.
This command doesn't work out.
virtualenv newThing
While this command is working out.
virtualenv -p /usr/bin/python2.7 newThing
What should I add to .bash_profile to make it working reguarly ?
That's probably the wrong question, as running a venv by default largely defeats the benefit of creating one.
To answer your question, though, you can enter a venv this way:
source newThing/bin/activate
Once you deploy this code to a server, you'll likely specify the venv to use in your WSGI conf.
If you have installed virtualenvwrapper as you say then you need to add some bits to your bash config:
# Virtualenv
source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME="$HOME/.virtualenvs"
This ensures you source the bash script for the wrapper commands to call in bash and sets the location to use to store and access your virtual envs.
Now to create a virtualenv you can run the wrapper commmand mkvirtualenv and then the name of your desired env.
Then to switch to that env to work on your project run workon and then the name of that env.
There are a bunch of other useful wrapper commands like setting your project directory for example - this is useful when you are switching between projects which use different env.
For this try activating an venv using workon and then cd to the working directory for the project and then running setvirtualenvproject - this then remembers that directory to switch to whenever you run workon for that venv.
I installed virtualenvwrapper using the following steps in Fedora 20:
$ sudo pip install virtualenvwrapper
$ export WORKON_HOME=$HOME/.virtualenvs
$ export PROJECT_HOME=$HOME/Devel
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv -p /usr/bin/python2 test
So far so good, but when I check the environment, test, that I created it included all the existing python dependencies on my system. Here's a small sample of what's included when the virtualenv is created:
$ pip freeze
tornado==2.2.1
urlgrabber==3.10.1
vboxapi==1.0
virtualenv==1.11.6
virtualenv-clone==0.2.4
virtualenvwrapper==4.3.1
I've never had this issue with Ubuntu so it makes me think the problem is related to how virutalenvwrapper is interacting with Fedora 20. Please let me know what you think.
EDIT: Included the virtualenv section of my .bashrc file after a suggestion to add --no-site-packages
# Virtualenvwrapper
alias pip="pip-python"
export WORKON_HOME=$HOME/virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
source /usr/bin/virtualenvwrapper.sh
I trying install virtualenvwrapper from your official guide http://virtualenvwrapper.readthedocs.org/en/latest/install.html
I install virtualenvwrapper with pip and when I define the environment variables and source
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
I reboot my shell and I get the following output.
bgarcial#el-pug:~$ bash
/usr/local/bin/python: 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 virtualenv has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is
set properly.
bgarcial#el-pug:~$
Despite this, the commands mkvirtualenv and others works. But .. What does this mean? How I can fix it? Thanks
What is the content of your ~/.bashrc file?
You may be calling "virtualenvwrapper" instead of "virtualenvwrapper.sh"
OR:
You may be exporting variables for the current shell that don't persist or propagate to subsequent shells.
The following script will install virtualenvwrapper and configure bash to persist the environment variables, which should in turn make virtualenvwrapper work as expected.
pip install virtualenvwrapper
configure_bashrc(){
echo '
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
' >> ~/.bashrc
source ~/.bashrc
}
[[ -z $(grep virtualenv ~/.bashrc) ]] && configure_bashrc