The easiest answer is probably to change your working directory, then call the second .py file from where it is:

python a.py && cd testA && python ../b.py

Of course you might find it even easier to write a script that does it all for you, like so:

Save this as runTests.sh in the same directory as a.py is:

#!/bin/sh
python a.py
cd testA
python ../b.py

Make it executable:

chmod +x ./runTests.sh

Then you can simply enter your directory and run it:

./runTests.sh
Answer from 3D1T0R on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how to execute .py file outside of it's residing directory?
r/learnpython on Reddit: How to execute .py file outside of it's residing directory?
February 10, 2022 -

This question has been absolutely beat to death. I know this because I spent quite some time googling it and not two answers are the same. I think this is partly because there are many use cases and the language / tech specific term barriers for us noobs. It's seriously infuriating!

So, I'll try to explain my use case. I simply want to be able to run my .py scripts from PowerShell (on win10) in other directories than where the script resides. This is because I want to do things with the files of those other dirs, while getting information from PowerShell console.

I know one solution is to compile into executables and make a batch script, but I wonder if there's a way to avoid having to go through that every time I make a little change to the script. Also it seems parameters does not work this way and console output is not visible (or it opens and closes).

I'm open to any suggestions!

Thanks for reading my question / rant :)

Discussions

python - Command line execution in different folder - Stack Overflow
I'm calling a command line program in python using the os.system(command) call. How can I call this command passing a different folder for execution? There is a system call for this? Or I should s... More on stackoverflow.com
🌐 stackoverflow.com
Run python script in subfolder
Hi, Please check the following picture which explains my questions. In Linux, in Top Folder, if I run python Scripts/SC1.py, the error is:ModuleNotFoundError: No module named 'package1' if I create a soft link in Top Folder using command: ln -s Scripts/SC1.py SC1.py, and then run command: python ... More on discuss.python.org
🌐 discuss.python.org
5
0
July 22, 2022
Running Executable in a different directory in python - Stack Overflow
I have an executable that pulls an image off of a camera and saves it to the current directory. I have a python application that calls this executable. Which is located in a different directory. ... More on stackoverflow.com
🌐 stackoverflow.com
function - Running a python file within a different directory - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
May 28, 2021
Top answer
1 of 4
10

No you don't need to use:

cd home/directoryA/directoryB/directoryC/DirectoryD
./somefile 

You can simply run the command by prefixing it with its path:

/home/directoryA/directoryB/directoryC/DirectoryD/somefile

Because you are already in the /home/directoryA you can use the current directory shortcut . and run the command like this:

./directoryB/directoryC/DirectoryD/somefile

I noticed OP has expanded scope via comments under other answers. Here is some additional information:

  • To find out where somefile is located use: locate somefile.
  • If somefile was added today you need to first update the locate database by running sudo updatedb.
  • When there are multiple versions of somefile located in the PATH you can find out which one is executed first use which somefile.
  • If you want to run somefile without specifying a directory name in front put it in the path. To check the path use echo $PATH. Common path locations to put somefile are /usr/local/bin (if it uses sudo powers) and /home/your_user_name/bin (you might have to create the directory first).
  • You can also add /home/directoryA/directoryB/directoryC/DirectoryD/ to your path but that would be highly unusual. However you could then simply type somefile no matter what directory you are in and it will run.
  • Of course somefile must be executable which you set with the command: chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile
2 of 4
3

Sure! If somefile is marked as executable, you can run it with

~/directoryA/directoryB/directoryC/DirectoryD/somefile

Want to know if somefile is executable? Go to its directory and run

find . -maxdepth 1 -perm -111 -type f

to see all the executables in that directory.

🌐
Server Fault
serverfault.com › questions › 1129330 › how-to-run-a-python-script-in-a-specific-directory-automatically
cron - How to run a python script in a specific directory automatically? - Server Fault
April 22, 2023 - Then Execute the Python Script. It will run in the Directory X. # My Bash Wrapper Script cd X # use the necessary Directory here python MyPythonScript.py · (2) In your Python Script , import module "os" & use "os.chdir" to Change Directory & then continue with the Python Script.
🌐
YouTube
youtube.com › watch
Run python file from any directory - YouTube
Run python file from any directoryLearn how to run python files from any directory in your system.Steps : https://gist.github.com/umangahuja1/51da3a453803f1f...
Published   July 12, 2018
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
running a script from another folder - Python
December 3, 2021 - $ python /home/pi/Desktop program.py python is looking for a /home/pi/Desktop/__main__.py file to execute it by passing it a command line argument too (in sys.argv) - the string "program.py"
🌐
Bioinformatics Answers
biostars.org › p › 205385
command line python script run on a file in different directory
September 23, 2019 - python script.py -C ~/Users/admin/Desktop/folder/file.txt StopIteration: because of missing file (file.txt) python • 12k views
Find elsewhere
🌐
Quora
quora.com › Do-all-Python-scripts-need-to-be-run-from-the-same-folder
Do all Python scripts need to be run from the same folder? - Quora
Answer (1 of 4): No - they can be executed from any directory you want, and often are. The only thing you have to do is ensure that if you have any modules on your pc which aren’t installed in pip, and are shared across mutliple applications ...
🌐
Medium
medium.com › @andrewdass › executing-commands-in-a-python-file-from-other-python-files-in-the-same-directory-7b7e86dd6dc1
Executing Commands in a Python File from other Python Files in the Same Directory | by Andrew Dass | Medium
December 30, 2023 - As shown above, in the “def run_test2() function”, the statement returns the contents of “test2.py”. Returning content from another file can be done by declaring in the following format: “filename.functionname()”. As shown in “test1.py”, the return statement is “test2.run_a_function()” since test2 is the file name and the function within test2 is called “run_a_function()”. ... “test2.py” contains the function “run_a_function” and returns the statement “Just test 2 running” if it ran successfully. When running “test1.py” in the same directory as “test2.py”, the following will occur:
🌐
YouTube
youtube.com › logicgpt
how to run python script from different directory - YouTube
Download this code from https://codegive.com Title: Running Python Scripts from Different Directories: A Step-by-Step TutorialIntroduction:Running Python scr...
Published   January 19, 2024
Views   83
🌐
Python.org
discuss.python.org › python help
Run python script in subfolder - Python Help - Discussions on Python.org
July 22, 2022 - Hi, Please check the following picture which explains my questions. In Linux, in Top Folder, if I run python Scripts/SC1.py, the error is:ModuleNotFoundError: No module named 'package1' if I create a soft link in Top Folder using command: ln -s Scripts/SC1.py SC1.py, and then run command: python ...
🌐
Super User
superuser.com › questions › 1133003 › running-python-script-in-scripts-from-different-working-directory-in-windows
Running Python script in Scripts from different working directory in Windows - Super User
October 9, 2016 - You can find these scripts in your python Script directory (usually C:\PythonXY\Scripts). You need either to create batch file to run them, or use Python interpreter:
🌐
Stack Overflow
stackoverflow.com › questions › 67732136 › running-a-python-file-within-a-different-directory
function - Running a python file within a different directory - Stack Overflow
May 28, 2021 - You don't need to import anything, just reference the folder name in main.py. To make it more robust, you should probably use a relative file otherwise you might get some odd results depending on where main.py is called from. import os.path from subprocess import call d = os.path.dirname(os.path.realpath(__file__)) # application folder call(["python3", f"{d}/appFolder/test.py"])