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
    

    10-04 10:53
    查看更多