将文件导入为Python中的模块
我想制作一个CMD工具。我创建了两个文件,一个名为main.py
,另一个名为version.py
在同一目录中有
import os
def pyVersion():
os.system("python --version")
版本
import version
version.pyVersion()
。印刷:
File "C:\Users\User\PycharmProjects\cmd tool\main.py", line 1, in <module>
import version
ModuleNotFoundError: No module named 'version'
I wanted to make a cmd tool. I created two files, one named main.py
, and the other named version.py
there are in the same directory
version.py
:
import os
def pyVersion():
os.system("python --version")
main.py
:
import version
version.pyVersion()
I think it should work, but when I run main.py
, it prints:
File "C:\Users\User\PycharmProjects\cmd tool\main.py", line 1, in <module>
import version
ModuleNotFoundError: No module named 'version'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,python应该使用文件夹
c:\ user \ user \ user \ pycharmprojects \ cmd tool \
搜索导入的模块,即使在列表sys.path
上,您也可以使用此文件夹列表上没有此文件夹,然后您可以在导入模块之前手动添加它。
为了使其更普遍,您可以使用
OS
获取此文件夹而无需硬编码Normally Python should use folder
C:\Users\User\PycharmProjects\cmd tool\
to search imported modules and you may have this folder even on listsys.path
But if it doesn't have this folder on list then you may add it manually before importing module.
To make it more universal you can use
os
to get this folder without hardcoding