python-3.x - 从另一个目录导入 Python 模块

标签 python-3.x python-import python-3.6

我有一个具有以下项目结构的 python 程序:

ProjectName
     |
     |----ProjectMain.py
     |
     |----__init__.py
     |
     |----Common
            |
            |----Dictionaries
            |         |
            |         |----Dicts.py
            |         |
            |         |----__init__.py
            |
            |----Core.py
            |
            |----LogoCreator.py
            |
            |----__init__.py

每个文件的内容,除了每个__init__.py文件都是空的,如下:

LogoCreator.py

def random_logo():
#    do stuff


内核.py

from LogoCreator import random_logo
import Dictionaries.Dicts as Dicts

#do stuff


字典.py

i_am_a_dictionary = {}


项目主体.py

from Common.Core import *
import Common.Dictionaries.Dicts as Dicts

#do stuff


当我运行 ProjectMain.py 时,出现以下错误:

Traceback (most recent call last):
  File "ProjectName\ProjectMain.py", line 1, in <module>
    from Common.Core import *
  File "ProjectName\Common\Core.py", line 1, in <module>
    from LogoCreator import random_logo
ModuleNotFoundError: No module named 'LogoCreator'

这是我第一次使用包,所以非常感谢任何帮助。

最佳答案

如果要使用 random_logo 方法,则必须在导入 LogoCreator 后使用点符号。

from Common import LogoCreator
def create (self):
  x = self.random_logo()
  #code that generates a random logo

关于python-3.x - 从另一个目录导入 Python 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43671710/

相关文章:

python-3.6 - Python 3.6+ 本地时间消歧 (PEP 495)

python - 如何在 Python3 中找到 os.scandir 的源代码?

python - 查询多个 between 子句

python - 列表在Python3.6

python - 计算两个列表字典之间的相似性的最有效方法是什么?

python - 创建从它继承的对象时在 Python 中有条件地导入

python - 如何将我的模块添加到 travis-ci pythonpath

python - 在 beautifulsoup 标签中插入 xml 树字符串

python - 导入错误 : No module named '' when importing my own sub-package

postgresql - 使用 SqlAlchemy 和 PostgreSQL 找不到外键