sys.path doesn't include PYTHONPATH (must type full path to run django-admin startproject)

561 views Asked by At

I'm running Windows 8.1, Python 3.4.2, Django 1.7.

.py files are associated with python.exe.

Windows system path is:

C:\lots_of_paths;C:\Python34;C:\Python34\Lib\site-packages\Django-1.7.1-py3.4.egg\django\bin

(total of 531 characters)

PYTHONPATH variable in registry is:

C:\Python34\Lib;C:\Python34\DLLs;C:\Python34\Lib\site-packages\Django-1.7.1-py3.4.egg\django\bin

(total of 96 characters)

I pasted the Django path when setting this variable, to avoid typos. When I copy and paste from PYTHONPATH to notepad there is not a break in the line, so there appears to be no hidden carriage return.

I start cmd.exe and work from the directory C:\projects.

When I enter python -c "import sys;print(sys.path)"

The result is:

['', 'C:\Python34\lib\site-packages\django-1.7.1-py3.4.egg', 'C:\WINDOWS\S YSTEM32\python34.zip', 'C:\Python34\DLLs', 'C:\Python34\lib', 'C:\Python34 ', 'C:\Python34\lib\site-packages']

\django\bin is being clipped from the path.

When I enter python django-admin.py startproject please_work

...it prints: python: can't open file 'django-admin.py': [Errno 2] No such file or directory

It appears that system path locates Python, but PYTHONPATH doesn't work, as expected.

When I type django-admin startproject saving_time I get this:

Usage: django-admin.py subcommand [options] [args]

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output

(etc. etc. etc.)

...so it appears that django-admin.py is found on the system path and ran via association with python.exe, but the arguments are not passed. I get the same result when the .py suffix is included in the command as suggested in one answer below: django-admin.py startproject testproject

Typing python C:\Python34\Lib\site-packages\Django-1.7.1-py3.4.egg\django\bin\django-admin.py startproject wasting_time functions properly, but it shouldn't be necessary, right?

3

There are 3 answers

1
Steve Barnes On BEST ANSWER

Since you do have a command line solution that works why not create a batch file in C:\Python34\Scripts possibly called dj-admin.bat which includes either your full command with parameters, if this is the command that you do all of the time, or allows you to use parameters.

By the way, as far as I know, when you type:

python somefile.py [parameters]

Python expects somefile.py to be either in the local directory or an absolute path it doesn't search PYTHON_PATH for it but if you type:

python -m somemodule [command_from_module] [parameters] 

Python will look first in the current directory then on PYTHON_PATH.

The other possible source of your problem is having more than one django-admin on your path - if it is earlier in the path it will take precedence. On Unix/Linux systems there is a lovely command called which which will tell you which commands are being found.

A python implementation of which is quite simple to do.

1
nishparadox On

django-admin.py startproject testproject should be enough

since django-admin.py resides on systempath

I suggest you to use virtual environments instead of globalizing the packages

0
codingcoding On

This problem was a result of downloading and installing Django from the .egg compressed file instead of using pip.

I'm not sure how it traces to the source, but that was the problem. When Django was installed using pip, this problem disappeared.

Warning: The remainder of this text is the type of emotional human expression upon which stackoverflow so scornfully frowns, the sharing of which I cannot resist.

It has been months since I stumbled upon the solution and I just now remembered to update this thread.

Thank you to Steve Barnes for teaching me to think outside of the box and find a temporary solution. Thank you to Nishan-Paradox for adding that suggestion to use virtual environments. Suggestions like those are really valuable to a newcomer, even if they don't pertain to the question. Common sense is still in vogue.

A side note: I am now using pip and virtual environments religiously, and life is much better :) To any other newbies who may read this: If you see many others using a tool, try it out before you decide if you want to use it or not. A Google search may not give you enough information to make the decision, but experience will. You may be delighted to try something new and add it to your toolbox.