SoFunction
Updated on 2024-12-13

Tutorial on setting up a virtual environment and changing mirrors in pycharm

Setting up a virtual environment for a project

Python's virtual environment can be similar to the Tomcat container in Java.
Its main purpose is to create mutually independent runtime environments for different projects. In the virtual environment, each project has its own package, independent of other projects. In a virtual environment, each project has its own package, independent of other projects. ** Different virtual environments can have different versions of the same package. **There is no limit to the number of virtual environments.
After creating a Python project, a virtual environment named venv will be created by default. Of course, we can also create our own virtual environment with the following command:

Creating a Virtual Environment Method 1

The following command sets up a virtual environment named test_venv for the project python_demo_1.

python3 -m venv test_venv

在这里插入图片描述

Creating a Virtual Environment Method 2

To create a virtual environment via the virtualenv command, first install the virtual environment tool via the pip command

pip install virtualenv

Then, we create a virtual environment named "evn_demo"

virtualenv evn_demo

在这里插入图片描述

Activate the virtual environment

To activate a virtual environment on Windows, just go to the Scripts directory of the virtual environment you want to activate and run command. Similarly: to exit a virtual environment, you only need to execute thecommand. As shown in the following figure:

在这里插入图片描述

The command to activate the virtual environment under Linux is:

source  env_demo/bin/activate

Exiting the virtual environment

deactivate

Creating a virtual environment without commands

The way to do this is byFile---->Settings—>Project:python_deom_1(Project Name)—>Project Interpreter---->Add Local

在这里插入图片描述

在这里插入图片描述

Search third-party libraries

A Python project always depends on a lot of third-party libraries, for example, now we're going to introduce thewebsocketlibrary, but don't know the version, what should we do? Again, we can search on pycharm as follows:
File---->Settings->Project:python_deom_1 (project name)->Project Interpreter Click on the **+** sign in the list of all third-party libraries.

在这里插入图片描述

In the following picture, we are searching for the websocket package, and the latest version we found is 0.2.1. Then, click the Install Package button to install the package. Of course, we can also check the Specify version to select another version.

在这里插入图片描述

Click the Manage Repositories button to change the mirror source of pip. Here you can change the official repositories to Tsinghua repositories.

/simple

在这里插入图片描述

to this article on pycharm set up a virtual environment and replace the mirror tutorial article is introduced to this, more related pycharm virtual environment content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!