Python imports relative path

Question:

I’ve got a project where I would like to use some python classes located in other directories.

Example structure:

/dir
 +../subdirA
 +../subdirB
 +../mydir

The absolute path varies, because this project is run on different machines.

When my python file with MySampleClass located in /mydir is executed, how do I import OtherClassRoot located in /dir or OtherClassA located in /subdirA?

I tried things like:

from . import MySampleClass as msc

or

from ../ import MySampleClass as msc

but this always fails or gives me error messages like Attempted relative import in non-package

So, whats the right way to relatively import python files?

Asked By: daniel451

||

Answers:

You will need an __init__.py in the mydir directory (and it can be empty), then as long as dir is in the sys path, assuming your MySampleClass is in myfile.py and myfile.py is in mydir

from mydir.myfile import MySampleClass

If you want to import top level functions from a file called util.py that reside in subdirA into myfile.py (where your class is), then an __init__.py must be in subdirA and then in myfile.py

from subdirA.util import somefunc, someotherfunc

The same is true of the sys path, that is, you must either start from ‘dir’ or add it. Everything is imported from the top level of the package (usually your project folder).

However, for module testing, where you might run a function from util in the interpreter, if you start from subdirA, you will need to add dir to the sys path, so your imports can resolve.

>>> import sys
>>> sys.path.append('../dir')

but this is a hack and would be preferable to only use from the interactive interpreter when you are testing. You can also add ‘dir’ to your site packages in a pth file.

To use relative imports, you would need a deeper nested folder, like subdirA/subdirofA, then in subdirofA, you could use . to back out (like from .subdirB ). Really, for me, relative imports are somewhat difficult to see the utility. It’s better for me to use direct imports relative to the project directory, but I could see them being useful if you wanted to nest a naive sub package, but again, still better to be explicit than implicit if possible.

also see this

Update for Python 3’s namespace packages. An __init__.py file is no longer required. Some libraries may still reference the file, so it may still be needed for compatibility. For example,

(test-env) machine:~ user$ mkdir new_package
(test-env) machine:~ user$ python
>>> import new_package
>>> new_package.__file__
>>> type(new_package.__file__)
<class 'NoneType'>
>>>
(test-env) machine:~ user$ touch new_package/__init__.py
(test-env) machine:~ user$ python
>>> import new_package
>>> new_package.__file__
'/Users/user/new_package/__init__.py'
>>> type(new_package.__file__)
<class 'str'>
>>>
Answered By: Wyrmwood

On dir structure as following:

/dir
 +../subdirA
 +../subdirB
 +../mydir

combining with linux command ln , we can make things a lot simpler:

1. cd dir/mydir
2. ln -s ../subdirA ./
3. ln -s ../subdirB ./

And, now if you want to import some_stuff from file: dir/subdirA/fileA.py into your file: dir/mydir/myfile.py, simply import like this:

from subdirA.fileA import some_stuff

And, the same applies to ‘dir/subdirB’

What’s More, this works for setup.py process, 🙂

Answered By: jacoolee

first add the relative paths to the pythonpath

import os

import sys

cwd = os.getcwd()

sys.path.append(cwd + '/../subdirA/')

sys.path.append(cwd + '/../subdirB/')

then import the modules.

Answered By: reggie
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.