简体   繁体   English

从其他目录导入python文件

[英]Importing python file from other directory

I have a following directory structure: 我有以下目录结构:

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

The Configuration files have few configuration constants defined inside a class. 配置文件在类内定义的配置常量很少。

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. 现在,我想将配置文件从Configs目录导入到Tasks目录下的python文件中,并使用每个配置文件中的类内部定义的常量。

I had tried with adding(after reading through few answers) - 我尝试添加(阅读一些答案后)-

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

inside the config files. 里面的配置文件。

Since I am new to python, I don't know to what extent I am correct in adding the above lines. 由于我是python的新手,所以我不知道在多大程度上正确添加上述行。

Please help! 请帮忙!

我认为您倒退了:如果您希望“任务”能够导入“配置”,则需要向“任务”添加代码以将Configs路径插入sys.path

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

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