Resolving 'mkvirtualenv: command not found' Error in CentOS Systems

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: virtualenvwrapper | CentOS | environment configuration | Python virtual environment | shell scripting

Abstract: This technical article provides an in-depth analysis of the 'mkvirtualenv: command not found' error when using virtualenvwrapper on CentOS systems. Based on real-world case studies, the paper explores installation path issues of virtualenvwrapper.sh script, environment variable configuration methods, and automated script localization techniques. By comparing multiple solutions, it offers best practices for configuring virtual environments in non-standard paths, complete with code examples and configuration instructions.

Problem Background and Diagnosis

When configuring Python virtual environments on CentOS 6 systems, users often encounter the mkvirtualenv: command not found error message. This issue typically arises from the virtualenvwrapper.sh script not being properly loaded into the shell environment. According to the user's configuration, the WORKON_HOME environment variable was set to /opt/virtual_env/, but the attempted script file /opt/virtual_env/bin/virtualenvwrapper_bashrc did not exist.

Core Problem Analysis

Through detailed analysis, the root cause was identified as the actual installation path of virtualenvwrapper.sh script differing from expectations. In standard CentOS installations, this script may be installed in the /usr/bin/ directory rather than /usr/local/bin/. This discrepancy prevents the source file path configured in .bash_profile from locating the corresponding script.

Solution Implementation

Solution 1: Direct Script Path Specification

Modify the user configuration file ~/.bash_profile to point the source command to the correct script path:

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"

This approach directly resolves the path localization issue without relying on the virtualenvwrapper_bashrc file. After configuration, reload the profile or restart the terminal session for changes to take effect.

Solution 2: Dynamic Script Localization

Use the which command to dynamically locate the virtualenvwrapper.sh installation:

source `which virtualenvwrapper.sh`

This method leverages shell command substitution to automatically search for the script file in the $PATH environment variable. To ensure automatic loading on each shell startup, add the following command to the configuration file:

echo "source `which virtualenvwrapper.sh`" >> ~/.bash_profile

Configuration Optimization and Practical Recommendations

When configuring virtualenvwrapper, follow these best practices:

  1. Before modifying configuration files, use find /usr -name virtualenvwrapper.sh to confirm the exact script location
  2. Place both environment variable settings and script loading commands in ~/.bash_profile or ~/.bashrc
  3. Use source ~/.bash_profile or restart the terminal to apply configuration changes
  4. For production environments, prefer absolute paths over dynamic lookup for improved stability

Technical Principles Deep Dive

virtualenvwrapper operates based on shell environment management and Python virtual environment technology. When executing source virtualenvwrapper.sh, the script:

Script loading failure prevents these functions from being available in the current shell session, resulting in command not found errors.

Compatibility Considerations

While this article primarily focuses on CentOS 6 and Python 2.6.6 environments, the described solutions apply equally to other Linux distributions and Python versions. Key differences across systems include:

In practical applications, adjust paths and configuration methods according to specific environment requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.