-3

There are answers for when both scripts are in the same directory.

However, I can't get any of the answers mentioned here working for a relative path. They are for when the full path is known.

I have the following folder hierarchy

enter image description here

i need to call and run sub.py in commander.py

the following is in commander.py

import importlib.util
spec = importlib.util.spec_from_file_location('sub.py', '..//main/sub/sub.py')
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.main()

it certainly recognized the directory as I can see the pycache 'folder getting created in sub folder.

However the sub script doesn't run. I have launched the sub script manually and i know it is functional.

the sub script has the following structure

import stuff

def main():
   do stuff
return

while true:
  main()
break

I am new to python. I have also tried:

import sys
    sys.path.insert(0, '..\\main\sub')

    import sub.py 

    sub.main()

i'm using py 3.7 there are no errors

Mel
  • 174
  • 1
  • 11

2 Answers2

1
import subprocess
import os
main_path = os.getcwd()
relative_path =r'..\\Main\sub'

filepath=os.path.join(main_path, relative_path[9:]) 
subprocess.call('python {filepath}', shell=True)
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
-1

Importing a file in python, runs that file.

Given this structure

-project
    -module
        -__init__.py
        -module_hello.py
    -__init__.py
    -hello.py

And these files

# module_hello.py
print('hello')

and

# hello.py
from project.module import module_hello

running hello.py would result in printing hello.

Işık Kaplan
  • 2,815
  • 2
  • 13
  • 28
  • this works if they are in the same directory. please refer to the question "relative path" "not in the same directory". this does not answer my question – Mel May 31 '19 at 23:20
  • In the question, in the picture you have a main folder and a subfolder where all the scripts are located in, am I incorrect? – Işık Kaplan May 31 '19 at 23:23
  • The picture shows commander.py and sub.py in two different folders. All the scripts are not in the same folder. the answer you have given does not work for relative path. Which is being asked in the question – Mel May 31 '19 at 23:25
  • 1
    You may want to edit your question then since the picture literally shows it as a subfolder and _says_ it is a subfolder and there is literally a line in between, and the "relative" path literally shows that sub.py is in subfolder which is inside main folder. – Işık Kaplan May 31 '19 at 23:32
  • kaplan relative path means the main folder's directory is not solid. hence why i have "'..\\main\sub" the .. means relative path. I can move the main folder anywhere and it would still reach sub folder – Mel May 31 '19 at 23:37
  • 1
    @Mel That relative path still shows that subfolder is inside mainfolder which then allows you to do `from subfolder import sub` regardless where the mainfolder is, either the subfolder is not always inside mainfolder, which then renders `..\\main\sub` irrelevant since it shows sub as being inside main, or the simple importing from a module would work. – Işık Kaplan May 31 '19 at 23:42