Reputation: 1569
I use django 1.3.0 under Windows XP and using Python 2.7 ...
This is my try to add path django application on sys.path
C:\nginx-1.0.5\html\mysite>python
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', 'C:\\Python27\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C
:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\P
ython27\\lib\\site-packages']
>>> sys.path = ['C:\\nginx-1.0.5\\html\\mysite'] + sys.path
>>> print sys.path
['C:\\nginx-1.0.5\\html\\mysite', '', 'C:\\Python27\\python27.zip', 'C:\\Python2
7\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib
\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
>>> import testapp
>>> ^Z
C:\nginx-1.0.5\html\mysite>python manage.py runserver 8080
Error: No module named testapp
C:\nginx-1.0.5\html\mysite>python
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', 'C:\\Python27\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C
:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\P
ython27\\lib\\site-packages']
>>>
The django application is on folder:
C:\nginx-1.0.5\html\mysite\testapp
I create this with :
C:\nginx-1.0.5\html\mysite>python manage.py startapp testapp
Where is the error.
Thank you. Regards.
Upvotes: 1
Views: 3653
Reputation: 48730
I think you might have a bit of a problem understanding how imports work with regards to PYTHONPATH. PYTHONPATH is the list of paths from sys.path.
Any module that is directly included in the paths listed in sys.path is available as a top level import. If you have packages (folders), they must contain an __init__.py
file in them to be importable, unless they are directly on the PYTHONPATH.
Now, along with the default paths that are included in sys.paths, the path that the first python file executed from is included also. For example:
c:\myapp\my_app.py
\my_module.py
> python my_app.py
>>> import sys
>>> sys.path
...,"c:\myapp\"...
In the context of django, it means that whatever directory your manage.py file resides in, is also included on your PYTHONPATH when you run a manage command. This seems to be important in your case, because it would appear that whatever folder you have your manage.py file in, does not directly include your testapp directory.
/path/to/project/
manage.py
settings.py
mysite/
__init__.py
testapp/
__init__.py
models.py
In the directory structure above, if you run the command:
python /path/to/project/manage.py shell
>>> import mysite.testapp
>>> import testapp
--- failure ---
The reason being that testapp is not included directly in any of the paths listed in sys.paths. However, mysite is.
You could change your directory structure like so:
/path/to/mysite/
manage.py
settings.py
__init__.py
testapp/
__init__.py
models.py
python /path/to/mysite/manage.py shell
>>> import testapp
>>> import mysite.testapp
--- failure ---
And now testapp is directly on sys.path, and can be imported as a top level module.
Upvotes: 1
Reputation: 1569
the solution is under settings.py
INSTALLED_APPS = (
...
'mysite.testapp',
Because i use project mysite and application testapp
Upvotes: 0
Reputation: 599638
Firstly, why would you put your application in a directory called nginx
and html
? It's neither.
Secondly, it's not clear what your question is. You seem to go into the shell, add something to sys.path
, then quit the shell again. That will never work: modifications to sys.path
are for that session only. If you want to add something to the path, do it every time you start up the server - say by modifying manage.py
- or add it to the PYTHONPATH
environment variable.
Upvotes: 0