简体   繁体   English

导入自定义模块python

[英]imports custom module python

I have a file I.py and X.py both are custom modules 我有一个文件I.pyX.py都是自定义模块

I'm trying to do an import from X.py file like: 我正在尝试从X.py文件进行import ,例如:

from myapp.I import Int

But, when I try to run in shell, it throws an import error of: 但是,当我尝试在shell中运行时,它将引发以下导入错误:

ImportError: No module named myapp.I

X.py is in a subdir of I.py X.pyI.py

X.py is in: myapp/a/x.py
I.py is in: myapp/

what am I doing wrong? 我究竟做错了什么? Sorry this is slightly basic, it seems so trivial, but I can't seem to get it to work. 抱歉,这有点基本,看似微不足道,但我似乎无法使其正常工作。

Thank you. 谢谢。

You're going to need an __init__.py within myapp/ and myapp/a/. 您将需要在myapp /和myapp / a /中使用__init__.py See What is __init__.py for? 请参阅__init__.py的作用是什么? for more details. 更多细节。

You can't execute X.py directly, even with the required __init__.py files (have you added those?) 即使使用必需的__init__.py文件,您也无法直接执行X.py (是否已添加这些文件?)

This is a limitation (or feature, depending on how you see it). 这是一个限制(或功能,取决于您如何看待它)。 To get around it, you need to create a simple runner script of sorts in the top-level directory which then imports X and executes it. 要解决它,你需要在顶层目录,然后导入到各种创建一个简单的脚本亚军 X并执行它。

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

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