从父目录的子目录导入python模块

时间:2022-10-11 19:42:41

I have a directory structure like this

我有这样的目录结构

dir1/subdir1/module1.py
dir1/subdir2/subdir22/module2.py

Assume that I am adding __init__.py to each of the directories & sub directories. I want to import module1 from module 2, after referencing related questions and trying different ways, I couldn't find the solution. For example

假设我将__init__.py添加到每个目录和子目录中。我想从模块2导入module1,在引用相关问题并尝试不同的方法后,我找不到解决方案。例如

in module2 I tried to import module 1 like

在module2中我尝试导入模块1

from .... import subdir1.module1
from ....subdir1 import module1

Both the above imports throw syntax errors.

以上导入都会抛出语法错误。

3 个解决方案

#1


This works for me:

这对我有用:

$ mkdir -p dir1/subdir1
$ mkdir -p dir1/subdir2/subdir22
$ touch dir1/{,subdir1,subdir2,subdir2/subdir22}/__init__.py
$ echo 'x = 42' > dir1/subdir1/module1.py
$ echo 'from ...subdir1.module1 import x; print x' > dir1/subdir2/subdir22/module2.py
$ python -m dir1.subdir2.subdir22.module2
42

The magic incantation is

神奇的咒语是

from ...subdir1.module1 import x

though

from ...subdir1 import module1

also works.

#2


The following code can load a module from a path even if not inside a package or not on default path (module here is Contemplate an engine of mine), you should have an dummy __init__.py file in that folder though:

以下代码可以从路径加载模块,即使不在包内,也不在默认路径上(这里的模块是Contemplate我的引擎),你应该在该文件夹中有一个虚拟__init__.py文件:

import imp
ContemplateModulePath = os.path.join(os.path.dirname(__file__), '../src/python/')
try:
    ContemplateFp, ContemplatePath, ContemplateDesc  = imp.find_module('Contemplate', [ContemplateModulePath])
    Contemplate = getattr( imp.load_module('Contemplate', ContemplateFp, ContemplatePath, ContemplateDesc), 'Contemplate' )
except ImportError as exc:
    Contemplate = None
    sys.stderr.write("Error: failed to import module ({})".format(exc))
finally:
    if ContemplateFp: ContemplateFp.close()

if not Contemplate:
    print ('Could not load the Contemplate Engine Module')
    sys.exit(1)
else:    
    print ('Contemplate Engine Module loaded succesfully')

#3


This worked for me,

这对我有用,

import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.dirname(path.dirname(path.abspath(__file__))) ) ) )

from dir1.subdir1 import module1

#1


This works for me:

这对我有用:

$ mkdir -p dir1/subdir1
$ mkdir -p dir1/subdir2/subdir22
$ touch dir1/{,subdir1,subdir2,subdir2/subdir22}/__init__.py
$ echo 'x = 42' > dir1/subdir1/module1.py
$ echo 'from ...subdir1.module1 import x; print x' > dir1/subdir2/subdir22/module2.py
$ python -m dir1.subdir2.subdir22.module2
42

The magic incantation is

神奇的咒语是

from ...subdir1.module1 import x

though

from ...subdir1 import module1

also works.

#2


The following code can load a module from a path even if not inside a package or not on default path (module here is Contemplate an engine of mine), you should have an dummy __init__.py file in that folder though:

以下代码可以从路径加载模块,即使不在包内,也不在默认路径上(这里的模块是Contemplate我的引擎),你应该在该文件夹中有一个虚拟__init__.py文件:

import imp
ContemplateModulePath = os.path.join(os.path.dirname(__file__), '../src/python/')
try:
    ContemplateFp, ContemplatePath, ContemplateDesc  = imp.find_module('Contemplate', [ContemplateModulePath])
    Contemplate = getattr( imp.load_module('Contemplate', ContemplateFp, ContemplatePath, ContemplateDesc), 'Contemplate' )
except ImportError as exc:
    Contemplate = None
    sys.stderr.write("Error: failed to import module ({})".format(exc))
finally:
    if ContemplateFp: ContemplateFp.close()

if not Contemplate:
    print ('Could not load the Contemplate Engine Module')
    sys.exit(1)
else:    
    print ('Contemplate Engine Module loaded succesfully')

#3


This worked for me,

这对我有用,

import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.dirname(path.dirname(path.abspath(__file__))) ) ) )

from dir1.subdir1 import module1