简体   繁体   English

如何从上面一个文件夹中的python文件导入函数?

[英]How to import function from python file from one folder above?

This is a follow up to " How to import a Python class that is in a directory above? ", which gives how to import a file from the parent directory:这是“ 如何导入上面目录中的 Python 类? ”的后续内容,其中介绍了如何从父目录导入文件:

from .. import module

I to get a specific function from a file in the directory above.我从上面目录中的文件中获取特定功能。

This is my attempt: There is a file in the higher directory called topLevel.py which has a function called test_function .这是我的尝试:在更高的目录中有一个名为topLevel.py的文件,它有一个名为test_function的函数。

from .. import topLevel
from topLevel import test_function

This is the error message:这是错误消息:

ValueError: attempted relative import beyond top-level package
import sys 
sys.path.insert(0, "./../" )
from topLevel import test_function

test_function()

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

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