简体   繁体   中英

Importing Modules from Multiple Parallel Subdirectories (Python)

Relatively new to python and I'm using it for lab equipment automation at work.

I have a script living in a directory B. Directory B's parent is Directory A. Directory A holds another directory which I need to enter and import modules from. The structure looks like this:

            A
  B                   C
  myscript.py    
                   E        F
                  m1.py    m2.py

I need to import m1.py and m2.py into my script. I cannot move my script above into the A directory because it's accessing a bunch of other modules that I wrote and dumped in B. I need to maintain the directory structure of everything including and below directory C for company-specific reasons (don't get me started.)

My question is, how do I go about intelligently importing these? I don't want to hard code their paths since these will live in an SVN and the root might change. I'm thinking about using a loop to scan through? I'm just unfamiliar with the syntax to make this possible. All solutions are welcome!

I have the following file setup:

    A
    ├── B
    │   ├── __init__.py
    │   └── myscript.py
    └── C
        ├── E
        │   ├── __init__.py
        │   └── m1.py
        ├── F
        │   ├── __init__.py
        │   └── m2.py
        └── __init__.py

Code:

A/C/E/m1.py

def my_func1():
    print 'func1'

A/C/F/m2.py

def my_func2():
    print 'func2'

A/B/myscript.py

from C.E.m1 import my_func1
from C.F.m2 import my_func2

my_func1()
my_func2()

Or, if your import directory name has a space (for example, C dir rather than C ):

import importlib
m1 = importlib.import_module("C dir.E.m1")
m2 = importlib.import_module("C dir.F.m2")

m1.my_func1()
m2.my_func2()

All __init__.py files are empty but need to exist so Python knows to look in these folders for code.

cd to the A directory and then run the script as a package. Note: there is no .py and the end of the filename.

$ python -m B.myscript

Output:

func1
func2

If the directory structure would always be the same, you can try importing m1.py and m2.py , by appending the path of C into sys.path in your myscript.py .

You can also relatively get the path of C from B by using os.path methods like os.path.dirname and os.path.join and os.path.abspath and the variable - __file__ .

Example -

import os.path
import sys
curdir = os.path.dirname(__file__)
cdir = os.path.abspath(os.path.join(curdir,'../C'))
sys.path.append(cdir)
import m1,m2

It is better practice to import explicitly in Python and be mindful of the directory structure. If the structure changes, your imports should change.

As long as your project is on the python path, you should be able to do this from the module that resides in B or anywhere else in the hierarchy:

from Project.A.B import myscript
from Project.A.C.E import m1
from Project.A.C.F import m2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM