Register to get access to free programming courses with interactive exercises

Scripts Python: Setting up the environment

There is only one step from writing code to running it in interpreted languages. We do not need to compile anything into machine code.

The interpreter does all the work. It only needs to input a script — a program in an interpreted language. Within this program, we have simple sequences of commands that the computer needs to execute.

If's is convenient to write scripts in a language, it gets called a scripting language.

Python scripts

Python is an excellent scripting language. We do not have to formalize the sequence of commands in simple scripts. We run the scripts as simply as possible and write the commands one by one into the file:

# file <script.py>
print('Hello, world!')
print('This is a python-script!')

Then we call the interpreter with the received file as input:

python3 script.py

Hello, world!
It is a python-script!

Python has many modules and functions included. Therefore, we often use them to automate tasks we don't want to perform manually. In addition, scripting is a great starting point for those who begin to get acquainted with programming.

Scripts and shebang

In Linux, macOS, BSD, and other UNIX-like operating systems, shells can run scripts in any language, including Python.

The scripts tell the shell which interpreter to call to execute the script. We specify the interpreter by shebang — a string in the very first line of the script file. This name comes from the first two characters of such a string: # is called sharp, and ! — bang.

A typical shebang looks like this:

#!/usr/bin/python3

After the #! characters comes the path to the interpreter. When you run a script with shebang, the shell reads the first line and tries to run the specified interpreter.

If we give execution rights to a script with the specified shebang, we can omit the interpreter on the command line:

cat script.py

#!/usr/bin/python3
print('Hello!')

chmod +x script.py

./script.py

Hello!

Shebang and different versions of Python

Shebang usually is pretty simple when there's only one interpreter in the system. But there can be several versions of Python installed on a system. Moreover, the path to the interpreter in a virtual environment will be different from /usr/bin and other environments.

How do I make the script always run with the correct version of Python? You can use the env program instead of specifying the path to the python command directly.

This program finds and runs programs to environment variables. When the virtual environment is activated, the $PATH variable is modified so that env will run the exact version of the interpreter needed. The interpreter will find the correct version earlier because we added the path to the environment binaries at the beginning of $PATH.

Now let us look at the correct way to specify shebang in Python projects:

#!/usr/bin/env python3
print('Hello!')

We always give the path to env, because this program is almost always located there and won't appear in several versions.


Recommended materials

  1. Shebang (Unix)
  2. The Running Programs lesson from the Command Line Basics course

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.