问题描述
我想将我的python版本从3.5升级到3.6。由于我使用的是,我已经下载并安装了最新版本,就像我之前使用的版本一样3.5。
I wanted to upgrade my python version from 3.5 to 3.6. Since I am using WinPython, I have downloaded and installed the recent version just as I did it before with version 3.5.
但是,如果我使用版本3.6,每当我导入自创模块时,我都会得到 ModuleNotFoundError
。一个最小的例子:我创建了一个文件 t1.py
,它只包含一个 pass
语句和一个文件 t2.py
包含以下代码:
However, if I use version 3.6 I get a ModuleNotFoundError
whenever I import a self-created module. A minimal example: I created a file t1.py
that contains only a pass
statement and a file t2.py
containing the following code:
import t1
print("done")
两个文件都在同一个文件夹中 D:\ MyProject \src
。现在当我使用python 3.5运行文件时,一切正常:
Both files are in the same folder D:\MyProject\src
. Now when I run the file with python 3.5, everything works fine:
'C:\Program Files\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\python.exe' D:\MyProject\src\t2.py
done
然而,使用python 3.6我得到
However, with python 3.6 I get
'C:\Program Files\WinPython-64bit-3.6.0.1Qt5\python-3.6.0.amd64\python.exe' D:\MyProject\src\t2.py
Traceback (most recent call last):
File "D:\MyProject\src\t2.py", line 6, in <module>
import t1
ModuleNotFoundError: No module named 't1'
我跑了出来问题可能是什么,并会欣赏新的灵感。
I ran out of ideas what the issue could be and would appreciate new inspiration.
推荐答案
这会有用吗? in t2.py
Would this work ? in t2.py
import os
__path__=[os.path.dirname(os.path.abspath(__file__))]
from . import t1
print("t2 done")
Python-3.6改变了它的工作方式,使用python.exe旁边的python._pth文件(而不是以前版本中的pyvenv.cfg)
Python-3.6 changes its way of working, with the "python._pth" file next to python.exe (instead of "pyvenv.cfg" in previous versions)
如果你不想修改你的source,那么你必须在Python._pth文件中添加D:\ MyProject \ src行,或者从python._pth位置添加它的相对路径。在我的例子中,它适用于:
If you don't want to modify your source, then you have to add "D:\MyProject\src" line in Python._pth file, or a relative path to it from python._pth location. in my example, it works with:
python36.zip
DLLs
Lib
.
..\test
import site
如果你没有系统安装的python,其他更简单的解决方案:将python._pth文件重命名为python.exe旁边的pythonzz._pth
Other, simpler solution if you have no system-installed python: rename the "python._pth" file, next to "python.exe", as "pythonzz._pth"
PythonWindows维护者刚刚写道,使用Python-3.6.0也可以使用更简单的解决方案。
The Python "Windows" maintainer just wrote that the simpler solution should be ok also with Python-3.6.0.
这篇关于ModuleNotFoundError:Python 3.6在Python 3.5中找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!