Python 从第二个模块导入模块
我正在开发一个小应用程序,其中包含几个子模块:Python 从第二个模块导入模块,python,Python,我正在开发一个小应用程序,其中包含几个子模块: src/ setup.py myapp/ myGuiapp.py modules/ __init__.py editorGui.py fileParser.py 模块/_u;初始化_uu; py.py from editorGui import editorWindow from fileParser import parseM
src/
setup.py
myapp/
myGuiapp.py
modules/
__init__.py
editorGui.py
fileParser.py
模块/_u;初始化_uu; py.py
from editorGui import editorWindow
from fileParser import parseMyFile
myGuiapp.py
from modules import editorWindow
from modules import parseMyFile
parseMyFile(parms)
editorWindow(self)
EditorGui.py
from fileParser import parseMyFile
# from .fileParser import parseMyFile
# from modules.fileParser import parseMyFile
fileParser(parms)
我希望我的EditorGui.py能够从myGuiapp.py调用,但也能够自己运行它。
在这种情况下,我应该如何设置相对导入
如果在EditorGui.py中,我将设置:
from modules.fileParser import parseMyFile
>python editorGui.py
ModuleNotFoundError: No module named 'modules'
from .fileParser import parseMyFile
>python EditorGui.py
ModuleNotFoundError: No module named '__main__.fileParser'; '__main__' is not a package
from fileParser import parseMyFile
>python editorGui.py
但从主应用程序
>python myGuiapp.py
好的
如果在EditorGui.py中,我将设置:
from modules.fileParser import parseMyFile
>python editorGui.py
ModuleNotFoundError: No module named 'modules'
from .fileParser import parseMyFile
>python EditorGui.py
ModuleNotFoundError: No module named '__main__.fileParser'; '__main__' is not a package
from fileParser import parseMyFile
>python editorGui.py
如果在EditorGui.py中,我将设置:
from modules.fileParser import parseMyFile
>python editorGui.py
ModuleNotFoundError: No module named 'modules'
from .fileParser import parseMyFile
>python EditorGui.py
ModuleNotFoundError: No module named '__main__.fileParser'; '__main__' is not a package
from fileParser import parseMyFile
>python editorGui.py
好的
我怎样才能使这两个都起作用,我在这里做错了什么?我希望避免设置此dev目录的绝对删除路径。
构建这样的应用程序,什么样的方式最为简洁
编辑:
在@Diego Trazzi帖子之后,我决定将我的EditorGui.py升级:
src/
setup.py
myapp/
myGuiapp.py
editorGui.py
modules/
__init__.py
fileParser.py
这解决了许多问题,但出于好奇,我的问题仍然存在。如何在通过代码从父目录导入模块时从同一级别导入模块 为避免指定模块的相对路径,可以将路径附加到文件中:
# yourfile.py
import sys
sys.path.insert(0, '/path/to/application/app/folder')
您还可以在运行命令的shell中指定它:
python [-bBdEiOQsRStuUvVWxX3?] [-c command | -m module-name | script | - ] [args]
更好的解决方案
理想情况下,您的项目应该以这样的方式进行结构,以便所有
package_name/
/* setup.py and other misc files */
package_name/
__init__.py
/* module files go here */
test/
/* tests go here */
这样,您的可执行文件位于顶层,并且在分发项目时,用户不必深入子文件夹。我发现这个答案非常有用:
为避免指定模块的相对路径,您可以将路径附加到文件中:
# yourfile.py
import sys
sys.path.insert(0, '/path/to/application/app/folder')
您还可以在运行命令的shell中指定它:
python [-bBdEiOQsRStuUvVWxX3?] [-c command | -m module-name | script | - ] [args]
更好的解决方案
理想情况下,您的项目应该以这样的方式进行结构,以便所有
package_name/
/* setup.py and other misc files */
package_name/
__init__.py
/* module files go here */
test/
/* tests go here */
这样,您的可执行文件位于顶层,并且在分发项目时,用户不必深入子文件夹。我发现这个答案非常有用:
使用。它听起来不像是清理代码的Python方式。这是一种解决方法,类似于模块的直接、绝对路径。使用。它听起来不像是清理代码的Python方式。这是一个解决方法,类似于模块的直接、绝对路径。谢谢迭戈!我真的希望避免在代码中添加任何类型的路径,因为这不是我希望采用的过程性方式。至于结构,也许你是对的,也许我应该决定将我的EditorGui.py升级到与main myGuiapp.py相同的级别,这可能会解决一些问题。谢谢迭戈!我真的希望避免在代码中添加任何类型的路径,因为这不是我希望采用的过程性方式。至于结构,也许你是对的,也许我应该决定将EditorGui.py向上移动,使其与main myGuiapp.py处于同一级别,这可能会解决一些问题。