本文主要是介绍How to manage Python environment based on virtualenv in Ubuntu 22.04,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
How to manage Python environment based on virtualenv in Ubuntu
- 安装
- 使用
- 创建环境
- 激活环境
- 安装软件包
- 退出环境
- 移除环境
安装
pip3 install virtualenv
使用
创建环境
lwk@qwfys:~$ mkdir ~/project/harbin
lwk@qwfys:~$ cd ~/project/harbin
lwk@qwfys:~/project/harbin$ virtualenv .venv
created virtual environment CPython3.10.12.final.0-64 in 171mscreator CPython3Posix(dest=/home/lwk/project/harbin/.venv, clear=False, no_vcs_ignore=False, global=False)seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/lwk/.local/share/virtualenv)added seed packages: pip==24.0, setuptools==69.1.0, wheel==0.42.0activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
lwk@qwfys:~/project/harbin$ ll
total 12
drwxrwxr-x 3 lwk lwk 4096 Mar 15 16:31 ./
drwxrwxr-x 15 lwk lwk 4096 Mar 15 15:54 ../
drwxrwxr-x 4 lwk lwk 4096 Mar 15 16:31 .venv/
lwk@qwfys:~/project/harbin$ ll .venv/
total 24
drwxrwxr-x 4 lwk lwk 4096 Mar 15 16:31 ./
drwxrwxr-x 3 lwk lwk 4096 Mar 15 16:31 ../
drwxrwxr-x 2 lwk lwk 4096 Mar 15 16:31 bin/
-rw-rw-r-- 1 lwk lwk 40 Mar 15 16:31 .gitignore
drwxrwxr-x 3 lwk lwk 4096 Mar 15 16:31 lib/
-rw-rw-r-- 1 lwk lwk 211 Mar 15 16:31 pyvenv.cfg
lwk@qwfys:~/project/harbin$ ll .venv/bin/
total 64
drwxrwxr-x 2 lwk lwk 4096 Mar 15 16:31 ./
drwxrwxr-x 4 lwk lwk 4096 Mar 15 16:31 ../
-rw-rw-r-- 1 lwk lwk 2249 Mar 15 16:31 activate
-rw-rw-r-- 1 lwk lwk 1516 Mar 15 16:31 activate.csh
-rw-rw-r-- 1 lwk lwk 3079 Mar 15 16:31 activate.fish
-rw-rw-r-- 1 lwk lwk 2764 Mar 15 16:31 activate.nu
-rw-rw-r-- 1 lwk lwk 1650 Mar 15 16:31 activate.ps1
-rw-rw-r-- 1 lwk lwk 1337 Mar 15 16:31 activate_this.py
-rwxrwxr-x 1 lwk lwk 261 Mar 15 16:31 pip*
-rwxrwxr-x 1 lwk lwk 261 Mar 15 16:31 pip3*
-rwxrwxr-x 1 lwk lwk 261 Mar 15 16:31 pip-3.10*
-rwxrwxr-x 1 lwk lwk 261 Mar 15 16:31 pip3.10*
lrwxrwxrwx 1 lwk lwk 16 Mar 15 16:31 python -> /usr/bin/python3*
lrwxrwxrwx 1 lwk lwk 6 Mar 15 16:31 python3 -> python*
lrwxrwxrwx 1 lwk lwk 6 Mar 15 16:31 python3.10 -> python*
-rwxrwxr-x 1 lwk lwk 248 Mar 15 16:31 wheel*
-rwxrwxr-x 1 lwk lwk 248 Mar 15 16:31 wheel3*
-rwxrwxr-x 1 lwk lwk 248 Mar 15 16:31 wheel-3.10*
-rwxrwxr-x 1 lwk lwk 248 Mar 15 16:31 wheel3.10*
lwk@qwfys:~/project/harbin$ ll .venv/lib/
total 12
drwxrwxr-x 3 lwk lwk 4096 Mar 15 16:31 ./
drwxrwxr-x 4 lwk lwk 4096 Mar 15 16:31 ../
drwxrwxr-x 3 lwk lwk 4096 Mar 15 16:31 python3.10/
lwk@qwfys:~/project/harbin$
激活环境
lwk@qwfys:~/project/harbin$ source .venv/bin/activate
(.venv) lwk@qwfys:~/project/harbin$
安装软件包
pip install package-name
退出环境
(.venv) lwk@qwfys:~/project/harbin$ deactivate
lwk@qwfys:~/project/harbin$ deactivate
移除环境
lwk@qwfys:~/project/harbin$ rm -rf .venv
参考文献
- 王战山的学习笔记 / 1-Python - Virtualenv
- Guide to Python / Virtual Environments
- medium / Virtualenv and local environment in Python, and pip
- 如何在 Python 中创建一个虚拟环境,以及为什么需要它
- 阿里云/开发者社区/Python 虚拟环境 看这一篇就够了
这篇关于How to manage Python environment based on virtualenv in Ubuntu 22.04的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!