mkvirtualenv: command not found

50,214

Solution 1

I added the following in my .bashrc, referring this

export PATH=/usr/local/bin:$PATH
source /usr/local/bin/virtualenvwrapper.sh

Now mkvirtualenv works-

pkoli@pkoli-SVE15136CNB:~/Desktop$ mkvirtualenv BUGS
Using base prefix '/usr'
New python executable in BUGS/bin/python3
Also creating executable in BUGS/bin/python
Installing setuptools, pip...done.

Solution 2

I ran into the same problem and I fixed it with by following the tutorial:

http://exponential.io/blog/2015/02/10/install-virtualenv-and-virtualenvwrapper-on-ubuntu/

Part 1: Install the required packages

sudo apt-get install python-pip python-dev build-essential

sudo pip install virtualenv virtualenvwrapper

sudo pip install --upgrade pip

Part 2: Setup virtualenvwrapper in ~/.bashrc.

I used vim to edit ~/.bashrc.

Firstly, install vim with the command:

sudo apt-get install vim

Secondly, edit ~/.bashrc

vi .bashrc

enter [Shift] + [g] //G (shift + g): to go the end of the file

enter [a] //Type a to edit the file

Then insert three lines:

export WORKON_HOME=~/virtualenvs

export VIRTUALENVWRAPPER_PYTHON=[link-to-python-folder]

source /usr/local/bin/virtualenvwrapper.sh

where [link-to-python-folder] can be, for example:

  • if you are using python 3,

export VIRTUALENVWRAPPER_PYTHON=/user/bin/python3

  • if you are using python 2,

export VIRTUALENVWRAPPER_PYTHON=/user/bin/python

Next, enter [Esc] then :wq

The Esc key means return the command line, :wq means save the changes and exit vi.

Part 3: Enable the virtual environment.

source ~/.bashrc

mkdir -p $WORKON_HOME

Now, you can try again with mkvirtualenv:

mkvirtualenv your_project

  • To enable the 'your_project' virtual environment:

    workon your_project

  • To exit it:

    deactivate

I noticed that when I installed separately virtualenv and virtualenvwrapper (with two times of 'sudo pip install'), I failed when run source ~/.bashrc because of a failure of the importing the module virtualenvwrapper.hook_loader, so although you really installed virtualenv and virtualenvwrapper, let try it one more time with sudo pip install virtualenv virtualenvwrapper.

Hope you find it useful!

Share:
50,214
aaragon
Author by

aaragon

Updated on January 28, 2020

