Featured image of post Installing Python and Setting Up a Virtual Environment

Installing Python and Setting Up a Virtual Environment

A comprehensive guide on installing Python and setting up a virtual environment without using pip.

Introduction

Python is a powerful programming language widely used for development, scripting, and automation. This guide will walk you through installing Python on Arch Linux or any Arch-based distribution and setting up a virtual environment without relying on pip. If you encounter pip errors, we will also explain the reasons and solutions.

Installing Python

On Arch Linux, you can install Python easily using the package manager:

1
sudo pacman -S python

This will install the latest version of Python available in the official Arch repositories.

Why Avoid pip for Installing Python Packages?

Many users rely on pip for installing Python packages, but using pip system-wide can lead to conflicts with system packages managed by pacman. This can cause issues when upgrading your system or installing software that depends on specific Python versions.

Setting Up a Virtual Environment

A virtual environment allows you to create an isolated Python environment, preventing conflicts with system packages. To set up a virtual environment:

  1. Navigate to your project directory:

    1
    
    mkdir my_project && cd my_project
    
  2. Create a virtual environment using venv:

    1
    
    python -m venv venv
    
  3. Activate the virtual environment:

    • For Bash/Zsh:
      1
      
      source venv/bin/activate
      
    • For Fish shell:
      1
      
      source venv/bin/activate.fish
      
  4. Your terminal prompt should now indicate that you are inside a virtual environment.

Handling pip Errors

If you encounter errors when using pip, it may be due to missing dependencies or conflicts with system packages. Some common errors include:

  • Command Not Found: Ensure python-ensurepip is installed:

    1
    
    sudo pacman -S python-ensurepip
    
  • SSL Errors: Some systems may require certificates to be installed:

    1
    
    sudo pacman -S ca-certificates-mozilla
    
  • Permission Errors: Avoid using pip with sudo. Instead, always install packages inside a virtual environment.

    1
    
    pip install package_name
    

Conclusion

Using a virtual environment is the best practice for Python development, as it isolates project dependencies and prevents system conflicts. By following this guide, you now have a clean and efficient Python setup that avoids modifying system-wide packages.