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 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 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 a Python Script using Docker? Docker helps you to run your Python application very smoothly in different environments without worrying about underlying platforms. Once you build an image using dockerfile you can run that image wherever you want to run. Docker image will help you to package all the dependencies required for the a 8 min read Command Line Scripts | Python Packaging How do we execute any script in Python? $ python do_something.py $ python do_something_with_args.py gfg vibhu Probably that's how you do it. If your answer was that you just click a button on your IDE to execute your Python code, just assume you were asked specifically how you do it on command line. 4 min read Schedule a Python Script to Run Daily In this article, we are going to see how to schedule a Python Script to run daily. Scheduling a Python Script to run daily basically means that your Python Script should be executed automatically daily at a time you specify.Step-by-Step ImplementationCreate a Python file, for example: gmail_automati 3 min read Like