Run python script from anywhere in linux Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report In Linux, there is a way to execute python files from anywhere. This can be done by typing several commands in the terminal. Prerequisite: Basic Shell Commands in Linux Basics of python Steps: At first, open the terminal and go to the home directory. To go the home directory type the following command. cd ~ Create a folder and a python script inside that folder. Let the name of the folder be "check" and name of the script be "file1". Type the following command to perform the above operations. mkdir check cd check touch file1.py Then type this script in the file1.py Python3 1== import os i = 1 # Write the path where to create the directories path ="/home / dev / test/" try: while i<5: os.mkdir(path+"file"+str(i)) i+= 1 except OSError: print("File creation failed !!") This script will create 4 directories in the specified path. Then to find where the python is installed in the system type the below commands. For python 2.7 which python For python 3 which python3 Copy the output and add this in the starting of the script e.g if output is /usr/bin/python3 then write the below command in the starting of the script. #!/usr/bin/python3 So the python script would look like Python3 1== #! usr / bin / python3 import os i = 1 # Write the path where to create the directories path ="/home / dev / test/" try: while i<5: os.mkdir(path+"file"+str(i)) i+= 1 except OSError: print("File creation failed !!") Type the following command to get the path of the working directory, starting from the root. pwd Let it be /home/usr/check Add this path in $PATH variable. For this type in terminal sudo nano .bashrc Before this command make sure that you are in the home directory. Then add this line in the file export PATH=$PATH:/home/dev/check This will get added to the path variable. Then type source ~/.bashrc Close the terminal and open again. Now we can run the python file directly from anywhere in the terminal by typing the file name file1.py This will create the four directories in the check folder. Now any python file placed in the check directory we can be executed from anywhere in the terminal by typing the file name. Create Quiz Comment D dev49199 Follow 2 Improve D dev49199 Follow 2 Improve Article Tags : Python Python-Miscellaneous python-utility Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like