代码之家  ›  专栏  ›  技术社区  ›  Frankenstein

Python3.5导入程序。py错误

  •  0
  • Frankenstein  · 技术社区  · 7 年前

    无法在同一文件夹中导入python程序

    所以我开始只是看看它在哪里好玩。py这可能是错误的,或者它不能只是找到现在的打印错误,所以我需要帮助。

    我在test67上的代码。py:

    代码开始

    import os.path
    
    if not os.path.exists(fun.py):
         print("Alarm")
    
    if os.path.exists(fun.py):
         print("import")
    

    代码结束

    1 回复  |  直到 7 年前
        1
  •  1
  •   Pintang    7 年前

    要导入同一目录中的文件,请使用:

    import fun
    

    os.walk :

    import os
    
    def find(nameofFile, path):
        for root, dirs, files in os.walk(path):
            if nameofFile in files:
                return os.path.join(root, nameofFile)
    

       import sys
       sys.path.insert(0, pathtoFile)
       from fun import *
    

    希望这有帮助。