view raw
Homunculus Reticulli Homunculus Reticulli - 6 months ago 32
Python Question

Why is virtualenvwrapper not adding my environment location to my $PATH

I am trying to create a django project, using virtualenvwrapper to isolate/sandbox different projects.

Here are the commands I typed so far ...

memyself@somebox:~/path/to/foo_project$ mkvirtualenv foo
Using base prefix '/usr'
New python executable in /home/memyself/.virtualenvs/foo/bin/python3
Also creating executable in /home/memyself/.virtualenvs/foo/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/memyself/.virtualenvs/foo/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/memyself/.virtualenvs/foo/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/memyself/.virtualenvs/foo/bin/preactivate
virtualenvwrapper.user_scripts creating /home/memyself/.virtualenvs/foo/bin/postactivate
virtualenvwrapper.user_scripts creating /home/memyself/.virtualenvs/foo/bin/get_env_details

I then installed django in the foo env:

(foo) memyself@somebox:~/path/to/foo_project$ pip install django
Collecting django
Using cached Django-1.10.3-py2.py3-none-any.whl
Installing collected packages: django
Successfully installed django-1.10.3

I then proceed to attempt to create a django website by entering the following commands:

(foo) memyself@somebox:~/path/to/$ python startproject mysite
python: can't open file '': [Errno 2] No such file or directory
(foo) memyself@somebox:~/path/to/$

Not believing what I was seeing, I decide to run a few checks, to make sure I'm not going mad ...

(foo) memyself@somebox:~/path/to/$ pip list --format=legacy
Django (1.10.3)
pip (9.0.1)
setuptools (28.8.0)
wheel (0.29.0)

(foo) memyself@somebox:~/path/to/$ pip show django
Name: Django
Version: 1.10.3
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Author: Django Software Foundation
License: BSD
Location: /home/memyself/.virtualenvs/foo/lib/python3.5/site-packages

(foo) memyself@somebox:~/path/to/$ which

I'm using a simple heuristic that the guy(s?) who created and maintain virtualenvwrapper is(are) much smarter than I am - however, not (automagically) adding the newly created environment to the path, reeks of incompetence bordering on malice.

It is highly unlikely (no, impossible) that the developer/mainteners are that incompetent - which leaves the only possible explanation to be that I'm missing something obvious and doing something foolish.

SO (pun unintended) - WHAT AM I DOING WRONG?!

Why can't I simply execute
python startproject xxx
after creating a new virt environment using virtualenvwrapper?


You don't need to run python (in fact you shouldn't), just run python isn't searching your $PATH; it's going to look for the file in the current directory. So long as has its execute bit set, you can use it as a command without explicitly running python.

It also installs itself as django-admin, so you can make the command line a little cleaner:

django-admin startproject mysite

You don't need the .py extension.

The underlying reason is that virtualenv modifies $PATH, which is what your shell (bash or whatever) uses to locate the command to execute. When you run python, the command is python, and $PATH is searched for that. is just a command argument, and your shell doesn't look for that at all. When python runs, it looks for in the current directory (and also accepts a relative path like ../otherdir/ or an absolute path like /home/someone/; it doesn't care about $PATH at all.