Run python script from anywhere in linux Last Updated : 03 Nov, 2019 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us Next Article Run python script from anywhere in linux D dev49199 Follow Improve Article Tags : Python Python-Miscellaneous python-utility Practice Tags : python Similar Reads How to check any script is running in linux using Python? Python is a strong and exponentially growing programming language in the present day. There is a multiple-way to check which script is running in the background of a Linux environment. One of them is using the subprocess module in python. Subprocess is used to run new programs through Python code by 2 min read How to run bash script in Python? If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python 2 min read How to run Python script directly in Kivy file? Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on the android device even we can use it on Raspberry pi also.  It is an open-source Python library for the rapid development of 3 min read How to Run a Python Script Python scripts are Python code files saved with a .py extension. You can run these files on any device if it has Python installed on it. They are very versatile programs and can perform a variety of tasks like data analysis, web development, etc. You might get these Python scripts if you are a begin 6 min read Run function from the command line In Python Python is a flexible programming language with a wide range of uses that one of Pythonâs most useful ones is its ability to execute functions from the command line. Especially this feature is very useful when it comes to automation and scripting etc. In this article, Iâll explain how to execute a Py 4 min read Python | Accepting Script Input A lot of people use Python as a replacement for shell scripts, using it to automate common system tasks, such as manipulating files, configuring systems, and so forth. This article aims to describe accepting Script Input via Redirection, Pipes, or Input Files. Problem - To have a script to be able t 2 min read Convert Python Script to .exe File We create lots of Python programs per day and want to share them with the world. It is not that you share that Python program with everyone, and they will run this script in some IDLE shell. But you want everyone to run your executable python script without the installation of Python. So for this wo 4 min read Open and Run Python Files in the Terminal The Linux terminal offers a powerful environment for working with Python files, providing developers with efficient ways to open, edit, and run Python scripts directly from the command line. Open and Run Python Files in the Linux TerminalIn this article, we'll explore various techniques and commands 2 min read Scheduling Python Scripts on Linux Sometimes we need to do a task every day, and we can do these repetitive tasks every day by ourselves, or we can use the art of programming to automate these repetitive tasks by scheduling the task. And today in this article we are going to learn how to schedule a python script on Linux to do the re 3 min read How to run multiple Python file in a folder one after another? In this article, we will discuss how to run multiple python files in a folder one after another. There can be many ways to this task, here, we will discuss a few of them. For doing this program, we have to create some python files in one folder and give some names to that folder. The content inside 3 min read Like