1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Running Python file

Running Python file

Writing Python code in a file and running.

Step 1: Open text editor of your choice, ex: Notepad++, or Vim

Step 2: Write code in to the file

Step 3: Save file with a .py extension ex: first.py

Windows:

Step 4: Run CMD or power shell

Step 5: cd to the directory where the Python file is saved

Step 6: Run the file with

python3 first.py

Contents of first.py

 

#open a new file

#Write below code

#save as first.py

#run on cmd line

print("My first python program file!")

 

 

 

Unix/Linux:

Step 4:In Unix/Linux flavors, first line of your python program, should to point to python interpreter.

It starts with #! (shebang) fallowed by path of the python interpreter.

Usually it will be one of the below, add this line your file if not added.

#!/user/bin/python

OR

#!/user/user/bin/python

OR

#!/user/bin/env python

you can find this by typing

whereis python or which python

on shell, after adding Python interpreter path, save the file.

Step 5: Give permission to file ex: chmod 777 first.py

Step 6:  Run first.py by below command.

python first.py

or

./python.py

Contents of first.py

 

#open a new file

#Write below code

#save it as first.py

#run on shell 

#!/user/bin/python

print("My first python program file!")