imports custom module python

旧时模样 提交于 2019-12-13 02:38:43

问题


I have a file I.py and X.py both are custom modules

I'm trying to do an import from X.py file like:

from myapp.I import Int

But, when I try to run in shell, it throws an import error of:

ImportError: No module named myapp.I

X.py is in a subdir of I.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.


回答1:


You're going to need an __init__.py within myapp/ and myapp/a/. See What is __init__.py for? for more details.




回答2:


You can't execute X.py directly, even with the required __init__.py files (have you added those?)

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.



来源:https://stackoverflow.com/questions/23638346/imports-custom-module-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!