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

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

off999 2025-05-03 16:00 20 浏览 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

相关推荐

pip的使用及配置_pip怎么配置

要使用python必须要学会使用pip,pip的全称:packageinstallerforpython,也就是Python包管理工具,主要是对python的第三方库进行安装、更新、卸载等操作,...

Anaconda下安装pytorch_anaconda下安装tensorflow

之前的文章介绍了tensorflow-gpu的安装方法,也介绍了许多基本的工具与使用方法,具体可以看Ubuntu快速安装tensorflow2.4的gpu版本。pytorch也是一个十分流行的机器学...

Centos 7 64位安装 python3的教程

wgethttps://www.python.org/ftp/python/3.10.13/Python-3.10.13.tgz#下载指定版本软件安装包tar-xzfPython-3.10.1...

如何安装 pip 管理工具_pip安装详细步骤

如何安装pip管理工具方法一:yum方式安装Centos安装python3和python3-devel开发包>#yuminstallgcclibffi-develpy...

Python入门——从开发环境搭建到hello world

一、Python解释器安装1、在windows下步骤1、下载安装包https://www.python.org/downloads/打开后选择【Downloads】->【Windows】小编是一...

生产环境中使用的十大 Python 设计模式

在软件开发的浩瀚世界中,设计模式如同指引方向的灯塔,为我们构建稳定、高效且易于维护的系统提供了经过验证的解决方案。对于Python开发者而言,理解和掌握这些模式,更是提升代码质量、加速开发进程的关...

如何创建和管理Python虚拟环境_python怎么创建虚拟环境

在Python开发中,虚拟环境是隔离项目依赖的关键工具。下面介绍创建和管理Python虚拟环境的主流方法。一、内置工具:venv(Python3.3+推荐)venv是Python标准...

初学者入门Python的第一步——环境搭建

Python如今成为零基础编程爱好者的首选学习语言,这和Python语言自身的强大功能和简单易学是分不开的。今天千锋武汉Python培训小编将带领Python零基础的初学者完成入门的第一步——环境搭建...

全网最简我的世界Minecraft搭建Python编程环境

这篇文章将给大家介绍一种在我的世界minecraft里搭建Python编程开发环境的操作方法。目前看起来应该是全网最简单的方法。搭建完成后,马上就可以利用python代码在我的世界自动创建很多有意思的...

Python开发中的虚拟环境管理_python3虚拟环境

Python开发中,虚拟环境管理帮助隔离项目依赖,避免不同项目之间的依赖冲突。虚拟环境的作用隔离依赖:不同项目可能需要不同版本的库,虚拟环境可以为每个项目创建独立的环境。避免全局污染:全局安装的库可...

Python内置zipfile模块:操作 ZIP 归档文件详解

一、知识导图二、知识讲解(一)zipfile模块概述zipfile模块是Python内置的用于操作ZIP归档文件的模块。它提供了创建、读取、写入、添加及列出ZIP文件的功能。(二)ZipFile类1....

Python内置模块pydoc :文档生成器和在线帮助系统详解

一、引言在Python开发中,良好的文档是提高代码可读性和可维护性的关键。pydoc是Python自带的一个强大的文档生成器和在线帮助系统,它可以根据Python模块自动生成文档,并支持多种输出格式...

Python sys模块使用教程_python system模块

1.知识导图2.sys模块概述2.1模块定义与作用sys模块是Python标准库中的一个内置模块,提供了与Python解释器及其环境交互的接口。它包含了许多与系统相关的变量和函数,可以用来控制P...

Python Logging 模块完全解读_python logging详解

私信我,回复:学习,获取免费学习资源包。Python中的logging模块可以让你跟踪代码运行时的事件,当程序崩溃时可以查看日志并且发现是什么引发了错误。Log信息有内置的层级——调试(deb...

软件测试|Python logging模块怎么使用,你会了吗?

Pythonlogging模块使用在开发和维护Python应用程序时,日志记录是一项非常重要的任务。Python提供了内置的logging模块,它可以帮助我们方便地记录应用程序的运行时信息、错误和调...

取消回复欢迎 发表评论: