Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a python file in a different directory from another python file?

During the course of my python program, I have generated the following string

"D:\something_else.py"

Lets say there are other resources in D: that something_else.py requires.

How would I go about running something_else.py from my code, which let's say is being run from C:\Users\Someone\Desktop?

Right now I'm using the following:

from subprocess import call 
call(["python",pythloc])

This gives me an error as it's able to find only something_else.py and is unable to find the other resources that something_else.py needs which are in the same folder as something_else.py

like image 804
glopquestions Avatar asked Mar 16 '26 23:03

glopquestions


1 Answers

Besides the fact that you need to be careful with "\" and instead of :

   locationDir="D:\something_else.py"

prepend your string with r:

   locationDir=r"D:\something_else.py"

you can simply set the PYTHONPATH environment variable to point to the directory in which sit the other modules.

See here for additional informations :

https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH

like image 199
NabilG Avatar answered Mar 18 '26 13:03

NabilG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!