简体   繁体   English

更改导入模块的工作目录

[英]Change working directory of a imported module

i am having trouble with some modules i want to import, so let's me put a sample to explain better. 我在要导入的某些模块上遇到了麻烦,所以让我放一个示例来更好地解释。

proyect/
  helpers/
    config.py
  locations/
    loc1.py
  pages/
    page1.py
  Tools/
    myTool.py

So whats happening is that in myTool.py i'm importing page1.py that import from loc1.py . 因此,whats发生的是,在myTool.py我正在导入page1.py从导入loc1.py to do that i'm appendind '../ ' to sys.path . 为此,我将'../'添加到sys.path The problem is that in loc1 is imported config.py and initialize, when it do that it working dir is TOols/ but i need it to be my proyect dir. 问题是在loc1中导入了config.py并初始化,当它这样做时,它的工作目录是TOols /,但是我需要它作为我的proyect目录。

Modules don't have working dirs, only the program as a whole does. 模块没有工作目录,只有整个程序有。

You should add the proyect dir to sys.path at the start of the script, then import loc1 from locations. 您应该在脚本开始时将proyect目录添加到sys.path,然后从位置导入loc1。

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

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