📜  mkvirtualenv python 版本 - Python (1)

📅  最后修改于: 2023-12-03 15:32:54.925000             🧑  作者: Mango

mkvirtualenv python 版本 - Python

Introduction

As a programmer, one of the most important tools in your arsenal is a virtual environment. A virtual environment allows you to create an isolated environment for your Python projects, which means you can install packages without affecting the global Python installation on your system.

mkvirtualenv is a command that allows you to create virtual environments with ease. In this tutorial, we will cover how to use mkvirtualenv to create virtual environments based on different Python versions.

Installation

Before we start, make sure you have virtualenvwrapper installed. If you don't, you can install it using pip:

pip install virtualenvwrapper

Once you have installed virtualenvwrapper, you need to add the following lines to your shell startup file (.bashrc, .zshrc, etc.):

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
Creating a virtual environment

To create a virtual environment with mkvirtualenv, you need to specify the Python version you want to use. Let's say you want to create a virtual environment with Python 3.7. You can run the following command:

mkvirtualenv --python=/usr/bin/python3.7 myenv

This will create a virtual environment called myenv with Python 3.7.

Activating a virtual environment

To activate a virtual environment, simply run:

workon myenv

This will activate the myenv virtual environment.

Deactivating a virtual environment

To deactivate a virtual environment, simply run:

deactivate
Removing a virtual environment

To remove a virtual environment, run:

rmvirtualenv myenv

This will remove the myenv virtual environment.

Conclusion

In this tutorial, we have covered how to use mkvirtualenv to create virtual environments based on different Python versions. Virtual environments are an essential tool for every Python programmer, so make sure you start using them in your projects!