Python is a versatile, high-level programming language known for its readability and simplicity. It's widely used in various domains such as web development, data analysis, artificial intelligence, scientific computing, and more. This tutorial will guide you through the basics of setting up Python on your system and using virtualenv to manage project-specific dependencies.
Before we dive into the setup, ensure that you have the following:
Python can be installed from the official website or through package managers. Here’s how you can install it on different operating systems.
Download the Installer:
Run the Installer:
Verify Installation:
python --version
Using Homebrew (Recommended):
brew install python
Verify Installation:
python3 --version
Using Package Manager:
sudo apt update
sudo apt install python3
sudo dnf install python3
Verify Installation:
python3 --version
A virtual environment is an isolated Python environment that allows you to manage dependencies for different projects separately. This prevents conflicts between project-specific packages.
virtualenvvirtualenv can be installed using pip, Python's package installer.
pip install virtualenv
Navigate to Your Project Directory:
cd path/to/your/project
Create a Virtual Environment:
env:
virtualenv env
python3 -m venv env
On Windows:
.\env\Scripts\activate
On macOS/Linux:
source env/bin/activate
Once activated, your command prompt or terminal will show the name of the virtual environment (e.g., (env)), indicating that it's active.
To deactivate the virtual environment and return to the global Python environment, simply run:
deactivate
pippip is the package installer for Python. It allows you to install and manage additional libraries and dependencies.
With your virtual environment activated, you can install packages using pip. For example, to install the popular web framework Flask:
pip install flask
To see all installed packages in your current virtual environment:
pip list
To create a requirements.txt file that lists all installed packages and their versions, use:
pip freeze > requirements.txt
This file can be used to replicate the environment on another machine or for future reference.
requirements.txt Updated: Regularly update this file to reflect any changes in your project's dependencies.Congratulations! You have successfully set up Python on your system and learned how to use virtualenv to manage project-specific dependencies. This setup is essential for developing robust and maintainable Python applications. In the next sections of this course, we will explore more advanced topics in Python programming.