从其他目录导入python文件 [英] Importing python file from other directory

查看:60
本文介绍了从其他目录导入python文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下目录结构:

                                parentDirectory
                                       |
                   -----------------------------------------
                  |                                         |
                 Task                                    Configs
                   - pythonFile1.py                         - Config1.py
                                                            - Config2.py

配置文件在类中定义了很少的配置常量.

The Configuration files have few configuration constants defined inside a class.

现在,我想将配置文件从 Configs 目录导入到 Tasks 目录下的 python 文件中,并利用每个配置文件中类内部定义的常量.

Now, I want to import the configuration files from Configs directory into the python file under Tasks directory and make use of constants defined inside the class in each config file.

我曾尝试添加(在阅读了几个答案后)-

I had tried with adding(after reading through few answers) -

sys.path.insert(0,'/home/MyName/parentDirectory/Tasks')

在配置文件中.

由于我是 python 新手,我不知道我在添加上面几行时正确到什么程度.

Since I am new to python, I don't know to what extent I am correct in adding the above lines.

请帮忙!

推荐答案

我觉得你说反了:如果你想让你的任务"能够导入你的配置",你需要将代码添加到任务中将配置路径插入 sys.path.

I think you got it backward: if you want your "Tasks" to be able to import your "Configs", you need to add code to the Tasks to insert the Configs path into sys.path.

这篇关于从其他目录导入python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