Ubuntu下virtualenvwrapper安装后路径不一致问题求助
Hey there, let's work through this issue step by step—you don't need to be a Unix pro to fix it!
First, let's unpack what's happening: You've got virtualenv installed at /usr/local/bin/virtualenv, and pip says virtualenvwrapper and its dependencies are in /usr/local/lib/python2.7/dist-packages, but you can't actually use virtualenvwrapper. The most likely culprit here is missing or misconfigured environment variables that virtualenvwrapper needs to find its components.
Here's how to fix it:
Step 1: Locate the virtualenvwrapper.sh script
First, let's confirm where the main virtualenvwrapper script lives. Run this command in your terminal:
which virtualenvwrapper.sh
If it returns a path like /usr/local/bin/virtualenvwrapper.sh, great—we'll use that in the next step. If it says "command not found", try looking for it in the Python 2.7 dist-packages directory:
find /usr/local/lib/python2.7/dist-packages -name virtualenvwrapper.sh
Note down the full path if you find it here.
Step 2: Add environment variables to your shell config
virtualenvwrapper relies on three key environment variables to work correctly. You need to add these to your shell's configuration file (like ~/.bashrc for Bash, or ~/.zshrc for Zsh):
- Point to your Python 2.7 executable: Since your
virtualenvis tied to Python 2.7, make surevirtualenvwrapperuses the same version. Runwhich python2.7to get the path, then add:export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7 # Replace with your actual path - Set a directory for your virtual environments: This is where all your virtual envs will live. Pick a location (like your home folder) and add:
export WORKON_HOME=~/.virtualenvs - Source the
virtualenvwrapper.shscript: Use the path you found in Step 1 here:source /usr/local/bin/virtualenvwrapper.sh # Replace with your actual script path
Save the config file, then reload it to apply changes:
source ~/.bashrc # Or ~/.zshrc if you use Zsh
Step 3: Test if it works
Run the workon command in your terminal. If it returns without errors (and shows an empty list if you haven't created any virtual envs yet), you're all set! If you still run into issues:
- Double-check that all paths in your environment variables match the actual locations on your system.
- Make sure
/usr/local/binis in yourPATH(runecho $PATHto verify)—this ensures your shell can findvirtualenvandvirtualenvwrapper.sh.
A quick side note: Python 2.7 has been end-of-life for years, so if you have the option, consider migrating to Python 3 for better security and support. But if you need to stick with Python 2.7, the steps above should resolve your path mismatch issue.
内容的提问来源于stack exchange,提问作者eSurfsnake

