In this tutorial we’ll cover how to fix the
mkvirtualenv: command not found or
workon: command not founderror on Linux after you just installed virtualenvwrapper on your Linux distro using
This error happens when virtualenvwrapper isn’t installed or configured correctly. So we’re assuming you just installed virtualenvwrapper on your Linux distro.
Fix workon or mkvirtualenv: command not found by Updating Your Shell’s Startup File
virtualenvwrapper by adding the following lines to your shell’s startup file, usually
~/.zshrc depending on the shell you are using.
~/.zshrc are files that store settings for your command-line interface (shell). When you add virtualenvwrapper configurations to these files and restart the shell, it becomes aware of the workon or mkvirtualenv commands, allowing you to create and manage Python virtual environments easily.
This is important. First we’ll need to find the paths to our Python interpreter and our virtualenvwrapper script.
Find Paths to Python Interpreter and virtualenvwrapper.sh Script
To find the path to your Python interpreter run the following command:
To find the path to your
virtualenvwrapper.sh script run:
find / -name virtualenvwrapper.sh 2>/dev/null
Edit Your Shell’s Startup File
To do this just open
~/.zshrc in your preferred text editor (I’m using
bash, and if you aren’t sure then you’re likely using
bash as well):
And add the lines at the end, replacing
your_virtualenvwrapper.sh_path with the ones from your system:
export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=your_python_interpreter_path source your_virtualenvwrapper.sh_path
export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /home/edxd/.local/bin/virtualenvwrapper.sh
Here’s a preview of my
Here’s a brief explanation of what each line does:
export WORKON_HOME=$HOME/.virtualenvs: This sets the
WORKON_HOMEenvironment variable, which is the directory where your virtual environments will be stored.
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3: This sets the
VIRTUALENVWRAPPER_PYTHONenvironment variable, which points to the Python interpreter that virtualenvwrapper should use.
source /usr/local/bin/virtualenvwrapper.sh: This sources the
virtualenvwrapper.shscript, making the virtualenvwrapper commands available in your shell.
Reload Your Shell’s Startup File
Now we just need to run a command to reload our
source command is a shell builtin that reads and executes commands from a file in the current shell environment.
It is used to apply changes made to a configuration file, like
~/.zshrc, without requiring the user to open a new shell session, allowing the updated settings to take effect immediately.
That’s it. Now you should be able to use both the
mkvirtualenv and all other virtualenvwrapper commands in your command-line.
Why You May Be Getting This Error
This usually happens because your shell (like
zsh) doesn’t recognize or know where to access virtualenvwrapper commands.
The likely cause of this is that the shell wasn’t configured automatically upon installing virtualenvwrapper.
- Installation method: Some tools provide installation scripts or packages that automatically set up the necessary configurations, like modifying the shell startup files as part of the installation process. However, if you install the tool using a different method, such as manually or via a package manager like
pip, the automatic configuration might not be applied.
- Package manager limitations: When installing a tool through a package manager like
pip, the package manager may not have the permissions or the information needed to modify the shell’s configuration files. In such cases, the package manager will install the tool, but the you need to manually add the appropriate configuration to their shell startup file.
In conclusion, virtualenvwrapper is a useful tool for managing Python virtual environments, providing convenient commands like
mkvirtualenv. However, it sometimes requires manual configuration to work properly.
By understanding the role of shell configuration files like
~/.zshrc and the source command, users can effectively set up and troubleshoot virtualenvwrapper on their systems.
This knowledge ensures a smoother experience when working with virtual environments and improves overall productivity in Python development.