Homunculus Reticulli Homunculus Reticulli - 20 days ago 4
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/foo_project.foo$ python django-admin.py startproject mysite
python: can't open file 'django-admin.py': [Errno 2] No such file or directory
(foo) memyself@somebox:~/path/to/foo_project.foo$


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/foo_project.foo$ 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/foo_project.foo$ pip show django
Name: Django
Version: 1.10.3
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: http://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: /home/memyself/.virtualenvs/foo/lib/python3.5/site-packages
Requires:


(foo) memyself@somebox:~/path/to/foo_project.foo$ which django-admin.py
/home/memyself/.virtualenvs/foo/bin/django-admin.py


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 djan-admin.py startproject xxx
after creating a new virt environment using virtualenvwrapper?

Answer

You don't need to run python django-admin.py (in fact you shouldn't), just run django-admin.py. python isn't searching your $PATH; it's going to look for the file in the current directory. So long as django-admin.py 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 foo.py, the command is python, and $PATH is searched for that. foo.py is just a command argument, and your shell doesn't look for that at all. When python runs, it looks for foo.py in the current directory (and also accepts a relative path like ../otherdir/foo.py or an absolute path like /home/someone/foo.py); it doesn't care about $PATH at all.