In this tutorial we’ll cover how to fix the mkvirtualenv: command not found
or workon: command not found
error on Linux after you just installed virtualenvwrapper on your Linux distro using pip
.
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
We’ll virtualenvwrapper
by adding the following lines to your shell’s startup file, usually ~/.bashrc
or ~/.zshrc
depending on the shell you are using.
~/.bashrc
or ~/.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:
which python3
/usr/bin/python3
To find the path to your virtualenvwrapper.sh
script run:
find / -name virtualenvwrapper.sh 2>/dev/null
/home/edxd/.local/bin/virtualenvwrapper.sh
Edit Your Shell’s Startup File
To do this just open ~/.bashrc
or ~/.zshrc
in your preferred text editor (I’m using bash
, and if you aren’t sure then you’re likely using bash
as well):
nano ~/.bashrc
And add the lines at the end, replacing your_python_interpreter_path
and 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 ~/.bashrc
file:
Here’s a brief explanation of what each line does:
export WORKON_HOME=$HOME/.virtualenvs
: This sets theWORKON_HOME
environment variable, which is the directory where your virtual environments will be stored.export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
: This sets theVIRTUALENVWRAPPER_PYTHON
environment variable, which points to the Python interpreter that virtualenvwrapper should use.source /usr/local/bin/virtualenvwrapper.sh
: This sources thevirtualenvwrapper.sh
script, 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 ~/.bashrc
file:
source ~/.bashrc
The 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 ~/.bashrc
or ~/.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 workon
, mkvirtualenv
and all other virtualenvwrapper commands in your command-line.
Why You May Be Getting This Error
This usually happens because your shell (like bash
or 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.
Factors include:
- 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.
Conclusion
In conclusion, virtualenvwrapper is a useful tool for managing Python virtual environments, providing convenient commands like workon
and mkvirtualenv
. However, it sometimes requires manual configuration to work properly.
By understanding the role of shell configuration files like ~/.bashrc
or ~/.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.