简体   繁体   English

从多个并行子目录导入模块(Python)

[英]Importing Modules from Multiple Parallel Subdirectories (Python)

Relatively new to python and I'm using it for lab equipment automation at work. 对于python来说相对较新,我正在使用它进行工作中的实验室设备自动化。

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. 我的脚本位于目录B中。目录B的父目录为目录A。目录A包含另一个目录,我需要从该目录输入和导入模块。 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. 我需要将m1.py和m2.py导入到脚本中。 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.) 我无法将我的脚本移到A目录中,因为它正在访问我在B中编写并转储的其他模块。出于公司特定的原因,我需要维护包括C目录和C目录以下的所有内容的目录结构(不要获取我开始了。)

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. 我不想对它们的路径进行硬编码,因为它们将存在于SVN中,并且根目录可能会更改。 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 A / C / E / m1.py

def my_func1():
    print 'func1'

A/C/F/m2.py A / C / F / m2.py

def my_func2():
    print 'func2'

A/B/myscript.py 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 ): 或者,如果您的导入目录名称带有空格(例如, C dir而不是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. 所有__init__.py文件均为空,但需要存在,因此Python知道在这些文件夹中查找代码。

cd to the A directory and then run the script as a package. cdA目录,然后以包形式运行脚本。 Note: there is no .py and the end of the filename. 注意:没有.py和文件名的末尾。

$ 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 . 如果目录结构始终相同,则可以尝试将m1.pym2.py导入,方法是将C的路径附加到myscript.py sys.path中。

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__ . 您还可以使用os.path.dirnameos.path.joinos.path.abspath类的os.path方法以及变量__file__B相对地获取C的路径。

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. 更好的做法是在Python中显式导入并注意目录结构。 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: 只要您的项目位于python路径上,您就应该能够从位于B或层次结构中其他任何位置的模块执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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