Content · Programming · Python

Python Run a Script from a Different Working Directory

You can run a Python script from any working directory if you give Python the correct path to the file. Learn the essential ideas and try a practical example.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

You can run a Python script from any working directory if you give Python the correct path to the file.

Understanding the Concept


A relative path is interpreted from the terminal’s current directory; an absolute path identifies a location independently of the current directory. This distinction explains why python hello.py fails when hello.py is somewhere else. Use quotes around paths containing spaces. Running the same script from different directories may also change where it looks for files opened with relative paths inside the program.

When and Why You Would Use This


Try running a script located in a project subfolder from its parent directory. Do not change system directories or permissions. You can use a relative path such as scripts/hello.py when your current directory is the project root.

Practical Example


python scripts/hello.py
python "/path/to/project/scripts/hello.py"

Understanding the Example


These are example shell commands. The second path is a placeholder that must be replaced with a real absolute path on your operating system. A correct path starts the script regardless of the terminal’s directory.

Try It Yourself


Move into the parent of your project folder and execute a script using a relative path.

Common Beginner Mistakes


  • Assuming Python searches your whole drive for a filename.
  • Forgetting quotes around a path containing spaces.
  • Confusing the script path with the working directory used for relative data files.

Key Takeaways


  • Give Python a correct relative or absolute script path.
  • The shell’s current directory affects relative paths.
  • Data files opened by a script may still depend on its working directory.
Share this resourceSend this page to someone who may find it useful.