Intrastellar Explorer
Intrastellar Explorer

Reputation: 2511

Python import module from nearby directory instead of from site packages

I am trying to test changes to a Python module, let's call it my_import_module. Currently, it's in a site package. I would like to import it from my local file structure, not from my site packages.

The folder structure looks like this:

dir
|--dirA
   |--dirB
      |--my_import_file.py
      |--__init__.py
|--dirC
   |--dirD
      |--main.py
      |--__init__.py

I am trying to import the file my_import_file.py into main.py, and not from the site packages version.

Currently I have from ../../dirA.dirB.my_import_file import XYZ. This doesn't work.

I would like to not add __init__.py into each directory.

Two questions:

Upvotes: 0

Views: 2043

Answers (1)

adrtam
adrtam

Reputation: 7241

See https://www.python.org/dev/peps/pep-0328/

The way to do is

from ...dirA.dirB.my_import_file import XYZ

Upvotes: 1

Related Questions