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
C:\Users\Usersname\Desktop\Folder\MyScript\script-1.py
C:\Users\Usersname\Desktop\Folder\MyScript\script-2.py
C:\Users\Usersname\Desktop\Folder\MyScript\script-3.py
main.py
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:
import script1
import script2
import script3
script1.run()
script2.run()
script3.run()
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
subprocess.run()
subprocess.call()
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:
C:\Users\Usersname\Desktop\Folder\MyScript\script1 .py:
Code:import time def run(): print('working on something in script1') time.sleep(2) print('done') if __name__ == '__main__': run()
Code:import sys sys.path.append("C:\Users\Usersname\Desktop\Folder\MyScript") import script1 script1.run()
Last edited by _Al_; 26th May 2023 at 19:13.
-
If you write an example in script1.py
print("script1 .. Is Rading ...") it works
But if I write my project I get an error
You can try it
https://app.box.com/s/6ay61i8of2eikxna5c3lzd4m4e58l31p -
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:
Code:import sys sys.path.append(r"C:\Users\Usersname\Desktop\Folder\MyScript") #or whatever path is correct import script1 script1.run()
Code: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 21:44.
-
thanks for the clarification . It works now
Last edited by D.LUFFY; 27th May 2023 at 07:30.
Similar Threads
-
anyone know if the following python script work ?
By DRMdec in forum Video Streaming DownloadingReplies: 2Last Post: 21st Feb 2023, 19:02 -
yt-dlp in Python
By blanc in forum Video Streaming DownloadingReplies: 1Last Post: 4th Jan 2023, 05:37 -
trying to get yt-dlp module for Python
By Skaperen in forum LinuxReplies: 3Last Post: 15th Jun 2022, 11:12 -
how to merge multiple subtitle files in python?
By D.LUFFY in forum ProgrammingReplies: 2Last Post: 7th Nov 2021, 08:24 -
Help Needed With Python ffprobe Script
By chris319 in forum ProgrammingReplies: 11Last Post: 28th Jun 2019, 10:15