简体   繁体   English

Package 和 python 中的模块导入

[英]Package and module import in python

Here is my folder structure:这是我的文件夹结构:

|sound
|-__init__.py
|-model
  |-__init__.py
  |-module1.py
  |-module2.py
|-simulation
  |-sim.py

The file module1.py contains the code:文件 module1.py 包含以下代码:

class Module1:
    def __init__(self,mod):
        self.mod = mod

The file module2.py contains the code:文件 module2.py 包含以下代码:

class Module2:
    def __init__(self,mods=None):
        if mods is None:
            mods = []
        self.mods = mods
    def append(self.mod):
        mods.append(mod)

Finally the file sim.py contains the code:最后文件 sim.py 包含代码:

import sound

sound_1 = sound.module2.Module2()

When I execute sim.py I get a ModuleNotFoundError: No module named 'sound'当我执行 sim.py 时,我得到一个ModuleNotFoundError: No module named 'sound'

I've tried pretty much everything such as from sound.model import module2 etc. but I believe the problem comes from python not finding the sound package.我已经尝试了很多东西,例如from sound.model import module2等,但我相信问题来自 python 没有找到sound package。

I've read several tutos, docs and threads, and I don't understand what I'm doing wrong.我已经阅读了几个教程、文档和线程,但我不明白我做错了什么。

That's because python is looking for a module sound inside simulation folder.那是因为 python 正在模拟文件夹中寻找模块声音。 When you try to execute python file this way:当您尝试以这种方式执行 python 文件时:

python sim.py

You should instead create run.py in the same folder as sound您应该改为在与声音相同的文件夹中创建 run.py

sound/
  model/
    ...
  simulation/
run.py

with the following content:内容如下:

from sound.simulator import sim

And run it with python run.py , that way python sets workdir to folder containing sound并使用python run.py运行它,这样 python 将 workdir 设置为包含sound的文件夹


Also in sim.py you should import the module as following:同样在 sim.py 中,您应该按如下方式导入模块:

import sound.model.module1
sound.model.module1.Module1()

Or like this:或者像这样:

from sound.model import module1
module1.Module1()

Explanation when module is being imported, python looks for a folder sound or a file sound.py in directories listed in sys.path , the first one being '' (current working directory).导入模块时的说明,python 在sys.path列出的目录中查找文件夹 sound 或文件 sound.py ,第一个是'' (当前工作目录)。

When running python files, for some reason python sets current workdir to the folder containing the file, even if it is run like python sound/simulation/sim.py运行 python 文件时,由于某种原因 python 将当前工作目录设置为包含该文件的文件夹,即使它像python sound/simulation/sim.py一样运行

To prevent this behaviour, we have to create run.py in the directory which we want to be workdir (ie directory containing sound), so when python sees import sound , it searchs the correct directory为了防止这种行为,我们必须在我们想要成为 workdir 的目录(即包含声音的目录)中创建 run.py,所以当 python 看到import sound时,它会搜索正确的目录

The simple FIX:简单的修复:

  • Move sim.py one folder up into soundsim.py上移一个文件夹到 sound
  • Try import module2尝试import module2
  • sound_1 = module2.Module2()

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

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