Python 3.x Python3导入模块无法根据文档工作

Python 3.x Python3导入模块无法根据文档工作,python-3.x,Python 3.x,所以我通常使用Python2.7.x,但今天我决定使用Python3,看看我是否想升级。我下载并安装了Python3.6.2,但到目前为止我还不相信 因此,我试图从位于同一目录中的另一个Python脚本/模块导入一些代码。下面是一个例子 目录结构 WorkingDir |-- main.py |-- module_a.py main.py import os import module_a if(__name__ == "__main__"): module_a.sayHi() d

所以我通常使用Python2.7.x,但今天我决定使用Python3,看看我是否想升级。我下载并安装了Python3.6.2,但到目前为止我还不相信

因此,我试图从位于同一目录中的另一个Python脚本/模块导入一些代码。下面是一个例子

目录结构

WorkingDir
|-- main.py
|-- module_a.py
main.py

import os
import module_a

if(__name__ == "__main__"):
    module_a.sayHi()
def sayHi():
    print("Hi There!")
模块a.py

import os
import module_a

if(__name__ == "__main__"):
    module_a.sayHi()
def sayHi():
    print("Hi There!")
如果我运行以下命令:

python main.py
我得到以下错误:

Traceback (most recent call last):
    File "main.py", line 2, in <module>
        import module_a
ModuleNotFoundError: No module named 'module_a'
因此,我也尝试设置了
PYTHONPATH
,但仍然出现了错误。我还尝试了其他各种格式,如来自。。。导入…但未成功。我是遗漏了什么还是文档有问题

编辑: 一些用户指出,我应该在
WorkingDir
中包含
\uuu init\uuuu.py
。我已经试过了,但它对我不起作用。此外,文档使我听起来似乎不再需要使用
\uuuu init\uuuu.py
文件来进行导入,对于这个特定的测试,我更希望尽可能避免导入

我还想说的是,我使用Python2.7.13尝试了完全相同的文件,没有任何更改,并且在
WorkingDir
中使用和不使用
\uu init\uuuuuuuuuuy.py
时,一切都按预期工作

更新:
由于其中一位评论者无法再现该问题,我尝试运行他/她正在使用的Python 3版本(Python 3.5.2),但没有出现错误。使用该版本,一切都按预期进行。我相信这是Python3.6.2中的一个bug,并在PythonBug tracker上提交了一份bug报告:

它适合我。这里有一个链接

它适合我。下面是一个链接,Python3的工作原理如文档中所述。问题是我下载了“嵌入式”Python包,根据文档,该包从Python 3.5开始提供

在python3文档的“findingmodules”部分中,有一句我遗漏了,导致导入错误的句子

To completely override sys.path, create a ._pth file with the same 
name as the DLL (python36._pth) or the executable (python._pth) and
specify one line for each path to add to sys.path. The file based on
the DLL name overrides the one based on the executable, which allows 
paths to be restricted for any program loading the runtime if desired.

When the file exists, all registry and environment variables are 
ignored, isolated mode is enabled, and site is not imported unless one
line in the file specifies import site. Blank paths and lines starting
with # are ignored. Each path may be absolute or relative to the
location of the file. Import statements other than to site are not
permitted, and arbitrary code cannot be specified.

标准Python安装没有使示例按预期工作的
\pth
文件。另外,从嵌入式发行版中删除
。\u pth
文件可以使我发布的示例正常工作。尽管嵌入式发行版用于特定目的,并且只能根据上面链接的Python文档使用。

Python3的工作原理如文档中所述。问题是我下载了“嵌入式”Python包,根据文档,该包从Python 3.5开始提供

在python3文档的“findingmodules”部分中,有一句我遗漏了,导致导入错误的句子

To completely override sys.path, create a ._pth file with the same 
name as the DLL (python36._pth) or the executable (python._pth) and
specify one line for each path to add to sys.path. The file based on
the DLL name overrides the one based on the executable, which allows 
paths to be restricted for any program loading the runtime if desired.

When the file exists, all registry and environment variables are 
ignored, isolated mode is enabled, and site is not imported unless one
line in the file specifies import site. Blank paths and lines starting
with # are ignored. Each path may be absolute or relative to the
location of the file. Import statements other than to site are not
permitted, and arbitrary code cannot be specified.


标准Python安装没有使示例按预期工作的
\pth
文件。另外,从嵌入式发行版中删除
。\u pth
文件可以使我发布的示例正常工作。尽管嵌入式发行版用于特定目的,并且只能根据上面链接的Python文档使用。

init.py在您的文件夹中丢失。在文件夹中添加一个init.py,它将解决问题
\uuuuu init\uuuuuuu.py
^^我会编辑问题,但我也这样做了,但仍然会出现错误。无法在Python 2.7.12或Python 3.5.2中重现。您实际上是从
WorkingDir
中运行Python吗?我是从
WorkingDir
中运行Python的。我将尝试获取Python3.5.x,并查看是否存在相同的问题。您的文件夹中缺少init.py。在文件夹中添加一个init.py,它将解决问题
\uuuuu init\uuuuuuu.py
^^我会编辑问题,但我也这样做了,但仍然会出现错误。无法在Python 2.7.12或Python 3.5.2中重现。您实际上是从
WorkingDir
中运行Python吗?我是从
WorkingDir
中运行Python的。我将尝试获取Python3.5.x,看看是否会遇到同样的问题。您在Windows计算机上试用过吗?我没有任何特殊的设置。只需将嵌入式软件包解压缩到我的工具目录中,并将其添加到我的PATH环境变量中。该软件包在Windows计算机(Win 10,64位)上运行良好。我的意思是在您的本地硬盘(即,不在韦小宝web工具内)上进行了尝试。它在Python 3.6.1和3.6.2上在Windows 7计算机上运行,64位Python你在Windows机器上试过了吗?我没有任何特殊的设置。只需将嵌入式软件包解压缩到我的工具目录中,并将其添加到我的PATH环境变量中。该软件包在Windows机器(Win 10,64位)上运行良好。我的意思是在您的本地硬盘上尝试过它(即不在该软件包的web工具中)。它在Python 3.6.1和3.6.2上运行,在Windows 7机器上运行,64位Python