Fix Virtualenvwrapper workon/mkvirtualenv: command not found

workon or mkvirtualenv command not found

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 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
Output
/usr/bin/python3

To find the path to your virtualenvwrapper.sh script run:

find / -name virtualenvwrapper.sh 2>/dev/null
Output
/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
Here are mine
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:

image 1

Here’s a brief explanation of what each line does:

  1. export WORKON_HOME=$HOME/.virtualenvs: This sets the WORKON_HOME environment variable, which is the directory where your virtual environments will be stored.
  2. export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3: This sets the VIRTUALENVWRAPPER_PYTHON environment variable, which points to the Python interpreter that virtualenvwrapper should use.
  3. source /usr/local/bin/virtualenvwrapper.sh: This sources the virtualenvwrapper.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:

  1. 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.
  2. 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.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
You May Also Like
Bash For Loop
Read More

Bash For Loop

Loops are used in computer programming languages to repeat certain instructions several times. This is not only to…