百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

刘心向学(25)Python中的虚拟环境(python虚拟环境安装和配置)

off999 2025-05-03 16:00 12 浏览 0 评论

分享兴趣,传播快乐,

增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为大家带来文章 “刘心向学(25)Python中的虚拟环境”

欢迎您的访问。

Share interest, spread happiness,

Increase knowledge, leave a beautiful!

Dear, this is LearningYard Academy.

Today, the editor brings you an article. “Liu Xinxiang (25) Virtual Environments in Python”

Welcome to your visit.

一、思维导图(Mind Map)

二、引言(Introduction)

在Python开发中,虚拟环境(Virtual Environments)是一种非常实用的工具。它通过创建独立的环境来隔离项目依赖,避免不同项目之间的依赖冲突。本文将简要介绍虚拟环境的基本概念、其优势以及如何使用,并通过几个实际例子展示其应用。

Virtual environments are a highly practical tool in Python development. They isolate project dependencies by creating independent environments, avoiding conflicts between dependencies of different projects. This article briefly introduces the basic concepts of virtual environments, their advantages, and how to use them, along with several practical examples to demonstrate their applications.

三、什么是虚拟环境?(What is a Virtual Environment?)

虚拟环境是一个自包含的目录,其中包含了Python解释器和所需的第三方库。每个虚拟环境都有独立的依赖管理,确保项目的依赖不会相互干扰。常用的工具包括:

A virtual environment is a self-contained directory that includes a Python interpreter and the required third-party libraries. Each virtual environment has its own dependency management, ensuring that the dependencies of one project do not interfere with others. Commonly used tools include:

venv:Python 3.3及以上版本自带的标准库。

venv: A standard library included in Python 3.3 and above.

virtualenv:功能更强大的第三方工具。

virtualenv: A more powerful third-party tool.

四、虚拟环境的优势(Advantages of Virtual Environments)

依赖隔离:每个项目拥有独立的依赖关系,避免版本冲突。

Dependency Isolation: Each project has its own independent dependencies, avoiding version conflicts.

简化依赖管理:通过 requirements.txt 文件记录和复制依赖。

Simplified Dependency Management: Dependencies can be recorded and replicated using a requirements.txt file.

便于测试:可以在不同的环境中快速部署和测试代码。

Ease of Testing: Code can be quickly deployed and tested in different environments.

五、创建和使用虚拟环境(Creating and Using Virtual Environments)

创建虚拟环境

Creating a Virtual Environment

此命令会在当前目录下创建名为 myenv 的虚拟环境。

This command creates a virtual environment named myenv in the current directory.

激活虚拟环境

Activating the Virtual Environment

Windows:

macOS/Linux:

激活后,命令行提示符会显示虚拟环境的名字。

Once activated, the name of the virtual environment will appear in the command prompt.

安装和导出依赖

Installing and Exporting Dependencies

安装依赖:

Install dependencies:

导出依赖到 requirements.txt:

Export dependencies to requirements.txt:

在新环境中安装依赖:

Install dependencies in a new environment:

六、实例:项目间的依赖隔离(Example: Dependency Isolation Between Projects)

假设两个项目需要不同版本的Flask。为每个项目创建独立的虚拟环境:

Suppose two projects require different versions of Flask. You can create separate virtual environments for each project:

项目A:

Project A:

项目B:

Project B:

这样,两个项目可以互不干扰地运行。

This way, the two projects can run without interfering with each other.

七、总结(Summary)

虚拟环境:提供隔离的环境管理依赖,防止冲突。

Virtual Environments: Provide isolated environments for managing dependencies, preventing conflicts.

简化依赖管理:通过 requirements.txt 文件轻松记录和复制依赖。

Simplified Dependency Management: Easily record and replicate dependencies using a requirements.txt file.

灵活性高:适用于个人开发和团队协作。

High Flexibility: Suitable for both individual development and team collaboration.



今天的分享就到这里了。

如果您对文章有独特的想法,

欢迎给我们留言,

让我们相约明天。

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

参考资料:通义千问

参考文献:Beazley, D., & Jones, B. K. (2019). Python Cookbook (3rd ed.). O'Reilly Media.

Hettinger, R. (2019). Transforming Code into Beautiful, Idiomatic Python. PyCon US.