Comments

  • aaragon
    aaragon over 4 years

    I'm new to Python virtual environments, so after reading this tutorial I tried to create my first environment using virtualenvwrapper. My python3 installation is at the bare bones now:

    $ pip3 list
    argparse (1.2.1)
    pip (1.5.6)
    setuptools (2.1)
    stevedore (0.15)
    virtualenv (1.11.6)
    virtualenv-clone (0.2.5)
    virtualenvwrapper (4.3.1)
    

    As suggested by the tutorial, I added the following lines to my .bashrc file:

    export WORKON_HOME=$HOME/.virtualenvs
    source /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper_lazy.sh
    

    which results in the following message when I open a new Terminal:

    Last login: Wed Sep 10 22:33:17 on ttys006
    -bash: _VIRTUALENVWRAPPER_API: unbound variable
    -bash: VIRTUALENVWRAPPER_SCRIPT: unbound variable
    -bash: VIRTUALENVWRAPPER_SCRIPT: unbound variable
    -bash: _VIRTUALENVWRAPPER_API: unbound variable
    -bash: _VIRTUALENVWRAPPER_API: unbound variable
    complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]
    

    I don't understand what the problem is, but clearly the source /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper_lazy.sh line fails because then I can't even find the mkvirtualenv command:

    $ mkvirtualenv test1 -p /usr/bin/python3.3
    -bash: mkvirtualenv: command not found
    

    I found this post and this one, with similar problems, but none of them gave me a solution.

    • Etan Reisner
      Etan Reisner over 9 years
      What else is in your .bashrc file?
    • aaragon
      aaragon over 9 years
      There's a lot of stuff in my .bashrc file, the result of many years of work of adding different commands and functions from here and there.
    • Etan Reisner
      Etan Reisner over 9 years
      Do you set -u in there?
    • aaragon
      aaragon over 9 years
      I do set -o notify, set -o noclobber, set -o ignoreeof, set -o nounset.
    • Etan Reisner
      Etan Reisner over 9 years
      Yeah, set -o nounset that's set -u. That's going to be the issue here. The preponderance of scripts out there aren't expecting to be run with that set as it makes many idiomatic actions fail. Disable that and see if it starts to work.
    • aaragon
      aaragon over 9 years
      I now got ERROR: virtualenvwrapper_lazy.sh: Could not find virtualenvwrapper.sh, and I guess it's because the virtualenvwrapper.sh script is not on a default path?
    • aaragon
      aaragon over 9 years
      Though it's recognizing the mkvirtualenv command now, but it doesn't seem to work: $ mkvirtualenv test1 -bash: : No such file or directory [Process completed]. After I see that message [Process completed] I can't do anything with the Terminal.
    • Etan Reisner
      Etan Reisner over 9 years
      That message is your shell exiting (and your terminal telling you the session is dead), presumably because of something mkvirtualenv did (probably wrongly). The PATH idea looks like it might be right (if the file I'm looking at is the one you have). I don't know what might be going wrong with mkvirtualenv specifically though.
    • Etan Reisner
      Etan Reisner over 9 years
      You don't have errexit on do you?
    • aaragon
      aaragon over 9 years
      I haven't set anything related to errexit in my .bashrc file, just that when I open a new Terminal window I get the message Last login: Wed Sep 10 23:25:04 on ttys000 ERROR: virtualenvwrapper_lazy.sh: Could not find virtualenvwrapper.sh.
    • Etan Reisner
      Etan Reisner over 9 years
      Adjust your PATH so which can find that script and see if that fixes everything?
    • aaragon
      aaragon over 9 years
      My PATH variable seems to have that directory already, 3 times... =/ and I'm not even adding that path in the .bashrc file. $ echo $PATH /Library/Frameworks/Python.framework/Versions/3.4/bin:/Libra‌​ry/Frameworks/Python‌​.framework/Versions/‌​3.4/bin:/Library/Fra‌​meworks/Python.frame‌​work/Versions/3.4/bi‌​n:/opt/local/bin:/op‌​t/local/sbin:/Librar‌​y/Frameworks/Python.‌​framework/Versions/3‌​.4/bin:/Users/aarago‌​n/Local/bin:/opt/loc‌​al/bin:/opt/local/sb‌​in:/usr/bin:/bin:/us‌​r/sbin:/sbin:/usr/lo‌​cal/bin:/opt/X11/bin‌​:/usr/local/MacGPG2/‌​bin:/usr/texbin. So I can do which virtualenvwrapper.sh and it finds it.
    • Etan Reisner
      Etan Reisner over 9 years
      Does it have it before you source virtualenvwrapper_lazy.sh?
    • aaragon
      aaragon over 9 years
      It was after, so I fixed that and now the command is working. Yet, I don't think I can create the environment yet: $ mkvirtualenv --python=/usr/local/bin/python3 pydev Running virtualenv with interpreter /usr/local/bin/python3 Using base prefix '/Library/Frameworks/Python.framework/Versions/3.4' New python executable in pydev/bin/python3 Also creating executable in pydev/bin/python Installing setuptools, pip...done. /usr/bin/python: No module named virtualenvwrapper
    • aaragon
      aaragon over 9 years
      That error shows up when I try to workon the environment: $ workon pydev /usr/bin/python: No module named virtualenvwrapper /usr/bin/python: No module named virtualenvwrapper. Well, I haven't installed virtualenvwrapper with python but only in python3.
    • Etan Reisner
      Etan Reisner over 9 years
      Does setting VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 fix that problem? I should point out that I am gathering all of this from looking at the scripts in question. I should also say that clearly something is wrong with either your setup or virtualenv itself if this much fiddling is required. It might make sense to find people who actually know/work with virtualenv to see what they make of all this.
    • aaragon
      aaragon over 9 years
      After setting that environmental variable things get even worst because now when I type workon pydev I get again a [Process completed] message and the Terminal doesn't work anymore.
    • Etan Reisner
      Etan Reisner over 9 years
      Like I said before [Process completed] means the shell that was being run in that window exited (in some way that didn't trigger the window to close, so perhaps an unclean exit). Also like I said, I think something is not quite right either with your system or with virtualenv itself but I'm not able to assess the virtualenv side of things and digging deeper into your system setup is an involved process. Sorry.
    • aaragon
      aaragon over 9 years
      I was finally able to make it work. I took my .bashrc file and I cleaned, removed everything I didn't need. Now it works fine, but I still think the process of making virtualenvwrapper work is painful, as you have to add stuff to your profile file.
    • Etan Reisner
      Etan Reisner over 9 years
      I will point out that there is a reason I asked what was in it at the start of all of this. =) I'm curious, if you are willing to share, what all you removed that might have been able to cause these myriad problems.
    • aaragon
      aaragon over 9 years
      Well, I went to this website, and I took many things from there, which I mixed with my old .bashrc. I spent the entire morning doing that, in the end, I tried virtualenvwrapper and it was working already, but I really don't know what fixed it. I could send you both versions of the .bashrc file if you want.
    • Etan Reisner
      Etan Reisner over 9 years
      Nothing in there looks overly harmful though the cd function and which alias could certainly cause some things problems.
    • aaragon
      aaragon over 9 years
      I recall the which function being called within the virtualenvwrapper_lazy.sh. But I don't think this was the problem because I still have that alias, and it now works.
  • caprica
    caprica almost 9 years
    For my Kubuntu it was installed here: /usr/share/virtualenvwrapper/virtualenvwrapper.sh
  • Feiteira
    Feiteira almost 5 years
    Note that on step 2, I needed to add an extra line: export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3