What is a common process / concept to determine what to add to PYTHONPATH to solve import problems?

I have been having problems with unresolved imports all the time in my Django project (in Eclipse w / PyDev). After I searched for a while on a given problem, I can find the right thing to add to PYTHONPATH to solve the problem. But it seems to me that I'm missing some kind of concept or process with which I can finally say "obviously, I have to add in order to correctly import this thing." What makes this more unpleasant is that I just follow the basic Django tutorial, so I would expect these things to be just solved. In the Java world, this is usually pretty clear, because I can find out exactly which library a particular package comes from, download it and add it to the build path. What is the equivalent concept in the Python / Django world?

I am not going to solve any specific problem here, but I know what steps should be performed, or where to look, or a concept that I am missing. Or perhaps find out that there is not one, but his guesses are always ...

I am running on Ubuntu 10.10, Python 2.6

+3
source share
3 answers

From your question, I assume that you are completely new to Python, so let's start from the beginning.

My advice is to look at the trace that Django creates and look for a line that looks like this: "ImportError: No module named somemodule". This tells you the name of the missing module.

Once you find out the name of the module, you can try to find out if the missing module is missing in PyPi (the Python package index) using easy_install.

Try to run:

easy_install somemodule

, - python (/usr/lib/python2.6/site-packages, , python). site-packages - , , PYTHONPATH.

, -, PyPi, Google - : p

p.s. easy_install ( ), , python setuptools, : http://pypi.python.org/pypi/setuptools

+1

eclipse, python ( , ). , .

, , , . python .

setup.py python. :

python setup.py install

, setup.py, python pip.

setup.py script, .

, python: , python .

, , virtualenv buildout .

Virtualenv , , :

[path-to-virtualenv]/bin/python setup.py install

and your library will install to:

[path-to-virtualenv/lib/site-packages

Buildout -. , , script . Buildout pydev. Buildout - , . Buildout , virtualenv, .

+3

( ), , , , PYTHONPATH .

PYTHONPATH , , PYTHONPATHS. script , __init__.py, , .

/myproject/
    entrypoint.py
    apps/
        __init__.py
        app1/
            __init__.py
            models.py
            views.py
        app2/
            models.py  # unreachable
            views.py   # unreachable

python entrypoint.py, app1, entrypoint.py app1 __init__.py. app2 , .

, , , __init__.py, /path/to/app1 sys.path, import models.

+1

Source: https://habr.com/ru/post/1792890/


All Articles