Understanding Python virtual environment and a little version manager experience

192 阅读2分钟

What problem had I meet?

As a django beginner, I am writing a simple application using python 3.12.0. At first everything is happy and fun until I want to add a feature to display my api using drf-yasg. It max supports python version 3.11. So my 3.12 application is stuck.

I know a little bit about Python virtual environment. Here are some conclusions that I had learned today.

What is python virtual environment and why we need it?

First, we should know what happens when we install a new package.

image.png

We can see that if we use python infact it will find our C:\Python312 folder.

image.png

This C:\Python312\Lib\site-packages folder contain all the package we installed. Let us install httpie and Hello-World-Package package and look what will happen.

python -m pip install httpie
...
ERROR: Could not install packages due to an OSError: [WinError 2] The system cannot find the file specified: 'C:\\Python312\\Scripts\\pygmentize.exe' -> 'C:\\Python312\\Scripts\\pygmentize.exe.deleteme'

python -m pip install Hello-World-Package
... 
ERROR: Could not install packages due to an OSError: [WinError 2] The system cannot find the file specified: 'C:\\Python312\\Scripts\\hello-world.exe' -> 'C:\\Python312\\Scripts\\hello-world.exe.deleteme'

I don't know why but both of them are failed. But the site-packages folder are changed and it created some other packages!

image.png


Know we know python a little about it install package logic:

  • default it will install into global folder
  • add a package may install some other folder (but if you uninstall it, others package will not uninstall)

As a frontend developer we have node_modules folder and when we use npm install it will not install into gloabl.

This is why python need virtual environment.

How to create a python virtual environment

venv

The venv is a official tool.So let us learn it first.

image.png

We create a simple project and then use python -m venv my-venv to create a python virtual environment into our project's my-venv folder.

image.png

You can find out it is similar with our root python interpreter.

Now we can activate this python virtual environment.

# activate.bat for windows cmd
# activate.ps1 for windows powershell
# activate for linux 
F:\wtf-python-env\my-venv\Scripts\activate.bat

After that cmd will auto reload means we have activated

If you are using pycharm's Terminal you should check which should you choose:

image.png

image.png

According to the official documentation, we know that it is not magic, just a copy.

image.png

After create and activate python virtual environment we test install a new package:

image.png

We can alow use pip install xxx because now it will find pip from our my-venv folder. 😀

image.png

Now we exit python virtual environment.

pipenv

pipenv is other python virtual environment tool.Let just have a try~ (But I will skip install process because it is easy

To create a pipenv's virtual environment is easy:

image.png

Let check what files it had created:

image.png

Same as venv.

To activate it, use pipenv shell. To deactivate it, use pipenv --rm (will delete that folder)

image.png

If you are using pycharm to create a python project you will notice it can help you to create virtual environment or let you to choose. Awesome:

image.png


To create virtual environment is easy. But how about to toggle different version?

I have tried pyenv but it doesn't support windows so you need pyenv for Windows and config them is horrible.

At last I think Anaconda is a good choice.Install it is easy and have China mirror website.

From bilibili video: www.bilibili.com/video/BV1ns…

anaconda清华大学开源软件镜像站 : mirrors.tuna.tsinghua.edu.cn/anaconda/ar…

验证:
conda --version
conda info
国内下载源:
conda config --set show_channel_urls yes

conda config --add channels mirrors.tuna.tsinghua.edu.cn/anaconda/pk…

conda config --add channels mirrors.tuna.tsinghua.edu.cn/anaconda/pk…

conda config --add channels mirrors.tuna.tsinghua.edu.cn/anaconda/cl…

conda config --set show_channel_urls yes

conda config --remove channels defaults

conda config --show channels

image.png

And Toggle version is easy. If you meet some error you can try use conda init then restart your cmd. image.png

At last

I think first we can use conda to toggle and activate python interpreter version. Then use that version python to create a python virtual environment. At last we can fix my problem to use python 3.11 to install drf-yasg.


Thanks for reading.