Quick Guide: Installing and Setting Up virtualenv
Here’s how to install virtualenv and troubleshoot if it doesn’t work initially.
1. Install virtualenv Using pip
Run the following command to install virtualenv:
pip install virtualenv
You might see a message like:
Defaulting to user installation because normal site-packages is not writeable
This means virtualenv was installed in your user directory.
2. Test if virtualenv Works
Check if the virtualenv command is available:
virtualenv --version
If it works, you’re all set and can start using it.
If it doesn’t work and you see an error like command not found, it’s likely a PATH issue. Follow the steps below to fix it.
3. Fix the PATH Issue
If virtualenv doesn’t work, it’s because the executable is not in your system’s PATH.
Find the Installation Path:
Run the following command to locate where virtualenv is installed:
python3 -m site --user-base
Example output:
/Users/ajotwani/Library/Python/3.9
The virtualenv executable is located in the bin directory of this path:
/Users/ajotwani/Library/Python/3.9/bin
Add the Path to ~/.zshrc:
Add the bin directory to your PATH by updating your ~/.zshrc file:
echo 'export PATH=$PATH:/Users/ajotwani/Library/Python/3.9/bin' >> ~/.zshrc
Reload your shell configuration:
source ~/.zshrc
4. Verify Again
After updating the PATH, check if virtualenv works:
virtualenv --version
If this now works, your setup is complete.
5. Create and Use Virtual Environments
To deactivate it:
deactivate
To activate it:
source myenv/bin/activate
To create a new virtual environment:
virtualenv myenv