I keep running into problems with Unresolved imports in my Django project (in Eclipse w/ PyDev). After googling for a while on a given issue I can generally find the right thing to add to PYTHONPATH to solve the problem. But it seems like I'm missing some concept or process by which I can definitively say "obviously there is what I should add to get that thing to import properly". What makes this more frustrating is that I'm just followin开发者_运维百科g the basic Django tutorial, so I'd expect these things to be simply dealt with. In Java world it is usually pretty clear because I can find out exactly what library a particular package is coming from, download it and add it to build path. What is the equivalent concept in Python/Django world?
I am not looking to solve a specific problem here, but rather to know what are the steps to follow, or where to look it up, or the concept I am missing. Or possibly to find out that there is not one and its always guesswork...
I am running on Ubuntu 10.10, Python 2.6
If you are using eclipse, she can automatically configure python interpreter (includes all libraries which are installed system-wide). You probably have done this step.
If you are still facing import problems you probably did not installed libraries system-wide. Or you have modified python paths and removed default directories.
Usually there is a setup.py
with python library. It will install that library into your system:
python setup.py install
Some libraries which comes with setup.py
can be installed from python package index with pip.
If library does not provide setup.py
script then you will need to deal with it manually.
So basic rule about python libraries is: if you did not installed them system wide, then you have to add them to python-path manually.
If you do not want to install bunch of libraries system-wide, tools like virtualenv or buildout can help.
Virtualenv creates an environment where you can install libraries as if you would install them globally:
[path-to-virtualenv]/bin/python setup.py install
and your library will install to:
[path-to-virtualenv/lib/site-packages
Buildout works a bit differently. You specify a list of packages you want to install in a configuration files, and it will generate script files with paths fixed. Buildout can also generate configuration settings for pydev
project. Buildout is automization tool with its own way to create virtual environment. Buildout does more things than virtualenv, but can be difficult to deal with at first.
From your question I assume that you are quite new to Python, so let's start at the beginning.
My advice would be to look at the traceback that Django generates and search for a line which looks like this: "ImportError: No module named somemodule". This tells you the name of the missing module.
Once you know the name of the module you can try to see if the missing module is listed in PyPi (Python Package Index) by using easy_install.
Try running:
easy_install somemodule
If you're lucky then the missing module will be installed into your python installation's site-packages directory (possibly /usr/lib/python2.6/site-packages, depending on where your python is installed). The site-packages directory is where all 3rd party modules should be installed to, and all modules in this directory are always import-able and on the PYTHONPATH.
It's possible that your code is trying to import something that is not available on PyPi, in which case Google is your best option :p
p.s. If easy_install doesn't work (or it's missing) then it's possible that you don't have the setuptools python module installed, which you can get from here: http://pypi.python.org/pypi/setuptools
If you're installing modules system wide (site-packages or similar), you probably won't have a problem running them on your system, so I'm going to discuss the PYTHONPATH of your project.
The directory of the file that is your entry path into your program is added to the PYTHONPATH, along with all the system defined PYTHONPATHS. To get to any script within that directory structure, each directory must contain a file called __init__.py
, which signals that that directory is a module within your program.
/myproject/
entrypoint.py
apps/
__init__.py
app1/
__init__.py
models.py
views.py
app2/
models.py # unreachable
views.py # unreachable
If run python entrypoint.py
you will be able to access app1
, because all folders from entrypoint.py
to app1
contain an __init__.py
file. However, app2
can't be imported, since it is not visible as a module.
If you have a directory that is a module, you must ensure that it contains an __init__.py
, or that /path/to/app1
is within sys.path itself, which can then be imported simply with import models
.
精彩评论