
PYTHON — Leaving and Switching Between Python Virtual Environments
Technology is best when it brings people together. — Matt Mullenweg

PYTHON — Python- Working with Unix Time
Leaving and switching between virtual environments in Python is a common task for many developers. In this article, we’ll cover how to leave an active virtual environment and how to switch between different virtual environments using Python’s built-in tools. Let’s dive into the terminal and explore these concepts through examples.
Leaving an Active Virtual Environment
When working in an active Python virtual environment, you can leave it and return to the global environment by using the deactivate command. This command is available whenever you're working in an active virtual environment.
deactivateAfter running the deactivate command, the virtual environment marker in your shell prompt will be removed, indicating that you have returned to the global environment.
Switching Between Virtual Environments
To switch between different virtual environments when working with multiple projects, first leave the active virtual environment. Then, navigate to the directory of the new project that contains another virtual environment and activate it using the activate command.
deactivate # Leave the current virtual environmentsource path_to_virtual_env/bin/activate # Activate the new virtual environmentIt’s a good practice to deactivate or leave the active virtual environment before activating a new one to avoid conflicts between different environments.
Customizing Virtual Environment Text
A common question is whether it’s possible to customize the (venv) text shown in the shell prompt to visually distinguish between different virtual environments.
The virtual environment prefix in the shell prompt is determined by the folder name of the virtual environment. For example, if you create a virtual environment with the folder name my_virtualenv, the prompt text will be (my_virtualenv).
python3 -m venv my_virtualenv
. /my_virtualenv/bin/activateBy creating a virtual environment with a different folder name, you can customize the prompt text to reflect that change.
Conclusion
In this article, we’ve covered how to leave an active virtual environment using the deactivate command and how to switch between different virtual environments by activating a new environment. We've also discussed how to customize the text shown in the shell prompt to visually distinguish between different virtual environments. These are important concepts for managing Python virtual environments effectively.







