In this article, we will learn more about how to run Python scripts. You should be familiar with Python syntax. If you don’t know many about Python check our guide here. You need to have Python installed in your machine. To know how to install python on your system check our installation guide.
We will discuss:
- How to update python
- Run Python scripts
- Run python in the terminal
- Comments in Python
How to update Python:
We discussed in a previous article how to install Python. It’s always recommended to have the latest version of Python. This will let you avoid a lot of issues. In each update, Python adds new fixes and enhancements. Updating python will let take the advantage or new fixes. To update the Python version you need to first check your operating system.
Updating Python in widows
- Visit Python official page
- Download the latest Python version
- It’ll download a .exe file.
- open it and proceed like the installation.
- It’ll update the Python version installed on your machine.
Updating Python in Ubuntu
Updating Python in Ubuntu is a simple easy command.
- Update apt package manager
sudo apt update && sudo apt-get upgrade
- Install the latest version of Python.
sudo apt install python3.8.2
This will update the Python version to 3.8.2.
Updating Python in Mac
- Visit Python official page
- Download the latest Python version
- It’ll download a file.
- open it and proceed like the installation.
- It’ll update the Python version installed on your macOS.
Updating Python globally might not be a good idea. We learned in a previous article that it’s better to use a virtual environment in Python projects. In most cases, you will need to only update the python version in your virtual environment.
python -m venv --upgrade <env_directory>
This will only update the python version inside your virtual environment.
After making sure that you have Python installed. And it is the latest version. Now you can run Python scripts.
Run Python scripts
Usually, a python script will look like this
import os def main(): path = input("Enter folder path: ") folder = os.chdir(path) for count, filename in enumerate(os.listdir()): dst ="NewYork" + str(count) + ".jpeg" os.rename(src, dst) if __name__ == '__main__': main()
It has the main function that will have the script logic. This code is the main functionality of the script. It actually defines what you need to do. The other part which invokes the main function is
if __name__ == '__main__': main()
First, define the function contains your code. Python will enter the if __name__ == ‘__main__’: condition and invoke your main() function. We need to know what happens behind the scene.
- The interpreter reads the python source file. interpreter sets the __name__ special variable and assigns it to the “__main__” string.
- This will tell the interpreter to run the function called inside it. In our case, it’s the main() function.
Scripting is one of the biggest features of Python. To achieve this Python needs to make its code executable in different places.
Run python scripts in the terminal
You can run Python code from the terminal. You can write python <file_name.py>. This will tell the Python interpreter to run the code in this file.
python my_script.py
This will invoke the main function inside the script and execute the logic. A feature like this is useful if you need to automate some of your work from the terminal. Let’s say that you’re a DevOps and every time you power the machine you need to log the time and the user. You can write Python code that will check the time and the username of logged in user. It’ll update the log file with these data each time someone logs in and run the machine. In such a case, you can’t open the file manually. You will add to system bootup commands the command that will run this script.
You can open a Python shell in the terminal. It’ll help you execute your code directly. With this, you don’t need every time you want to test new changes to run the script again. You can write your script and test it in the same place. It’ll help you develop the script faster.
Comments in Python scripts
Python is shining for its readability. Commenting helps you explain more logic that could be difficult to understand from code. Adding comments helps your colleagues to understand the complex logic. Comments are very helpful in teams. It helps team members to know the reason for each part of the code. This makes it easy to work and make the development process faster. Adding comments in python is simply by adding hash.
# the below code will print items from 1 to 5 for x in range(1, 5): print(x)
Notice that the code after the # symbol will not be run. Python sees this code as a comment. It’ll not execute it
conclusion
You can run a Python script from the system GUI or from the terminal. It depends on how you want to use it. The comments in Python used to add human-readable text in the code. Python comments start with a hash symbol. Python interpreter skips the hashed text. Updating python is very important. It makes you up to date with new fixes. You can update Python globally across your entire system. The most efficient is to update the Python version in your virtual environment. It helps in big projects to keep things safe and not break it.