Ubuntu 18.04无法创建Python虚拟环境,执行python3 -m venv env报错求助
Hey there, let's work through this venv issue you're hitting. That error about ensurepip failing is super common on Ubuntu 18.04, especially if you've messed around with system Python packages or accidentally removed some dependencies. Here's what's likely going on and how to fix it:
Common Causes
- Ubuntu 18.04 splits Python's venv and pip components into separate system packages, which might have gotten uninstalled or corrupted during your earlier troubleshooting.
- Your system's Python
ensurepipmodule could be damaged from accidental file deletion or previous broken installations.
Step-by-Step Fixes
1. Reinstall System Python Dependencies
First, make sure you have all the required system packages for venv and pip. Open a terminal and run:
sudo apt update sudo apt install python3-venv python3-pip --reinstall
This refreshes your package list and reinstalls the official Ubuntu packages for Python venv and pip. A lot of the time, this alone fixes the ensurepip error because it restores any missing or broken system files.
2. Repair the ensurepip Module
If reinstalling the system packages doesn't work, try manually repairing the ensurepip module:
python3 -m ensurepip --upgrade --default-pip
This command forces Python to refresh the ensurepip tool, which handles installing pip inside your virtual environment.
3. Create Venv Without Pip (Fallback)
If the above steps still fail, you can create a virtual environment without pip first, then manually install it:
# Create venv without pip python3 -m venv --without-pip env # Activate the virtual environment source env/bin/activate # Install pip using the official bootstrap script curl https://bootstrap.pypa.io/get-pip.py | python
Once pip is installed, your virtual environment should work normally. Just remember to always activate it with source env/bin/activate before using Python or pip.
Quick Notes
- Never use
sudoto create virtual environments in your home directory—you don't need root permissions here, and it can cause permission issues later. - If you're still having trouble, double-check that you're using
python3(not justpython) since Ubuntu 18.04 defaults to Python 2 for thepythoncommand.
内容的提问来源于stack exchange,提问作者Hunter




