3

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

8
  • 1
    does your folder has __init__.py Commented Sep 25, 2018 at 16:23
  • 3
    Set cwd in call: call([xxx], cwd="D:\\") Commented Sep 25, 2018 at 16:26
  • 1
    Change pythloc for the abs PATH to the python file you Want to call. If you Want to use relativa paths use os. Path(file) to build your PATH. That shows the loción of the python file you are running Commented Sep 25, 2018 at 16:43
  • 1
    What kind of resources by the way, do you have something like D:\images\*.jpg" ? Seems like you might do better to make something_else.py a little smarter about where to look for resource(s) but hard to say without know about their nature (are they in the same directory as something_else.py? subdirectories?) Commented Sep 25, 2018 at 18:20
  • @VaibhavVishal No, my project is quite large and doing that would cause more problems than it would solve Commented Sep 26, 2018 at 9:53

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Same with mine. Looks like it's some downvote and run show going on. I would personally appreciate if downvoter leaves a comment telling what is wrong, which may help the person writing the answer, as well as anyone who reads it later.
Agreed on "why downvotes", they both look good to me so +1. I just went through all kinds of headache trying to understand this and seems like you both explain it well and concisely. (I ended up using PYTHONPATH myself because I have lots of shell scripts calling things so that seemed slightly more clear than tweaking sys.path in python code. If I had just *.py files I'd probably tweak sys.path )
ahh, I just saw the OP's part about "other resources" so apparently the "somethingelse" code @glopquestions wants to call assumes those resources are somewhere relative to the whatever whatever somethingelse lives in. Which makes me think the other program needs to be smarter about finding those resources.
@NabilGhodbane thanks for the answer! Your method works generally, just not for my particular usecase as somethingelse.py is intended to reside in different drives and different locations. Not sure why you were downvoted, it's a good solution.
2

If you can have the mentioned folder as part of the project itself, you can turn that into python package, simply by adding an empty file in the folder named __init__.py

If it is on some other path like "D:\some_directory\", which has something_else.py along with other dependencies, do this along with your import statements:

sys.path.append(r"D:\some_directory\")
from something_else import some_useful_function, some_useful_class

It will add the mentioned directory to your sys.path, and then you can import anything from python files kept there, and just call the functions as usual.

If you are using any IDE like PyCharm, it may still show you unresolved errors for the 2nd line, as part of import checks. But the code will simply work when you run it.

It's almost never a good idea to call a python script from other(python something.py), when you can just import and call functions, with a better control & error checking.

3 Comments

It would be great if downvoter could also call out the reason for disagreement.
Again, I'm not sure why your answer was downvoted - it's a way to solve the problem by getting rid of the problem entirely. I can see that particular workaround working, but definitely not for my usecase as importing individual functions and classes from somethingelse.py is definitely not an option for me
Yeah, never mind that downvote. Whoever downvoted all these answers, was probably not interested in coming back and reading the comments. :D Btw, If it works for you, you can have a main function inside the something_else.py, which basically does the same thing, that your script does. Just wanted to highlight that, calling a function gives you more control in terms of error checking, etc. But you obviously are the best judge of your use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.