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
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.

Discussions

How to execute .py file outside of it's residing directory?
You can use the command python directory\to\your\py\file\yourpyfile.py. Encapsulate the directory/file part in quotes if you have spaces. More on reddit.com
🌐 r/learnpython
8
1
February 10, 2022
Running a Python Script Inside Another Directory - Stack Overflow
You mean use python ./test/script.py? Cause as I said I am trying to run the script from outside its directory. More on stackoverflow.com
🌐 stackoverflow.com
May 25, 2017
linux - Running a (python) script in another directory - Stack Overflow
I have a python script, which generates files. All i want is to force it to write the files in a specific folder. Right now i have to do 3 steps: cd foo python ../awesome_script.py cd .. Is there ... More on stackoverflow.com
🌐 stackoverflow.com
August 2, 2016
Running a python script from any directory?
Place the script inside a directory that is available in your $PATH such as /usr/bin and you should be able to call it from anywhere. More on reddit.com
🌐 r/learnprogramming
2
2
October 17, 2021
🌐
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.
🌐
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"
🌐
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 :)

🌐
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 ...
Find elsewhere
🌐
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 ...
Top answer
1 of 11
28

sh /path/to/script will spawn a new shell and run she script independent of your current shell. The source (.) command will call all the commands in the script in the current shell. If the script happens to call exit for example, then you'll lose the current shell. Because of this it is usually safer to call scripts in a separate shell with sh or to execute them as binaries using either the full (starting with /) or relative path (./). If called as binaries, they will be executed with the specified interpreter (#!/bin/bash, for example).

As for knowing whether or not a script will find the files it needs, there is no good answer, other than looking at the script to see what it does. As an option, you could always go to the script's folder in a sub-process without leaving your current folder:

(cd /wherever/ ; sh script.sh)
2 of 11
8

You can definitely do that (with the adjustments the others mentioned like sudo sh /pathto/script.sh or ./script.sh). However, I do one of a few things to run them system wide to not worry about dirs and save me useless extra typing.

1) Symlink to /usr/bin

ln -s /home/username/Scripts/name.sh /usr/bin/name

(be sure there is no overlapping name there, because you would obviously override it.) This also lets me keep them in my development folders so I can adjust as necessary.

2) Add the Scripts dir to your path (using .bash_profile - or whatever.profile you have on your shell)

PATH=/path/to/scripts/:$PATH

3) Create Alias's in the .bash_profile in ~/.bash_profile add something like:

alias l="ls -l"

As you can tell, the syntax is just alias, digits you want to act as a command, the command. So typing "l" anywhere in the terminal would result in ls -l If you want sudo, just alias sl="sudo ls -l" to note to yourself l vs sl (as a useless example).

Either way, you can just type sudo nameofscript and be on your way. No need to mess with ./ or . or sh, etc. Just mark them as executable first :D

🌐
GeeksforGeeks
geeksforgeeks.org › run-python-script-from-anywhere-in-linux
Run python script from anywhere in linux - GeeksforGeeks
November 3, 2019 - This can be done by typing several commands in the terminal. ... At first, open the terminal and go to the home directory. To go the home directory type the following command. ... Create a folder and a python script inside that folder.
Top answer
1 of 2
6

You can call your script using only the whole path, without the dot .:

/path/to/script

sudo also works fine:

sudo /path/to/script

Will they usually find all the stuff they need to run properly?

Do you mean like, "will my script find the files which are in the same folder?" That depends of your code. For example, if you have the script /tmp/test.sh:

#!/bin/bash
ls

If you invoke it from your home folder, it will run ls in your home:

test@ubuntu:~:/tmp/test.sh
Desktop     Dropbox         Imagens  NetBeansProjects     Público     
Documentos  Modelos    R          Vídeos

In this situation, dirname is your ally:

#!/bin/bash
current=$(dirname $0)
ls $current

Running it from your home folder, it gives:

test@ubuntu:~:/tmp/test.sh
acroread_1000_1000     hiRF7yLSOD              pulse-2L9K88eMlGn7   
unity_support_test.0    clementine-art-jt5332.jpg

which is the content of my /tmp/ folder.

2 of 2
3

Common *nix practice for user scripts is to put them in your bin directory. That way, the script will be searched for by default when you type the name from the command line.

Your bin (binary) directory should be in your home folder so the full path would be /home/yourusername/bin. (Obviously change yourusername to your login name.)

The bin directory should have the following permissions if you run ls -lh from the terminal: drwxr-xr-x

By default when you make a directory with the terminal you have the following permissions set: drwxrwxr-x

The only thing you need to change is the group permission, but for I will walk you through each step from the terminal (assuming you don't already have a bin):

  • Open a terminal ([Ctrl] + [Alt] + t)

  • Create the binary directory: mkdir bin

  • Now either use the GUI or CLI to copy your scripts to /bin.

  • Now we have to change some permissions so that everything is set up correctly:

  • chmod -R 755 bin

  • This sets the owner (you) to read, write and execute, groups and others to read and execute the scripts. The -R means recursive or set the permissions for all the files in the directory as well.

For more help on file permissions see this document.

Now, you can just type myscript to execute your scripts - you do not have to be in the directory. You don't need the . / or anything!

For sudo it's a little different as you will have to define the full path to the script file.

🌐
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...
🌐
Real Python
realpython.com › run-python-scripts
How to Run Your Python Scripts and Code – Real Python
February 25, 2026 - Pressing the Ctrl+Z and Enter key combination on Windows, or the Ctrl+D combination on Unix systems, such as Linux and macOS · Go ahead and give the Python REPL a try. You’ll see that it’s a great development tool that you must keep in ...