简体   繁体   English

如何在python中导入文件?

[英]How do I import a file in python?

I am trying to import a python file like this我正在尝试导入这样的 python 文件

import tiny

tiny.create_window('test')

and inside tiny is里面很小的是

from tkinter import *

def create_window (Name='Window'):
    window = Tk()
    window.title(Name)
    return window

but when I run it gives an error但是当我运行它时会出错

Traceback (most recent call last):
  File "C:\Users\Harry\Documents\python files\tests\test4.py", line 1, in <module>
    import tiny
ModuleNotFoundError: No module named 'tiny'

Is your "tiny.py" file in the same directory as your main "test4.py" file?您的“tiny.py”文件是否与您的主“test4.py”文件在同一目录中? In this case are both your tiny.py and test4.py files in在这种情况下,您的 tiny.py 和 test4.py 文件都在

C:\\Users\\Harry\\Documents\\python files\\tests\\ C:\\Users\\Harry\\Documents\\python files\\tests\\

Python interpreter doesn't know what directories to search in to find the file. Python 解释器不知道要搜索哪些目录才能找到文件。 Thus both your files must be in the same place for the interpreter to know where to find them.因此,您的两个文件必须位于同一位置,以便解释器知道在哪里可以找到它们。 Alternatively you can look up some tutorials on creating modules and packages but that is a bit more advanced.或者,您可以查找一些有关创建模块和包的教程,但这更高级一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM