0

I have created a python script e.g. HelloWorld.py.

Within this script it calls a second script GoodMorning.py via exec(open("GoodMorning.py").read()).

I have now created a .bat file to run my HelloWorld.py.

"C:\Users\myself\AppData\Local\Programs\Python\Python38-32\python.exe" "C:\Users\myself\Desktop\Test\HelloWorld.py"
pause

if I start the .bat file now it returns the error no such file or directory: GoodMorning.py

How can this be adjusted in order for the HelloWorld.py script to properly be able to start the GoodMorning.py script at the correct moment?

Do I have do make an adjustment in my HelloWorld.py script or the .bat file?

Neuron
  • 5,141
  • 5
  • 38
  • 59
dats
  • 3
  • 4
  • 1
    Running Python from Python is almost never a good idea. Refactor the code in the second script so you can `import` it. If you need them to run in parallel, look into `multiprocessing` – tripleee Dec 30 '21 at 10:48

1 Answers1

0

Are HelloWorld.py and GoodMorning.py in the same folder?

I'm not 100% sure, but looks like the problem is that you use a relative path to GoodMorning.py instead of an absolute path like "C:\Users\myself\Desktop\Test\GoodMorning.py", and your script tries to find GoodMorning.py in the current working directory (from where you execute the .bat script) instead of the directory where HelloWorld.py is. If that's the case, you have two options:

  • Use an absolute path to GoodMorning.py. This is a very fragile solution, it only works if no one else would ever install your script to a different folder than "C:\Users\myself\Desktop\Test".

    By the way, your .bat script is already very fragile, because it assumes your username and Python version ("C:\Users\myself\AppData\Local\Programs\Python\Python38-32\python.exe"). It's better to add Python to PATH, and than in your script just call it as "python.exe".

  • In your .bat script or HelloWorld.py, automatically change the current working directory to the location of HelloWorld.py before executing GoodMorning.py. This solution is more reliable, but a bit harder to code.

Expurple
  • 877
  • 6
  • 13
  • hi @Expurple,thanks for your input. This is just for me to "play around" so no potential usage on others. I managed to find a solution by changing the directory within the batch scrip. – dats Dec 30 '21 at 13:39