Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python packaging for relative imports

First off all: I'm sorry, I know there has been lots of question about relative imports, but I just didn't find a solution. If possible I would like to use the following directory layout:

myClass/     __init__.py     test/         demo.py         benchmark.py         specs.py     src/         __init__.py         myClass.py 

Now my questions are:

  • How do the test files from within the package properly import myClass.py?

  • How would you import the package from outside, assuming you take myClass as submodule in libs/myClass or include/myClass?

So far I couldn't find an elegant solution for this. From what I understand Guido's Decision it should be possible to do from ..src import myClass but this will error:

ValueError: Attempted relative import in non-package

Which looks as it doesn't treat myClass as packages. Reading the docs:

The __init__.py files are required to make Python treat the directories as containing packages;

It seems I'm missing something that specifies where the scripts of the package are, should I use .pth ?

like image 781
eerne Avatar asked Dec 03 '10 18:12

eerne


People also ask

Does Python do relative import?

Relative import specifies object or module imported from its current location, that is the location where import statement resides. There two types of relative imports : Implicit relative imports : Implicit relative import have been disapproved in Python(3.

Can a package be imported in Python?

While importing packages, Python looks in the list of directories defined in sys. path , similar as for module search path.

How do I import another Python package?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.

How does Python package import work?

When a package is imported, Python runs all of the code in the package's __init__.py file, if such a file exists. All of the objects defined in the module or the package's __init__.py file are made available to the importer.


1 Answers

ValueError: Attempted relative import in non-package

Means you attempt to use relative import in the module which is not package. Its problem with the file which has this from ... import statement, and not the file which you are trying to import.

So if you are doing relative imports in your tests, for example, you should make your tests to be part of your package. This means

  1. Adding __init__.py to test/
  2. Running them from some outside script, like nosetests

If you run something as python myClass/test/demo.py, relative imports will not work too since you are running demo module not as package. Relative imports require that the module which uses them is being imported itself either as package module, from myClass.test.demo import blabla, or with relative import.

like image 125
Daniel Kluev Avatar answered Sep 22 '22 07:09

Daniel Kluev