I'm having a problem adding a specific path
If you set a specific path, it won't work. But if you put a script name without a path, it works, but all files script-1, script-2, and script-3 must be in the same folder. All I want is a short path
Code:import os os.system("script-1.py && script-2.py && script-2.py")
Code:Desktop │ │ ├── Folder "main.py inside Folder" │ │ │ └ MyScript │ │ │ ├─── script-1.py │ │ │ ├─── script-2.py │ │ │ └─── script-3.py
this is what I want
Code:import os os.system("./MyScript/script-1.py && ./MyScript/script-2.py && ./MyScript/script-2.py")
I tried this does not work
Any other suggestionsCode:import os os.system(Python "./MyScript/script-1.py && ./MyScript/script-2.py && ./MyScript/script-2.py") os.system(Py "./MyScript/script-1.py && ./MyScript/script-2.py && ./MyScript/script-2.py") os.system("C:\\Users\\Usersname\\Desktop\\Folder\\MyScript\\script-1.py ") os.system(Python "C:/Users/Usersname/Desktop/Folder/MyScript/script-1.py ")
+ Reply to Thread
Results 1 to 7 of 7
---python workflows are different, python scripts are imported to main script and then you call their functions or class methods or load their globals, so importing another python script into main one is called importing a module,
---you do not have to set up paths as long scripts are in same folder as main.py, if in different folder you just tell python where to look for those scripts if threre are imported
---do not use "-" in python script names, it is not allowed
---setup some run function (whatever name, could be main also) in those scripts that start doing things. This forces you generally to create functions, it is a good habit. Then in main.py:
but that is weird, if code is repetitive like this it usually means it is not well set up, it could be done differently
Thanks brother for reply
but I want it to run on a specific path. to add it to my project
I tried as well module subprocess
If you explain to any other module, no problem
Using subprocess or os.system etc. is kind of an awkward way to run a python script from within other python script.
You can specify a folder for python to look into if importing a module from some place (and it is not in same directory or site-packages directory).
This is how it is set up in python:
import time def run(): print('working on something in script1') time.sleep(2) print('done') if __name__ == '__main__': run()
import sys sys.path.append("C:\Users\Usersname\Desktop\Folder\MyScript") import script1 script1.run()
Last edited by _Al_; 26th May 2023 at 20:13.
because working directory is from my_main_script.py, not script1.py, that png image could not be found,
path for images cannot be relative in imported module, this is what I meant, you have to construct functions and that basically means to pass data into those functions. Data in your case are: png image absolute path or cv.threshold arguments etc.
if insisting that png image is always in the same folder as script1, make path always relative to script1 file, not current working directory, also note, include r for those paths, I forgot to include it above:
import sys sys.path.append(r"C:\Users\Usersname\Desktop\Folder\MyScript") #or whatever path is correct import script1 script1.run()
import cv2 as cv import numpy as np from matplotlib import pyplot as plt from pathlib import Path FILE_DIR = Path(__file__).parent def run(): img_filepath = str(FILE_DIR / 'test.png') img = cv.imread(img_filepath, cv.IMREAD_GRAYSCALE) assert img is not None, "file could not be read, check with os.path.exists()" ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY) cv.imwrite(str(FILE_DIR / 'Output.png'), thresh1) if __name__ == '__main__': run()
but anyway, it looks like you would design a script for each png image? That does not look right. You'd need a generic function for that and pass an image etc.
Last edited by _Al_; 26th May 2023 at 22:44.
thanks for the clarification . It works now
Last edited by D.LUFFY; 27th May 2023 at 08:30.