简体   繁体   English

从调用其他模块的python文件导入模块

[英]Importing a module from a python file calling other modules

I am trying to run a specific code, now it requires the following importing of the Python file and I am getting an error, I have constructed a toy example to illustrate the problem.我正在尝试运行特定的代码,现在它需要导入以下 Python 文件,但出现错误,我构建了一个玩具示例来说明问题。 I am running on Python version 3.6.3.我在 Python 3.6.3 版上运行。
Consider the following folder structure.考虑以下文件夹结构。

root
  |- outside.py
  |- folA
       |- __init__.py
       |- inside.py
       |- folB
            |- __init__.py
            |- eveninside.py

Now the contents of these files are as follows.现在这些文件的内容如下。

outside.py外面.py

import folA.inside
print("Outside")


inside.py里面.py

import folB.eveninside
print("Inside")


eveninside.py Eveninside.py

print("Even Inside")   #All inits are empty


Now when I run inside.py things run perfectly well and have expected outputs.现在,当我运行inside.py 时,事情运行得非常好并且有预期的输出。 But when I run outside.py there is an error saying "no module named folB".但是当我运行outside.py 时出现错误提示“没有名为folB 的模块”。 I have tried appending the system path but there are no changes.我尝试附加系统路径,但没有任何更改。 Please explain me how to resolve this.请向我解释如何解决这个问题。

This just come from the fact python goes look module from python path. 这只是来自python从python路径查找模块的事实。 Here when you launch outside he try to see if there is a folB in your directory where is outside.py. 在这里,当您在外部启动时,他尝试查看您的目录中outside.py是否存在folB。

Two solutions. 两种解决方案。

  1. You use sys.path.append in inside.py to add the directory where is folA as a place to look for when trying to import module. 您可以在inside.py中使用sys.path.append添加目录folA作为尝试导入模块时要查找的位置。 I don't know how good practice it is. 我不知道这是一种好的做法。

  2. In your case you can just write in inside.py `import folA.folB.eveninside. 就您而言,您可以只在inside.py`import folA.folB.eveninside中编写。 But obviously it won't work when you launch inside.py. 但是显然,当您启动inside.py时,它将无法工作。 In this case you have the first solution. 在这种情况下,您有第一个解决方案。

If you only wish to run the program from the outside.py, you can add this code to your includes in outside.py: 如果只希望从outside.py运行该程序,则可以将此代码添加到outside.py中的includes中:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '.')))

and add a context.py to each level of your package structure with the following code: 并使用以下代码将context.py添加到包结构的每个级别:

import os
import sys
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.insert(0, path)

_ = ''

you can then import this into your code to make it aware of the package structure. 然后,您可以将其导入代码中,以使其了解程序包的结构。 ie inside.py would have from .context import _ 即inside.py将具有from .context import _

Disclaimer: This code came from someone else but I'm unsure who. 免责声明:此代码来自其他人,但我不确定是谁。 Possibly this article . 可能是这篇文章 I am certain a better practice exist, but I have found this to work and the context.py makes a great place to keep constants in a central location. 我确信存在更好的做法,但是我发现它可以工作,并且context.py是将常量保持在中央位置的好地方。 just import them instead of the underscore 只需导入它们而不是下划线

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

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