【发布时间】:2020-06-03 18:26:59
【问题描述】:
我的桌面上有两个文件夹,一个名为“testpackage”,另一个名为“testplay”。 'testpackage' 包含一个 init.py 和另一个名为 'numberstuff.py' 的文件(一个有两种方法的类)。 'testplay' 包含'play_01.py',一个简单的脚本来检查我可以将一个包添加到 sys.path 而不将它物理添加到 sys.path 中的库中,我想我可以通过 sys.path.append 做到这一点(path\to\file) 在 windows 上的 python3 上。
测试播放“play_01.py”代码:
import sys
for i in sys.path:
print(i,'\n')
sys.path.append('C:\\Users\\priper\\Desktop\\testpackage')
from testpackage import numberstuff
a = numberstuff.maffs()
print(a.sqrtx(3))
控制台返回:
C:\Users\priper\AppData\Local\Programs\Python\Python37\python37.zip
C:\Users\priper\AppData\Local\Programs\Python\Python37\DLLs
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib
C:\Users\priper\AppData\Local\Programs\Python\Python37
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages
C:\Users\priper\AppData\Roaming\Python\Python37\site-packages
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\win32
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\win32\lib
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\Pythonwin
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\mypackage
sdnplay.py
sdnplay.py
sdnplay.py
sdnplay
sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\testplay
C:\Users\priper\Desktop\testpackage
C:\Users\priper\Desktop\testpackage
错误:
ModuleNotFoundError: No module named 'testpackage'
File"C\Users\priper\Desktop\testplay\play_01.py", line 6, in <module> from testpackage import numberstuff
我可以看到 sys.path 中有 testpackage,我只是不知道为什么它被视为一个模块以及为什么我不能导入它>
预期输出为“9”。
【问题讨论】:
-
试试
from testpackage.numberstuff import *而不是from testpackage import numberstuff -
嗨 Anwarvic,同样的错误。
标签: python module package sys.path