gpt4 book ai didi

python 3。导入错误 : no module named 'myfile'

转载 作者:行者123 更新时间:2023-12-04 19:07:52 25 4
gpt4 key购买 nike

iMac-Mark:~ Mark$ python3
Python 3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import myfile
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'myfile'

我是 Python 和编程的新手。我读了一点关于 sys.path , __PATH__但我什么都不明白。

最佳答案

解决方案:

  • 你有吗__init__.py myfile.py 附近的文件?
  • 您在 sys.path 中有您的目录吗? ? (如果没有像这样添加它 sys.path.append('path/to/python/code'))
  • 如果你尝试从 python 环境导入它,你需要在这个文件附近运行 python shell

  • 您也可以尝试运行此代码
    import sys
    print(sys.path)

    在您的输出中,您可以看到 python 看到的所有目录的列表。如果您的文件在任何其他目录中,您必须更改目录或 python sys.path
    你可以这样做
    $ python3
    >>> import sys
    >>> sys.path.append('/all/path/to/Desktop/Python')
    >>> import myfile

    或者像这样
    $ cd ~/Desktop/Python
    $ python3
    >>> import myfile

    关于 python 3。导入错误 : no module named 'myfile' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20099481/

    25 4 0