Quick and dirty:

In your start up script instead of just executing the python script, use cd first.

#!/bin/sh

cd /home/username/projectname &&
python ./scriptname.py
Answer from bahamat on Stack Exchange
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
bash - How to execute a python script in a different directory? - Stack Overflow
Solved see my answer below for anyone who might find this helpful. I have two scripts a.py and b.py. In my current directory "C:\Users\MyName\Desktop\MAIN", I run > python a.py. The first script,... 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
python: Change the scripts working directory to the script's own directory - Stack Overflow
Works great. Tried this as it was 'simpler', but it gives /home/rbain/anaconda3/envs/ephys/lib/python39.zip because I'm using an anaconda and running the script with spyder. Not the intended output in the development setup / scenario. 2024-01-23T06:45:58.51Z+00:00 ... Don't do this. Your scripts and your data should not be mashed into one big directory... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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:
🌐
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.
🌐
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 :)

🌐
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 ...
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
Summary You can run Python scripts from any folder, but make your code robust to cwd differences by using absolute paths based on file or package resources, running modules with -m, or installing your package.
🌐
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
🌐
O'Reilly
oreilly.com › library › view › programming-python-second › 0596000855 › ch02s07.html
Current Working Directory - Programming Python, Second Edition [Book]
March 1, 2001 - Keep in mind, though, that filenames without full pathnames map to the CWD, and have nothing to do with your PYTHONPATH setting. Technically, the CWD is always where a script is launched from, not the directory containing the script file. Conversely, imports always first search the directory containing the script, not the CWD (unless the script happens to also be located in the CWD). Since this distinction is subtle and tends to trip up beginners, let’s explore it in more detail. When you run a Python script by typing a shell command line like python dir1\dir2\file.py, the CWD is the directory you were in when you typed this command, not dir1\dir2.
Author   Mark Lutz
Published   2001
Pages   1296
🌐
Stack Overflow
stackoverflow.com › questions › 62972727 › python-run-script-from-different-directories
python: run script from different directories - Stack Overflow
Calling the script in the sripts directory with python3 main.py successfully returns home/laptop/PycharmProjects/saveclipboard. How to get the scripts absolute path, to open files with open(getabsolutepathsomehow() + "foo.bar", "w") in its directory, when the script does get called in another directory? ... The latter should work if you use __file__ rather than "__file__".
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.

🌐
Linuxize
linuxize.com › home › python › how to get and change the current working directory in python
How to Get and Change the Current Working Directory in Python | Linuxize
February 27, 2026 - They are often different. Use os.getcwd() for the working directory and os.path.dirname(os.path.realpath(__file__)) for the script’s location. Does os.chdir() affect the parent process?
🌐
DNMTechs
dnmtechs.com › executing-python-scripts-in-different-directories-using-command-line
Executing Python Scripts in Different Directories Using Command Line – DNMTechs – Sharing and Storing Technology Knowledge
... function to add the directory ... script as usual. If you are using a Unix-based system, you can also change the current working directory using the “cd” command before executing the Python script....
🌐
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?
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 execute it from the desired directory.