Importing python script from some directory with dependent scripts

124 Views Asked by At

I have the following problem and would like to ask you what is the best way to solve it.

I the following structure of files:

  1. Main Folder 1.1 Subfolder1 1.1.1 SubSubfolder1

I have main python script in main folder, and other sripts in subfolders, which are importing some other scripts from subsubfloders. I need to run the main script in Main Folder, which is importing scripts from subfolders. Each subfolder is standalone, and I just need to import and run scripts available in subfolders (which themselves are importing some other scripts).

I am getting an error because when I import scripts, the running directory is not changing automatically when the script is called.

So I need that if the script is imported, the dependent scripts were imported from the relative path.

Thanks

1

There are 1 best solutions below

0
Pauel3312 On

I believe using sys.path.append method from sys built-in module should work.
Here is the syntax :

sys.path.append(YourPath)
import YourScript

please note that your path should not lead to a file, but to a folder