Python3.5 导入 program.py 错误
Python3.5 import program.py error
无法在同一文件夹中导入 python 程序
C:\Users\User_Unknowed\Desktop\Marvin
所以我已经开始查看它是否 fun.py 可能是错误的或者它不能只是找到现在打印错误所以我需要帮助。
我在 test67.py 上的代码:
代码开始
import os.path
if not os.path.exists(fun.py):
print("Alarm")
if os.path.exists(fun.py):
print("import")
代码结束
任何解决方案都会很好。请尽快回复,thx.
要在同一目录中导入文件,请使用:
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)
如果您发现此文件不在同一目录中,您可以使用 sys.path.insert() 添加要导入的文件的路径:
import sys
sys.path.insert(0, pathtoFile)
from fun import *
希望对您有所帮助。
无法在同一文件夹中导入 python 程序
C:\Users\User_Unknowed\Desktop\Marvin
所以我已经开始查看它是否 fun.py 可能是错误的或者它不能只是找到现在打印错误所以我需要帮助。
我在 test67.py 上的代码:
代码开始
import os.path
if not os.path.exists(fun.py):
print("Alarm")
if os.path.exists(fun.py):
print("import")
代码结束
任何解决方案都会很好。请尽快回复,thx.
要在同一目录中导入文件,请使用:
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)
如果您发现此文件不在同一目录中,您可以使用 sys.path.insert() 添加要导入的文件的路径:
import sys
sys.path.insert(0, pathtoFile)
from fun import *
希望对您有所帮助。