No history yet

Scripting and Environment Control

From Code Snippets to Scripts

So far, you've likely been writing Python in an interactive shell or a notebook. That's great for experimenting, but real work happens in script files. A Python script is just a plain text file containing Python code, saved with a .py extension.

Instead of running code line by line, you tell the Python interpreter to execute the entire file from your terminal or command line. This is how you build reusable tools and applications.

# A simple script named `greeter.py`

name = "Alice"
print(f"Hello, {name}!")

To run this script, you open your terminal, navigate to the directory where you saved greeter.py, and run the following command:

python3 greeter.py

The terminal will then display the output:

Hello, Alice!

This is the fundamental workflow for running any Python program. You write the code in a .py file and use the python3 command to execute it.

Managing Project Dependencies

As your projects grow, you'll start using external libraries, code written by other developers that you can install and use. For example, you might use the requests library for making web requests or pandas for data analysis. The problem is, different projects might need different versions of the same library.

Project A might need version 1.2 of a library, while Project B needs the newer 2.0. If you install these system-wide, you’ll have a conflict. One of your projects will break. This is where virtual environments come in.

A virtual environment is an isolated directory that contains a specific version of Python and any libraries needed for a particular project.

Python's built-in tool for this is called venv . To create a virtual environment for a new project, navigate to your project folder in the terminal and run:

python3 -m venv my_project_env

This command creates a new folder (in this case, my_project_env) containing a copy of the Python interpreter and its standard libraries. The -m flag tells Python to run a module from its standard library as a script.

Next, you need to activate the environment. The command differs slightly depending on your operating system.

On macOS and Linux:

source my_project_env/bin/activate

On Windows:

my_project_env\Scripts\activate

Once activated, your terminal prompt will change to show the environment's name, like (my_project_env). Now, any libraries you install with pip will be placed inside my_project_env, leaving your global Python installation untouched. When you're done working, simply type deactivate to exit.

Making Scripts Dynamic

Our greeter.py script is static. It always greets Alice. To make it more useful, we can have it accept a name as an argument directly from the command line. Python's built-in sys module makes this easy.

The sys.argv attribute is a list containing the command-line arguments passed to the script. The first item, sys.argv[0], is always the name of the script itself. The arguments you provide follow after that.

# A dynamic script named `dynamic_greeter.py`
import sys

# sys.argv[0] is the script name, so we get the first argument from index 1
if len(sys.argv) > 1:
    name = sys.argv[1]
else:
    name = "World"

print(f"Hello, {name}!")

Now you can run it with different names:

python3 dynamic_greeter.py Bob
# Output: Hello, Bob!

python3 dynamic_greeter.py "Sarah Jones"
# Output: Hello, Sarah Jones!

python3 dynamic_greeter.py
# Output: Hello, World!

Notice that we put "Sarah Jones" in quotes. Without them, the shell would treat Sarah and Jones as two separate arguments.

Executable Scripts

On Unix-like systems (macOS, Linux), you can make a script directly executable, so you don't have to type python3 every time. This involves two steps: adding a shebang line and changing file permissions.

The shebang is a special line at the very top of your script that tells the operating system which interpreter to use to run the file. For a Python script, it looks like this:

#!/usr/bin/env python3

import sys

# ... rest of your script code

Using /usr/bin/env python3 is generally preferred over a hard-coded path like #!/usr/bin/python3 because it finds the python3 interpreter in the user's current environment path. This makes your script more portable.

Next, you need to give the file execute permissions. In your terminal, you use the chmod (change mode) command:

chmod +x dynamic_greeter.py

The +x adds the executable permission. Now, you can run your script directly:

./dynamic_greeter.py Devika

The ./ tells the shell to look for the file in the current directory. This two-step process turns your Python file into a command-line tool.

Quiz Questions 1/6

What is the standard command to execute a Python script named app.py from the command line?

Quiz Questions 2/6

What is the primary purpose of a virtual environment in Python?

With these skills, you can now create, manage, and execute standalone Python scripts in a clean, organized, and powerful way.