简体   繁体   English

从父目录导入 function

[英]Import function from parent directory

I have a directory which contains a bunch of function and another folder which contains my main project which I would like to be able to run from the terminal: kf_sine_demo.py .我有一个目录,其中包含一堆 function 和另一个文件夹,其中包含我希望能够从终端运行的主要项目: kf_sine_demo.py

在此处输入图像描述

When I run line by line the code from VS-Code (using Shift + Enter ), everything works fine.当我逐行运行 VS-Code 中的代码时(使用Shift + Enter ),一切正常。 In particular I can import the functions for further use:特别是我可以导入功能以供进一步使用:

from EKFUKF_Py import lti_disc, kf_predict, kf_update, rts_smooth

However, when I run the file from the terminal:但是,当我从终端运行文件时:

python kf_sine_demo.py

I get the following error:我收到以下错误:

Traceback (most recent call last):
  File "EKFUKF_Py/demo/kf_sine_demo/kf_sine_demo.py", line 16, in <module>
    from EKFUKF_Py import lti_disc, kf_predict, kf_update, rts_smooth
ModuleNotFoundError: No module named 'EKFUKF_Py'

I see solutions which include specifying the full path.我看到了包括指定完整路径的解决方案。 I have strong preference for relative imports.我对相对进口有强烈的偏好。


UPDATE:更新:

This solution has been most useful to me: https://stackoverflow.com/a/37193862/4576194 .该解决方案对我最有用: https://stackoverflow.com/a/37193862/4576194

  • Run python -m EKFUKF_Py.demo.kf_sine_demo.kf_sine_demo form the parent directory of EKFUKF_Py从 EKFUKF_Py 的父目录运行python -m EKFUKF_Py.demo.kf_sine_demo.kf_sine_demo EKFUKF_Py

But, that's not quite what I want.但是,这不是我想要的。 I want to be able to run python kf_sine_demo.py from kf_sine_demo directory and I want it to know that the functions it needs to import are located 2 levels up.我希望能够从kf_sine_demo目录运行python kf_sine_demo.py并且我希望它知道它需要导入的函数位于 2 级以上。

You have to understand how python path works.您必须了解 python 路径的工作原理。 This is a list of directories where python looks for the modules you try to import, you can display it uing the sys.path command.这是 python 查找您尝试导入的模块的目录列表,您可以使用sys.path命令显示它。 There are a bunch of directories that python automatically add to, like path\to\your_python_install\lib , and python also add the working directory when you run a script. python 会自动添加一堆目录,例如path\to\your_python_install\lib ,并且 python 在运行脚本时也会添加工作目录。

This means that when you do:这意味着当您这样做时:

python kf_sine_demo.py

The parent directory of this file (kf_sine_demo) is added to the path, but the EKFUKF_Py is not.该文件的父目录 (kf_sine_demo) 已添加到路径中,但EKFUKF_Py未添加。 Thus python can not find the modules in it.因此 python 找不到其中的模块。

From here, two solutions.从这里,两个解决方案。 Either you manually add this directory (which I find a little ugly, but it works):要么你手动添加这个目录(我觉得它有点难看,但它有效):

sys.path.append("path/to/EKFUKF_Py")

Or you ensure to always run your files from the main directory, through a main.py file, for example.或者,您确保始终从主目录运行文件,例如通过main.py文件。 from there, you will be able to call every submodule in this directory.从那里,您将能够调用此目录中的每个子模块。

More here: https://www.devdungeon.com/content/python-import-syspath-and-pythonpath-tutorial更多信息: https://www.devdungeon.com/content/python-import-syspath-and-pythonpath-tutorial

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

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