17

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.

Community
  • 1
  • 1
aaragon
  • 2,314
  • 4
  • 26
  • 60
  • What else is in your `.bashrc` file? – Etan Reisner Sep 10 '14 at 21:01
  • 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. – aaragon Sep 10 '14 at 21:03
  • Do you `set -u` in there? – Etan Reisner Sep 10 '14 at 21:04
  • I do `set -o notify`, `set -o noclobber`, `set -o ignoreeof`, `set -o nounset`. – aaragon Sep 10 '14 at 21:06
  • 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. – Etan Reisner Sep 10 '14 at 21:10
  • 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 Sep 10 '14 at 21:12
  • 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. – aaragon Sep 10 '14 at 21:14
  • 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 Sep 10 '14 at 21:24
  • You don't have `errexit` on do you? – Etan Reisner Sep 10 '14 at 21:25
  • 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`. – aaragon Sep 10 '14 at 21:26
  • Adjust your PATH so `which` can find that script and see if that fixes everything? – Etan Reisner Sep 10 '14 at 21:29
  • 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:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/aaragon/Local/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/MacGPG2/bin:/usr/texbin`. So I can do `which virtualenvwrapper.sh` and it finds it. – aaragon Sep 10 '14 at 21:41
  • Does it have it *before* you source `virtualenvwrapper_lazy.sh`? – Etan Reisner Sep 10 '14 at 21:45
  • 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 Sep 10 '14 at 21:53
  • 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`. – aaragon Sep 10 '14 at 21:56
  • 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. – Etan Reisner Sep 10 '14 at 22:25
  • 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. – aaragon Sep 11 '14 at 06:51
  • 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. – Etan Reisner Sep 11 '14 at 13:09
  • 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. – aaragon Sep 11 '14 at 13:26
  • 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. – Etan Reisner Sep 11 '14 at 13:43
  • Well, I went to [this website](http://natelandau.com/my-mac-osx-bash_profile/), 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. – aaragon Sep 11 '14 at 13:49
  • Nothing in there looks overly harmful though the `cd` function and `which` alias could certainly cause some things problems. – Etan Reisner Sep 11 '14 at 14:06
  • 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. – aaragon Sep 11 '14 at 14:09

3 Answers3

33

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.
pkoli
  • 646
  • 8
  • 21
5

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!

Nguyen Xuan
  • 51
  • 1
  • 4
  • 1
    Note that on step 2, I needed to add an extra line: ``export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3`` – Feiteira Jun 27 '19 at 20:46
0
export WORKON_HOME=~/Virtualenvs
export PROJECT_HOME=~/Devel
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
source /usr/local/bin/virtualenvwrapper.sh

If you have a clean installation of virtualenv and virtualenvwrapper, then this should work in mac OS. It did for me.

akkhil
  • 359
  • 4
  • 14