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

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
windows - Run python script from another directory - Stack Overflow
The PATH environment variable contains all directories where programs can be found, not input files. That seems to be your misunderstanding. ... The interpreter doesn't search PATH for scripts. You'll have to run the script directly. Make sure the .py file association is configured correctly to use the "Python.File" progid. If you'd like to omit the file extension (i.e. run demo), add .PY to the PATHEXT environment variable. The shell will pass the fully-qualified path of the script ... More on stackoverflow.com
🌐 stackoverflow.com
December 19, 2018
Run Python Script From Script Directory/Current Directory - Stack Overflow
I'm writing a Python script to automate installation of UWP-Apps. The script uses Dependencies inside the script directory; my older scripts use this code: ... The above code doesn't work on my current script but works fine on older scripts. It shows: OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '' Researching this topic, I only found talk about running the script from outer/different ... More on stackoverflow.com
🌐 stackoverflow.com
run python scripts from anywhere in the filesystem in windows - Stack Overflow
For example if I am currently in some folder I want to run something like ... And get the required result in the current folder. ... If it's not possible is there a way to create one module with .py scripts and .template files and then take it with me anywhere? If I put it into PYTHON_HOME aka D:\Python27 it shows error · python: can't open file 'util1.py': [Errno 2] No such file or directory · If I put it into C:\Windows ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Kayako
correlated.kayako.com › article › 40-running-python-scripts-from-anywhere-under-windows
Running Python Scripts from anywhere under Windows - Correlated Solutions, Inc.
December 21, 2020 - Create directory to put all your python scripts in. E.g. "C:\Users\Your Name\My Scripts" ... Add the path to this directory in Windows "PATH" system variable: Open Explorer Right-click on "My Computer" Click "Properties" Click "Advanced system settings" Select tab "Advanced" Click "Environment ...
🌐
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:
🌐
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
🌐
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 ...
🌐
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 - ... I wanted to use Comments , though typing habit made those accidental shebangs. I have made the Corrections , @GeraldSchneider , thank you. ... You can just cd into the directory before running the script.
Find elsewhere
🌐
Python Forum
python-forum.io › thread-3688.html
How do I run python scripts from any directory using Windows command prompt?
The default directory I'm usually in when I start up Windows command prompt is C:\Users\Moon\, which I'm content with. I have some folders located in the directory C:\Users\Moon\AppData\Local\Programs\Python\Python36\ such as 'data scripts', deep l...
🌐
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 ...
🌐
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"
🌐
DEV Community
dev.to › joshthecodingaddict › use-custom-python-scripts-from-any-directory-in-windows-2376
Use custom python scripts from any directory in windows - DEV Community
May 19, 2022 - In this one I am going to be showing you how you can run your python scripts from the terminal in directory on windows.
🌐
Stack Overflow
stackoverflow.com › questions › 47419251 › run-python-scripts-from-any-folder-under-powershell-in-windows
Run python scripts from any folder under powershell in windows - Stack Overflow
Update: powershell solution is python $env:scripts\test.py which unfortunately is a quite long command 2017-11-21T18:56:03.633Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... How would contemporary physicalist philosophers respond to Abhinavagupta’s critique of the materialist accounts of causality? ... Is there any difference between a mage losing their Avatar through Gilgul and losing it because they are Embraced by a vampire?
🌐
Iditect
iditect.com › faq › python › how-to-execute-a-python-script-in-a-different-directory.html
How to execute a python script in a different directory?
Description: This query seeks ways to execute a Python script located in another directory from the command line. cd /path/to/your/script/directory python script.py · By changing the directory to where the script is located using cd command and then running the script with python, you can ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › visualstudio › python › quickstart-05-python-visual-studio-open-folder
Quickstart: Open and run Python code in a folder - Visual Studio (Windows) | Microsoft Learn
December 11, 2025 - Run the code by selecting Debug > Start without Debugging or use the keyboard shortcut Ctrl+F5. You can also select the solid play arrow next to the Startup Item name on the Visual Studio toolbar.
🌐
Python
docs.python.org › 3 › faq › windows.html
Python on Windows FAQ — Python 3.14.3 documentation
Let’s say your Python script ... and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as ...