简体   繁体   English

如何从另一个文件夹导入(Python)

[英]How to import from another folder (python)

I am running from folder A (the name doesn't matter), and in this folder there is another folder called bin. 我从文件夹A运行(名称无关紧要),并且在此文件夹中还有另一个名为bin的文件夹。

In bin I have the .py file "functions", how do I import it if I'm in folder A. 在bin中,我有.py文件“ functions”,如果我在文件夹A中,该如何导入它。

Reminder: main.py -> location: A 提醒:main.py->位置:A

Reminder: functions.py -> location: A/bin 提醒:functions.py->位置:A / bin

Thanks. 谢谢。

You need to create an empty file bin/__init__.py . 您需要创建一个空文件bin/__init__.py This will tell python that bin is a "package" and should look for modules there. 这将告诉python bin是一个“包”,应在此处查找模块。

from bin import functions

If you want to do something like from bin.functions import * you can add which functions you want to load defining them in __init__.py (more here ) 如果您想执行from bin.functions import * ,则可以在__init__.py添加要加载的函数,以对其进行定义(更多信息请参见此处 )。

# __init__.py
__all__ = ["fun1", "fun2"]
# doing import * will load those 2

You can find more info here . 您可以在此处找到更多信息。

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

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