In Linux, there is a way to execute python files from anywhere. This can be done by typing several commands in the terminal.
Prerequisite:
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
importosi=1# Write the path where to create the directoriespath="/home / dev / test/"try:whilei<5:os.mkdir(path+"file"+str(i))i+=1exceptOSError:print("File creation failed !!")chevron_rightfilter_noneThis 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.7which 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/python3then write the below command in the starting of the script.#!/usr/bin/python3
So the python script would look like
#! usr / bin / python3importosi=1# Write the path where to create the directoriespath="/home / dev / test/"try:whilei<5:os.mkdir(path+"file"+str(i))i+=1exceptOSError:print("File creation failed !!")chevron_rightfilter_none - 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
$PATHvariable. For this type in terminalsudo nano .bashrc
Before this command make sure that you are in the home directory.
Then add this line in the fileexport 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.
Recommended Posts:
- Run Python script from Node.js using child process spawn() method
- How to Run a Python Script
- Run Python Script using PythonShell from Node.js
- How to control PC from anywhere using Python?
- PyQt5 QCalendarWidget - How to drag and drop it anywhere in the window using mouse
- How to Compile and Run C/C++/Java Programs in Linux
- Add a User in Linux using Python Script
- Deleting a User in Linux using Python Script
- Python script to change MAC address of Linux machine
- Run Length Encoding in Python
- How to run Python code on Google Colaboratory
- Python | Making program run faster
- Python | Longest Run of given Character in String
- Python IMDbPY – Getting run time of the series
- Python IMDbPY – Getting Series Run times as XML
- Python | Add Logging to a Python Script
- Guide to deploy containers on Google Cloud Run
- How to create an instance of a Metaclass that run on both Python2 and Python3?
- Python script to open a Google Map location on clipboard
- Autorun a Python script on windows startup
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

