[Django]-Django import search path

3đź‘Ť

âś…

Three choices.

  1. Set PYTHONPATH environment variable to include your application’s directory. Be sure it has an __init__.py file.

  2. Create a .pth file in site-packages to point to your application’s directory.

  3. Install your application in site-packages.

These are the three ways of “installing” a Python module. Read about the site module for more information.

👤S.Lott

4đź‘Ť

Try appending the path to sys.path at runtime.

import sys
sys.path.append('/path/to/myapp')
👤rubayeet

3đź‘Ť

Add this in your .bashrc or .bash_profile

export PATH=$PATH:/path/to/django/bin

export PYTHONPATH=$PYTHONPATH:/path/to/myapp

0đź‘Ť

import from path containing dir with module (with __init__.py file)

example:

dm@batman:~/.local/15/lib/python2.7/site-packages $ pwd
/home/d/dm/.local/15/lib/python2.7/site-packages

dm@batman:~/.local/15/lib/python2.7/site-packages $ ls
django  Django-1.5.11-py2.7.egg-info

dm@batman:~/.local/15/lib/python2.7/site-packages $ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
>>> import sys
>>> sys.path.append("/home/d/dgaloc/.local/15/lib/python2.7/site-packages")
>>> import django
>>> django.VERSION
(1, 5, 11, 'final', 0)
👤dmgl

Leave a comment