Skip to main content

How to Resolve Python Pip "EnvironmentError: [Errno 13] Permission denied" & "HTTPSConnectionPool" Errors

When installing Python packages using pip, encountering an EnvironmentError signals a problem related to the operating system or the network environment where pip is running. Two common variants are [Errno 13] Permission denied, indicating filesystem access issues, and errors involving HTTPSConnectionPool, indicating network connection or security problems.

This guide explains the distinct causes of these errors and provides comprehensive solutions for both permission and connection-related pip installation failures.

Understanding the Errors: Permissions vs. Network

While both errors fall under EnvironmentError, their root causes differ significantly:

  • EnvironmentError: [Errno 13] Permission denied: This error occurs when pip tries to write files (the package files) to a directory (typically the Python site-packages directory), but the operating system denies write access for the current user. This is common when trying to install packages globally without administrator/root privileges.
  • EnvironmentError: HTTPSConnectionPool(...): This error (often followed by Max retries exceeded... or ConnectionResetError...) indicates a problem establishing or maintaining a secure HTTPS connection to the package index (like PyPI - pypi.org). Common causes include network proxies, firewalls blocking the connection, SSL certificate verification failures (covered in another guide), or general internet connectivity issues.

Solving EnvironmentError: [Errno 13] Permission denied

  • Cause: Pip lacks permission to write to the target installation directory.

This is the best practice for Python development. Virtual environments create isolated directories for projects where you do have write permissions.

  1. Create: (From your project directory)
    # Replace 'python3' if needed (e.g., 'python', 'py')
    python3 -m venv venv # 'venv' is the standard folder name
  2. Activate:
    # Linux/macOS (bash/zsh)
    source venv/bin/activate

    # Windows CMD
    venv\Scripts\activate.bat

    # Windows PowerShell (may require execution policy adjustment)
    venv\Scripts\Activate.ps1
  3. Install: (Your prompt should now show (venv))
    pip install <package-name> # No --user or sudo needed here

Packages install into the venv/lib/pythonX.Y/site-packages directory, avoiding system-wide permission issues.

Solution 2: Install to User Site-Packages (--user)

This installs the package into a directory within your user profile, bypassing the need for system-wide write access.

pip install <package-name> --user
# Or:
pip3 install <package-name> --user
# Or:
python -m pip install <package-name> --user
note

This is generally acceptable for user-specific tools but can sometimes lead to path conflicts if not managed carefully. Virtual environments provide better isolation. This flag has no effect inside an active virtual environment.

Solution 3: Run as Administrator / sudo (Use with Caution)

Running the installation command with elevated privileges grants system-wide write access.

# Linux/macOS
sudo pip3 install <package-name>

# Windows
# 1. Right-click Command Prompt or PowerShell
# 2. Select "Run as administrator"
# 3. Run the normal pip command: pip install <package-name>
warning

Installing packages globally with sudo is generally discouraged. It can lead to conflicts with system packages, make environments harder to manage, and pose security risks if installing untrusted packages. Prefer virtual environments or the --user flag.

Solution 4: Fix Filesystem Permissions (Advanced / Less Common)

If installing globally is intended and permissions are wrong (less common for standard Python installs), you might need to adjust directory permissions. This is OS-specific and risky if done incorrectly. Example for Windows (Use Cautiously): Find the Python site-packages directory (e.g., C:\Python310\Lib\site-packages), right-click -> Properties -> Security -> Edit -> Select your user/group -> Grant "Full control" or at least "Modify". Example for Linux/macOS (Generally Not Recommended for system Python): sudo chown -R $USER /path/to/python/site-packages (takes ownership) or sudo chmod -R u+w /path/to/python/site-packages (adds write permission for owner). Modifying system directory permissions is risky.

Solution 5: Check Virtual Environment Ownership/Permissions

If you get Errno 13 inside a virtual environment, it might mean the venv folder itself was created with incorrect ownership (e.g., using sudo python -m venv venv).

  • Fix: Change ownership (Linux/macOS): sudo chown -R $USER:$USER venv
  • Or (Simpler): Delete the venv folder (rm -rf venv or rd /s /q venv) and recreate it without sudo.

Solving EnvironmentError: HTTPSConnectionPool Errors

  • Cause: Problems connecting securely to the package index (PyPI), often due to firewalls, proxies, or network instability.

Solution 1: Add Trusted Hosts (--trusted-host) (Use with Caution)**

If you are behind a proxy/firewall that interferes with SSL verification for PyPI, but you trust the connection path, you can tell pip to skip verification for specific hosts. This reduces security.

# Trust the standard PyPI hosts
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org <package-name>

You can add this persistently to pip.conf/pip.ini or using pip config. Use only when necessary and understood.

Solution 2: Configure Proxy Settings (--proxy / Env Vars)

If you need to go through an HTTP/HTTPS proxy:

  • Command Line (--proxy):
    # Format: scheme://[user:password@]proxy.server:port
    pip install --proxy http://your-proxy.com:8080 <package-name>
    pip install --proxy http://user:[email protected]:8080 <package-name>
  • Environment Variables: Pip automatically checks HTTP_PROXY and HTTPS_PROXY. Set these in your shell before running pip.

Solution 3: Check Network Connectivity & Firewalls

  • Ensure you have a stable internet connection.
  • Check if a corporate or personal firewall is blocking access to pypi.org or files.pythonhosted.org on port 443 (HTTPS). You might need to configure firewall exceptions or contact your network administrator.
  • Check VPN connections – try with the VPN on and off if applicable.

Solution 4: Increase Timeout (--default-timeout)

On very slow or unreliable networks, pip might time out before downloading completes. Increase the timeout duration (in seconds):

pip install --default-timeout=100 <package-name> # Increase to 100 seconds

General Solutions (Applicable to Both Errors)

Upgrade pip, setuptools, wheel

Outdated tools can sometimes contribute to both permission handling and connection/dependency issues. Keep them current:

python -m pip install --upgrade pip setuptools wheel

Ensure Correct Python Environment is Active

Double-check that the correct virtual environment (or global environment, if intended) is active in your terminal before running pip install. Installing into the wrong environment is a common source of confusion.

Debugging

  • Verbose Output: Use pip install -v <package-name> (add more vs for more detail) to get extensive logs, which can reveal permission issues or specific connection problems.
  • pip show: After an install attempt (even if it showed errors), run pip show <package-name> to see if the package did get installed partially or fully despite the messages.

Conclusion

pip EnvironmentErrors signal problems outside of the package code itself.

  • [Errno 13] Permission denied: Primarily solved by using virtual environments or the --user flag. Avoid using sudo for pip unless absolutely necessary and understood. Check directory/venv permissions if problems persist.
  • HTTPSConnectionPool: Usually network-related. Check connectivity, firewalls, and proxy settings (--proxy or environment variables). Use --trusted-host cautiously if SSL interception is the known issue. Increase the --default-timeout for slow connections.

Keeping pip, setuptools, and wheel updated and consistently using virtual environments are the best overall strategies for preventing many pip install issues, including these EnvironmentErrors.