VideoHelp Forum
+ Reply to Thread
Results 1 to 7 of 7
Thread
  1. 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


    Code:
    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 ")
    Any other suggestions
    Quote Quote  
  2. ---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
    Quote Quote  
  3. 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
    Quote Quote  
  4. 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()
    my_main_script.py:
    Code:
    import sys
    sys.path.append("C:\Users\Usersname\Desktop\Folder\MyScript")
    import script1
    script1.run()
    This way it is a setup for two ways of usage. script1.py can be used as a standalone, if you just run it itself, __name__ will be '__main__' , because python recognize it is a main script and variable __name__ has '__main__' value and it will run that block and execute that run() function. If script1 is imported on the other hand into other script, __name__ will not be equal to '__main__' . So it is just imported and you actually trigger it yourself to run that function by calling script1.run() in your my_main_script.py. You control what is run and what not after importing a script. So python working scripts should be all different working functions within separate functions. You can use it like that when you test something or develop something, but when done , it should be all put into a function so that just developed module could be called from other script. It is not good if you have a python script and it just start to run everything. When importing script like that, everything would start to run when imported. Not good.
    Last edited by _Al_; 26th May 2023 at 19:13.
    Quote Quote  
  5. 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
    Quote Quote  
  6. 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()
    and script1:
    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()
    this way script1.py could be imported and also can act as a standalone, if you double click it, it will work as well,
    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.
    Quote Quote  
  7. thanks for the clarification . It works now
    Last edited by D.LUFFY; 27th May 2023 at 07:30.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!