0

I was studying a document on Django settings. It reminds us that the settings module should be on the Python import search path. So I added the following two lines to .bashrc as suggested in here:

-dell:~/Documents/DjangoTutorial$ tail -2 ~/.bashrc
export PYTHONPATH=$HOME/Documents/DjangoTutorial/mysite
export DJANGO_SETTINGS_MODULE=mysite.settings
-dell:~/Documents/DjangoTutorial$ . ~/.bashrc

and "django-admin runserver" is working from anywhere, with sys.path in Python3.7 showing:

>>> sys.path
['', '/home/Documents/DjangoTutorial/mysite', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']

Then I read article1 and article2 which imply that "sys.path" consists of : current dir.(denoted as ''), PYTHONPATH, standard and installed libraries. So I invoked django-admin strictly from the PYTHONPATH directory but commented out "#export PYTHONPATH" in ./bashrc and got an error as shown below:

-dell:~/Documents/DjangoTutorial/mysite$ django-admin runserver
Traceback (most recent call last):
...
**ModuleNotFoundError**: No module named 'mysite'

I then checked sys.path and '' was there:

-dell:~/Documents/DjangoTutorial/mysite$ python3
>>> sys.path
['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']

My question is that if '' is equal to PYTHONPATH in this case and why cannot it find the settings module?

Leon Chang
  • 669
  • 8
  • 12
  • I'm not sure if you know how bashrc works, but please ensure you restart your terminal every time you change it. It is run every time you start it. – IllustriousMagenta Nov 08 '18 at 06:31

1 Answers1

1

From sys.path definition and another post:

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

In my case,

-dell:~/Documents/DjangoTutorial/mysite$ django-admin runserver

where the file abspath is /usr/local/bin/django-admin and listed as such

sys.path = [/usr/local/bin, ...]

whereas CWD = ~/Documents/DjangoTutorial/mysite is not listed in the sys.path and thus the module "mysite.settings" under ~/Documents/DjangoTutorial/mysite cannot be found.

With

export PYTHONPATH=/home/user/Documents/DjangoTutorial/mysite 

it's working because now it is listed in:

sys.path = [/usr/local/bin, /home/user/Documents/DjangoTutorial/mysite, ...]

That is why manage.py is recommended over django-admin as its path is same as the export PYTHONPATH's and you can run it from anywhere even without setting $PYTHONPATH as it's duplicated below.

-dell:~/Documents/DjangoTutorial% mysite/manage.py runserver --noreload
sys.path = [
/home/user/Documents/DjangoTutorial/mysite # CWD = manage.py's path
/home/user/Documents/DjangoTutorial/mysite # export $PYTHONPATH's
...]
Leon Chang
  • 669
  • 8
  • 12