Mitchell van Zuylen Mitchell van Zuylen - 1 month ago 11
Python Question

Manage.py is added to PYTHONPATH, but can't be found

In my, apparently faulty, understanding of PYTHONPATH, calling

sys.path
returns the contents of PYTHONPATH. Using the windows GUI, I added a script -manage.py, from Django- to it.

>>> import sys
>>> sys.path
['', 'C:\\programming\\django_testing\\tango_with_django_project\\manage.py', 'C:\\Users\\mjpvanzuijlen\\AppData\\Local\\Programs\\Python\\Python36', ]


I though this would allow me to call it in CMD from whatever directory using:

C:\>python manage.py


Yet, this returns an error:

python: can't open file 'manage.py': [Errno 2] No such file or directory.


Clearly my understanding of PYTHONPATH is wrong. How would I be able to call a file, in this case
manage.py
from any location in CMD?

EDIT:
I changed the path from
.../manage.py
to the directory. It still returns the same error.

>>> import sys
>>> sys.path
['', 'C:\\programming\\django_testing\\tango_with_django_project',

Answer Source

How would I be able to call [..] manage.py from any location in CMD?

You can't. manage.py needs to be called from the directory where it's located. It wouldn't make sense otherwise, as amanage.py files exist for each django project, which would easily go wrong.

This is also true in the general case: You can't call a .py file from outside the directory. Only modules can be called this way.

Information mostly from Klaus D. in the comments.