I want to rename a project which I created with:
django-admin.py startproject
But after renaming the folder and all the references inside my project, I still can't get it to start. It says myproject.settings is not in the pythonpath. Since the old project name is neither in the pythonpath i figure that django must keep these names and paths somewhere else. Where does it store this information ?
I know I could just add the path to sys.path while execution, but i want to fix this completely.
If i run:
python manage.py runserver
I get:
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named settings
The name for 'mysite' is already the correct one and corrosponds with the folder name. But still it can't find it.
Any ideas?
PS: I'm running debian.
It doesn't store the project path anywhere. Everything is calculated relative to the path you specified for the settings module.
If you renamed your project folder and it's still trying to load the old settings, it's possible that you still have the old settings file configured somewhere, e.g. in the environment DJANGO_SETTINGS_MODULE is still pointing to oldproject.settings or your WSGI server is still configured to load oldproject.settings. Also check that you don't have any package weirdness in your interpreter's site-packages.
i'm sorry, but the solution was more simple in my case:
one of the apps had the same name i wanted to give to the project. this resulted in the described error message !
sorry for the trouble.
Related
I've made a separate directory for my django modules. I've added this directory to my PATH variable. I've also created a new PYTHONPATH variable, since it wasn't there. I've added modules to settings.py. But when im trying to run manage.py syncdb for the new module it still says
Error: No module named my_module
Why, oh why?
EDIT: I didn't created the app with manage.py startapp, but manually created the files. Can this cause the problem?
PATH tells your shell where to find executables; it has nothing to do with Python. PYTHONPATH is a list of directories to search for Python modules. It should be edited to include the directory with my_module.
Hard to tell you what the issue is with only that output, however this should solve the problem:
Inside bar.py or bar/__init__.py
import os,sys
sys.path.append(os.path.dirname(__file__))
Now, in other files you can import bar
I've made a separate directory for my django modules. I've added this directory to my PATH variable. I've also created a new PYTHONPATH variable, since it wasn't there. I've added modules to settings.py. But when im trying to run manage.py syncdb for the new module it still says
Error: No module named my_module
Why, oh why?
EDIT: I didn't created the app with manage.py startapp, but manually created the files. Can this cause the problem?
PATH tells your shell where to find executables; it has nothing to do with Python. PYTHONPATH is a list of directories to search for Python modules. It should be edited to include the directory with my_module.
Hard to tell you what the issue is with only that output, however this should solve the problem:
Inside bar.py or bar/__init__.py
import os,sys
sys.path.append(os.path.dirname(__file__))
Now, in other files you can import bar
I'm practising Django now, and I'm stuck. When I run:
>>> from django import template
>>> t = template.Template('hi')
Gives me:
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
Any idea what causes this? What should I do?
Django is designed to work within a project, which contains a file named settings.py. Rather than just importing it on the command line, you should work through the tutorial, which shows you how to create a project and its apps.
If you just want a quick start, you need to execute this on the command line:
django-admin.py startproject test_project
This will create a directory named test_project with the needed files. Within that directory, you can execute the command:
./manage.py shell
which will get you a Python shell with the settings module properly loaded.
As noted here, you have to do the following:
When you use Django, you have to tell it which settings you're using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.
The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.
I run into this problem pretty consistently... keep in mind I am quite new to Django and a total Python amateur.
It seems that, for example, whenever I check out my Django project on a new computer after a clean install of Python and Django, it can never find the project/apps I create or copy in.
So right now I have an app that is working, and I downloaded a 3rd party Django module and installed it into my app directory, include it in my settings, and the web server quits because it cannot find the module.
This is the first time I've imported an third party module. In the past when it couldn't find modules I created, I would just rename the folder and run "manage.py startapp appname", delete the folder it created, and name my original folder back, boom, problem solved...
But that's obviously a hack, I am wondering if anyone can explain the the heck is going on here and how best to approach it.
I can't be the only one who has run into this, but I couldn't find any other questions on this site that seemed to match my issue.
Happens on both OS X and Windows 7.
They way Django works is pretty much how Python works. At default the folder you create when you run django-admin.py startproject name is added to your python path. That means that anything you put into there you can get to. But you have to mind that when you write the app into the installed app list. If you have an app at project/apps/appname, you would have to write 'app.appname' in the installed apps list.
Now there are some ways to go about adding 3rd party apps located somewhere else to your project. You can either add them to your python path, put in your python path, or make a link to your python path. However, you can also add a sys.path.insert(...) in your manage.py file where you add the folder of your liking to your python path. Doing this will allow you to add folders to your python path for that project only, and will keep your python path more clean.
Your third party django module should be searchable by PYTHONPATH, because django module is no other than a python module. Now there are two ways to do this:
Create a folder (anywhere you want), put your third party django module under there. Now set that directory to environment variable $PYTHONPATH
e.g (on Linux box):
export PYTHONPATH = /home/me/pythonmodules/
Create a folder (anywhere you want), put the third party django module under there. Now if you are on Unix box, create a symlink to that directory to python site-packages. Use this command to find out where your python site packages is:
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
I am an experienced PHP programmer using Django for the first time, and I think it is incredible!
I have a project that has a lot of apps, so I wanted to group them in an apps folder.
So the structure of the project is:
/project/
/project/apps/
/project/apps/app1/
/project/apps/app2
Then in Django settings I have put this:
INSTALLED_APPS = (
'project.apps.app1',
'project.apps.app2',
)
This does not seem to work?
Any ideas on how you can put all your apps into a seprate folder and not in the project root?
Many thanks.
Make sure that the '__init__.py' file is in your apps directory, if it's not there it won't be recognized as part of the package.
So each of the folders here should have '__init__.py' file in it. (empty is fine).
/project/
/project/apps/
/project/apps/app1/
/project/apps/app2
Then as long as your root 'module' folder is in your PYTHONPATH you'll be able to import from your apps.
Here's the documentation regarding the python search path for your reading pleasure:
http://docs.python.org/install/index.html#modifying-python-s-search-path
And a nice simple explanation of what __init__.py file is for:
http://effbot.org/pyfaq/what-is-init-py-used-for.htm
As long as your apps are in your PYTHONPATH, everything should work. Try setting that environment variable to the folder containing your apps.
PYTHONPATH="/path/to/your/apps/dir/:$PYTHONPATH"
Your top-level urls.py (also named in your settings.py) must be able to use a simple "import" statement to get your applications.
Does import project.apps.app1.urls work? If not, then your PYTHONPATH isn't set up properly, or you didn't install your project in Python's site-packages directory.
I suggest using the PYTHONPATH environment variable, instead of installing into site-packages. Django applications (to me, anyway) seem easier to manage when outside site-packages.
We do the following:
Django projects go in /opt/project/.
PYTHONPATH includes /opt/project.
Our settings.py uses apps.this and apps.that (note that the project part of the name is part of the PYTHONPATH, not part of the import.