Python Installation
Setting up the Python development environment involves installing Python, configuring an IDE, running Python scripts, and managing Python packages.
Installing Python
To start programming in Python, you first need to install Python on your system.
-
Download Python: Visit the official Python website and download the installer for your operating system (Windows, macOS, or Linux).
-
Installation Instructions:
-
Windows:
- Run the installer and ensure you check the box “Add Python to PATH.”
- Follow the prompts to complete the installation.
-
macOS:
- Download the
.pkginstaller from the Python website. - Run the installer and follow the instructions.
- Download the
-
Linux:
- Most Linux distributions come with Python pre-installed. If not, use the package manager to install it. For example:
sudo apt-get install python3
- Most Linux distributions come with Python pre-installed. If not, use the package manager to install it. For example:
-
Setting up an IDE (PyCharm, VSCode, etc.)
An Integrated Development Environment (IDE) can help you write and manage your Python code more efficiently.
-
PyCharm:
- Overview: A popular IDE specifically for Python development.
- Installation:
- Download PyCharm from the official website.
- Run the installer and follow the prompts.
-
Visual Studio Code (VSCode):
- Overview: A lightweight, versatile code editor with support for Python through extensions.
- Installation:
- Download VSCode from the official website.
- Run the installer and follow the prompts.
- Setting Up Python Extension:
- Open VSCode.
- Go to Extensions (Ctrl+Shift+X) and search for “Python.”
- Install the Python extension by Microsoft.
Running Python Scripts
Once Python and your IDE are set up, you can start running Python scripts.
-
Using the Command Line:
- Open your terminal or command prompt.
- Navigate to the directory containing your Python script.
- Run the script with:
python script.py
-
Using an IDE:
- Open your Python script in your chosen IDE.
- Look for the “Run” button or use the IDE’s built-in terminal to execute the script.
Python Package Management (pip)
pip is the package installer for Python, allowing you to install and manage additional libraries and packages.
- Installing Packages:
pip install package_name - Upgrading Packages:
pip install --upgrade package_name
- Listing Installed Packages:
pip list
- Uninstalling Packages:
pip uninstall package_name
With Python installed, an IDE configured, and package management set up, you’re ready to start developing Python applications and leveraging the extensive Python ecosystem.
pyenv for Multiple Python Versions
# macOS
brew install pyenv
pyenv install 3.12.0
pyenv global 3.12.0
python --version # 3.12.0
Create a .python-version file in a project to pin the version locally.
uv — Fast Modern Tooling
curl -LsSf https://astral.sh/uv/install.sh | sh
uv venv
uv pip install requests
uv pip compile requirements.in -o requirements.txt
uv is significantly faster than pip for installs and resolves dependencies quickly.
Verifying Your Setup
python --version
pip --version
python -c "import sys; print(sys.executable)"
Confirm the executable points to your venv when one is active.
Creating a requirements.txt
pip freeze > requirements.txt
pip install -r requirements.txt
For applications, pin exact versions. For libraries, specify ranges in pyproject.toml.
Troubleshooting
| Issue | Solution |
|---|---|
python not found |
Use python3; add to PATH on Windows install |
pip installs to system |
Activate venv first |
| SSL errors on pip | Upgrade pip: pip install --upgrade pip |
| Wrong version running | Check which python and pyenv/venv activation |