本文由LearningYard新学苑整理发出,如有侵权请在后台留言沟通! LearningYard新学苑

文字:song

排版:song

审核|qiu

相关推荐

python入门到脱坑经典案例—清空列表

在Python中,清空列表是一个基础但重要的操作。clear()方法是最直接的方式,但还有其他方法也可以实现相同效果。以下是详细说明:1.使用clear()方法(Python3.3+推荐)...

python中元组,列表,字典,集合删除项目方式的归纳

九三,君子终日乾乾,夕惕若,厉无咎。在使用python过程中会经常遇到这四种集合数据类型,今天就对这四种集合数据类型中删除项目的操作做个总结性的归纳。列表(List)是一种有序和可更改的集合。允许重复...

Linux 下海量文件删除方法效率对比,最慢的竟然是 rm

Linux下海量文件删除方法效率对比,本次参赛选手一共6位,分别是:rm、find、findwithdelete、rsync、Python、Perl.首先建立50万个文件$testfor...

数据结构与算法——链式存储(链表)的插入及删除,

持续分享嵌入式技术,操作系统,算法,c语言/python等,欢迎小友关注支持上篇文章我们讲述了链表的基本概念及一些查找遍历的方法,本篇我们主要将一下链表的插入删除操作,以及采用堆栈方式如何创建链表。链...

Python自动化:openpyxl写入数据,插入删除行列等基础操作

importopenpyxlwb=openpyxl.load_workbook("example1.xlsx")sh=wb['Sheet1']写入数据#...

在Linux下软件的安装与卸载(linux里的程序的安装与卸载命令)

通过apt安装/协助软件apt是AdvancedPackagingTool,是Linux下的一款安装包管理工具可以在终端中方便的安装/卸载/更新软件包命令使用格式:安装软件:sudoapt...

Python 批量卸载关联包 pip-autoremove

pip工具在安装扩展包的时候会自动安装依赖的关联包,但是卸载时只删除单个包,无法卸载关联的包。pip-autoremove就是为了解决卸载关联包的问题。安装方法通过下面的命令安装:pipinsta...

用Python在Word文档中插入和删除文本框

在当今自动化办公需求日益增长的背景下,通过编程手段动态管理Word文档中的文本框元素已成为提升工作效率的关键技术路径。文本框作为文档排版中灵活的内容容器,既能承载多模态信息(如文字、图像),又可实现独...

Python 从列表中删除值的多种实用方法详解

#Python从列表中删除值的多种实用方法详解在Python编程中,列表(List)是一种常用的数据结构,具有动态可变的特性。当我们需要从列表中删除元素时,根据不同的场景(如按值删除、按索引删除、...

Python 中的前缀删除操作全指南(python删除前导0)

1.字符串前缀删除1.1使用内置方法Python提供了几种内置方法来处理字符串前缀的删除:#1.使用removeprefix()方法(Python3.9+)text="...

每天学点Python知识:如何删除空白

在Python中,删除空白可以分为几种不同的情况,常见的是针对字符串或列表中空白字符的处理。一、删除字符串中的空白1.删除字符串两端的空白(空格、\t、\n等)使用.strip()方法:s...

Linux系统自带Python2&yum的卸载及重装

写在前面事情的起因是我昨天在测试Linux安装Python3的shell脚本时,需要卸载Python3重新安装一遍。但是通过如下命令卸载python3时,少写了个3,不小心将系统自带的python2也...

如何使用Python将多个excel文件数据快速汇总?

在数据分析和处理的过程中,Excel文件是我们经常会遇到的数据格式之一。本文将通过一个具体的示例,展示如何使用Python和Pandas库来读取、合并和处理多个Excel文件的数据,并最终生成一个包含...

【第三弹】用Python实现Excel的vlookup功能

今天继续用pandas实现Excel的vlookup功能,假设我们的2个表长成这样:我们希望把Sheet2的部门匹在Sheet1的最后一列。话不多说,先上代码:importpandasaspd...

python中pandas读取excel单列及连续多列数据

案例:想获取test.xls中C列、H列以后(当H列后列数未知时)的所有数据。importpandasaspdfile_name=r'D:\test.xls'#表格绝对...

取消回复欢迎 发表评论: