Frage

I have following directory structure

outdir
     |--lib
            |--- __init__.py
            |--- abc.py
     |--indir
            |--- __init__.py
            |---- import_abc.py

How to import lib in import_abc.py?

when i try to import lib in import_abc.py

I get following error

  Traceback (most recent call last):
  File "import_abc.py", line 1, in <module>
  import lib
  ImportError: No module named lib
War es hilfreich?

Lösung 2

Add a __init__.py file in outdir and then do:

#import_abc.py
import sys
sys.path.append('/home/monty/py')   #path to directory that contains outdir
from outdir.lib import abc

Andere Tipps

Take a minute to look at what you're trying to achieve: you want to import the module abc.py which is a part of the package lib, so in order to import it correctly you need to specify in which package it is:

from lib import abc

or

import lib.abc as my_import

On a side note, have a look at the Python Tutorial chapter on modules.

Given what @Noelkd commented, I forgot to tell you about PYTHONPATH which is an environment variable that contains the folder where Python will look for the module (like the Java CLASSPATH). You need to put your root folder inside the PYTHONPATH to avoid doing some fiddling with sys.path.append.

In Bash:

export PYTHONPATH=<abs. path to outdir>

For example, I've put outdir in my Desktop:

export PYTHONPATH=~/Desktop/outdir

And the import works like a charm.

You can find some great explanations of the import mechanism and PYTHONPATH in this blog post.

N.B.: If you're using an IDE (PyDev for example), generally it will set up the PYTHONPATH automatically for each project. But if you want to do it all by yourself, you will need to set the PYTHONPATH environment variable.

I have experienced similar problem. There are 2 ways of importing modules generally. One is absolute path, the other one is relative path.

For absolute path, python uses sys.path to resolve the path, as suggested by above answers. But personally I don't like manipulate sys.path.

For relative path, python uses __name__ resolve the path. Say an module has __name__ as package1.package2.module3 and module3 imports module in upper package using from ..packge3 import module4. Python will combine them to get package1.packge3.module4. Details have been illustrated in this post.

My solution is adding an /outside dir as the parent dir of /outdir and make outdir as a package by adding __init__.py under /outdir.

Then you can add from ..lib.abc import cls in import_abc.py file. The relative path should work

import sys
sys.path.append('../lib')
import abc

You could set parent outdir directory as module adding a __init__.py to it too:

outdir
     |-- __init__.py
     |-- lib
            |-- __init__.py
            |-- abc.py
     |-- indir
            |-- __init__.py
            |-- import_abc.py

Then in import_abc.py you will be able to import lib if adding outdir's path:

#In import_abc.py

import sys
sys.path.append('../outdir')
import lib.abc
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top