virtualenvwrapper commands aren't working

只谈情不闲聊 提交于 2019-11-28 02:54:57
chirinosky

1st, ensure you're installing with sudo:

sudo pip install virtualenvwrapper

2nd, append the following lines to your .bashrc file (with nano ~/.bashrc):

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

3rd, reload your profile

source ~/.bashrc
Will

Summary

I'm on a Mac and my answer is similar to @Ramces answer except it was with bash_profile. I just want to elaborate a little further for Mac users to be aware that there's a lot of different profiles including:

  • .bashrc
  • .bash_profile
  • .profile

Some files like .profile do not take precedence over .bash_profile (if it exists) and will then be ignored. If you successfully do the below steps and get a virtual env working, but then close out your terminal and 'workon command not found', then you need to setup for the correct profile. For a detailed answer, see here

Install Steps:

  1. sudo pip install virtualenv

    Installs virtualenv (allows you to separate your envrionments)

  2. sudo pip install virtualenvwrapper

    Installs virtualenvwrapper (allows you to use the 'workon' command)

  3. nano ~/.bash_profile

    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
    
  4. source ~/.bash_profile

    Reloads the profile. Going forward you only need step 5 (to create new environments) and step 6 (to run environments)

  5. mkvirtualenv my_env

    This creates your virtual environment (this example is with 'my_env')

  6. workon my_env

    This lets you work on a specific environment (this example is with 'my_env')

After installing the virtualenvwrapper package using pip, you also have to do some initialisation/set your preferences. See the introduction in the virtualenvwrapper docs.

Most relevant for finding the commands should be sourcing the virtualenvwrapper script into your shell. In the docs it is mentioned as

$ source /usr/local/bin/virtualenvwrapper.sh

You still have to adjust the path to your setup. My guess for your Mac would be:

$ source /Library/Python/2.7/site-packages/virtualenvwrapper.sh

Simple process

  1. sudo apt-get install python-pip(if pip is not installed)
  2. sudo pip install virtualenv
  3. Create a dir to store your virtualenvs

    mkdir ~/.virtualenvs

  4. sudo pip install virtualenvwrapper

  5. Run following command

    export WORKON_HOME=~/.virtualenvs

  6. Add virtualenvwrapper.sh to .bashrc Add this line to the end of ~/.bashrc so that the virtualenvwrapper commands are loaded.

    . /usr/local/bin/virtualenvwrapper.sh

  7. you will find .bashrc.sh file in home directory by doing ctrl+h. if not then use find command to find .bashrc.sh "file ls -la ~/ | more"

  8. Hit this command

    source /usr/local/bin/virtualenvwrapper.sh

  9. Hit this command

    source ~/.bashrc

It sounds like you have multiple Python installations on your machine and virtualenvwrapper is not pointing to the right Python.

Find out which Python virtualenvwrapper is using. You get a hint where to look with which virtualenvwrapper.sh (In this case /usr/local/bin):

> /usr/local/bin/virtualenvwrapper.sh

If you don't get any return here make sure you use the correct pip when installing. The pip command might link to a different Python then you expect. Check your usr/local/bin directory for pip links (pip, pip2, pip2.7, pip3, pip3.5). It is easy to get system pip, pip2 and pip2.7 mixed up.

After you have found the Python location, add/update all paths in your .profile:

export WORKON_HOME=$HOME/Envs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2
source /usr/local/bin/virtualenvwrapper.sh

Finally reload your profile: source ~/.profile

I am on Mac OS X 10.9.2 and for me virtualenvwrapper.sh file was present in

/usr/local/bin/virtualenvwrapper.sh

So I simply copied this into ~/.profile file:

source /usr/local/bin/virtualenvwrapper.sh

And now my ~/.profile file looks something like this:

# MacPorts Installer addition on 2014-02-23_at_17:28:39: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

source /usr/local/bin/virtualenvwrapper.sh

And now I am able to use virtualenvwrapper commands without any issue whatsoever

Users of the Anaconda (from Continuum) distribution of Python should note that

sudo pip install virtualenvwrapper

will be anaconda-aware. So if you

which python

that should give you an idea of where to point your virtualenv in your .bashrc and/or .profile configuration files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!