ThoughtWorks
  • 联系我们
  • Español
  • Português
  • Deutsch
  • English
概况
  • 工匠精神和科技思维

    采用现代的软件开发方法,更快地交付价值

    智能驱动的决策机制

    利用数据资产解锁新价值来源

  • 低摩擦的运营模式

    提升组织的变革响应力

    企业级平台战略

    创建与经营战略发展同步的灵活的技术平台

  • 客户洞察和数字化产品能力

    快速设计、交付及演进优质产品和卓越体验

    合作伙伴

    利用我们可靠的合作商网络来扩大我们为客户提供的成果

概况
  • 汽车企业
  • 清洁技术,能源与公用事业
  • 金融和保险企业
  • 医疗企业
  • 媒体和出版业
  • 非盈利性组织
  • 公共服务机构
  • 零售业和电商
  • 旅游业和运输业
概况

特色

  • 技术

    深入探索企业技术与卓越工程管理

  • 商业

    及时了解数字领导者的最新业务和行业见解

  • 文化

    分享职业发展心得,以及我们对社会公正和包容性的见解

数字出版物和工具

  • 技术雷达

    对前沿技术提供意见和指引

  • 视野

    服务数字读者的出版物

  • 数字化流畅度模型

    可以将应对不确定性所需的数字能力进行优先级划分的模型

  • 解码器

    业务主管的A-Z技术指南

所有洞见

  • 文章

    助力商业的专业洞见

  • 博客

    ThoughtWorks 全球员工的洞见及观点

  • 书籍

    浏览更多我们的书籍

  • 播客

    分析商业和技术最新趋势的精彩对话

概况
  • 申请流程

    面试准备

  • 毕业生和变换职业者

    正确开启技术生涯

  • 搜索工作

    在您所在的区域寻找正在招聘的岗位

  • 保持联系

    订阅我们的月度新闻简报

概况
  • 会议与活动
  • 多元与包容
  • 新闻
  • 开源
  • 领导层
  • 社会影响力
  • Español
  • Português
  • Deutsch
  • English
ThoughtWorks菜单
  • 关闭   ✕
  • 产品及服务
  • 合作伙伴
  • 洞见
  • 加入我们
  • 关于我们
  • 联系我们
  • 返回
  • 关闭   ✕
  • 概况
  • 工匠精神和科技思维

    采用现代的软件开发方法,更快地交付价值

  • 客户洞察和数字化产品能力

    快速设计、交付及演进优质产品和卓越体验

  • 低摩擦的运营模式

    提升组织的变革响应力

  • 智能驱动的决策机制

    利用数据资产解锁新价值来源

  • 合作伙伴

    利用我们可靠的合作商网络来扩大我们为客户提供的成果

  • 企业级平台战略

    创建与经营战略发展同步的灵活的技术平台

  • 返回
  • 关闭   ✕
  • 概况
  • 汽车企业
  • 清洁技术,能源与公用事业
  • 金融和保险企业
  • 医疗企业
  • 媒体和出版业
  • 非盈利性组织
  • 公共服务机构
  • 零售业和电商
  • 旅游业和运输业
  • 返回
  • 关闭   ✕
  • 概况
  • 特色

  • 技术

    深入探索企业技术与卓越工程管理

  • 商业

    及时了解数字领导者的最新业务和行业见解

  • 文化

    分享职业发展心得,以及我们对社会公正和包容性的见解

  • 数字出版物和工具

  • 技术雷达

    对前沿技术提供意见和指引

  • 视野

    服务数字读者的出版物

  • 数字化流畅度模型

    可以将应对不确定性所需的数字能力进行优先级划分的模型

  • 解码器

    业务主管的A-Z技术指南

  • 所有洞见

  • 文章

    助力商业的专业洞见

  • 博客

    ThoughtWorks 全球员工的洞见及观点

  • 书籍

    浏览更多我们的书籍

  • 播客

    分析商业和技术最新趋势的精彩对话

  • 返回
  • 关闭   ✕
  • 概况
  • 申请流程

    面试准备

  • 毕业生和变换职业者

    正确开启技术生涯

  • 搜索工作

    在您所在的区域寻找正在招聘的岗位

  • 保持联系

    订阅我们的月度新闻简报

  • 返回
  • 关闭   ✕
  • 概况
  • 会议与活动
  • 多元与包容
  • 新闻
  • 开源
  • 领导层
  • 社会影响力
博客
选择主题
查看所有话题关闭
技术 
敏捷项目管理 云 持续交付 数据科学与工程 捍卫网络自由 演进式架构 体验设计 物联网 语言、工具与框架 遗留资产现代化 Machine Learning & Artificial Intelligence 微服务 平台 安全 软件测试 技术策略 
商业 
金融服务 全球医疗 创新 零售行业 转型 
招聘 
职业心得 多元与融合 社会改变 
博客

话题

选择主题
  • 技术
    技术
  • 技术 概观
  • 敏捷项目管理
  • 云
  • 持续交付
  • 数据科学与工程
  • 捍卫网络自由
  • 演进式架构
  • 体验设计
  • 物联网
  • 语言、工具与框架
  • 遗留资产现代化
  • Machine Learning & Artificial Intelligence
  • 微服务
  • 平台
  • 安全
  • 软件测试
  • 技术策略
  • 商业
    商业
  • 商业 概观
  • 金融服务
  • 全球医疗
  • 创新
  • 零售行业
  • 转型
  • 招聘
    招聘
  • 招聘 概观
  • 职业心得
  • 多元与融合
  • 社会改变
语言、工具与框架Bangalore技术

Python Working Environment for Beginners: Part 2

Surya Sreedevi Vedula Surya Sreedevi Vedula

Published: Oct 24, 2014

Part 2 of Python Working Environment. For ways to set Python Working Environment, check out Part 1 here.

Creating Virtual Environments

Understanding Virtual Environments

As we saw in the Installing Packages section , when we install third-party dependencies using pip (or easy_install), they are installed in the <python-base-dir>/lib/python<major>.<minor>/site-packages directory. But, what if two different applications (hosted on the same machine) need two different versions of the same package? Virtual Environments solve this problem by isolating one application's environment from another. These environments are based on a Python installation and can be considered as light-weight containers for holding application-specific dependencies. Virtual environments also come in handy when we do not have write permissions to the Python installation's site-packages directory.

Python virtual environments were originally provided by “virtualenv” package. Starting from Python 3.3, virtual environments have been added to Python language in the module “venv”. In this post, we will see how to create virtual environments using “virtualenv” as well as Python’s out-of-the-box virtual environments provided using “venv”.

Virtual Environments using "virtualenv"

“virtualenv” creates an isolated Python environment in a directory structure which contains the “site-packages” directory. When we activate the virtual environment and install packages, the packages are placed in the virtual environment's “site-packages” directory instead of Python Installation's site-packages directory.

Let us install virtualenv and try to create a virtual environment using it.

# Update PATH to include Python Installation's bin directory
$ export PATH= <python-base-dir>/bin:$PATH
# Install virtualenv using pip.
$ pip install virtualenv

After the installation of “virtualenv”, we can create a new virtual environment in an empty / non-existent directory using “virtualenv”.

$ virtualenv my-project-env

The command virtualenv does the below important things:

  • Creates “python”, “easy_install” and “pip” executables for the virtual environment in “bin” directory
  • Creates a “lib” directory to hold all the libraries and a lib/python<major>.<minor>/site-packages directory to hold the third-party dependencies.
  • Creates an activate script in the “bin” directory which is used to bring the virtual environment into effect.

Let us now proceed to activate the environment and install a package.

# Activate the Python Environment. This also changes the shell prompt to indicate the environment.
$ cd my-project-env
$ source bin/activate

Observe that the shell prompt is changed to (my-project-env) indicating that the virtual environment is activated.

Let us now try to install “requests” package in the virtual environment and notice that it is installed in the “site-packages” of the virtual environment and isolated from the Python installation. 

# Install “requests” package in the virtual environment.
(my-project-env) $ pip install requests
# Verify that “requests” is installed in virtual environment using “pip show” command.
(my-project-env) $ pip show requests

Once you are done working with the virtual environment, you can deactivate the virtual environment using the function call 'deactivate'.

(my-project-env) $ deactivate

Virtual Environments using "venv"

Virtual environments have been added to Python language from Python 3.3 in the module “venv”. The “pyvenv” executable helps you create the virtual environments. The detailed documentation can be found here. One important difference in Python 3.3 virtual environments from those created by “virtualenv” package is that the binaries “pip” and “easy_install” are not copied over to “bin” directory, because of which, we should install these packages additionally in the virtual environment. However, with Python 3.4.1, this issue has been fixed and "pip" executable is made available in the bin directory of the virtual environment for our ready use.

Let us try to create a virtual environment using "pyvenv" executable, install "requests" package and verify that the packages are installed in the virtual environment.

# Create a virtual environment in the directory my-project-env.
$ pyvenv my-project-venv
# Change to the virtual environment directory.
$ cd my-project-venv
# Activate the virtual environment.
$ source bin/activate
# Install “requests” package.
(my-project-venv) $ pip install requests
# Observe that “requests” is installed in the virtual environment using “pip show” command.
(my-project-venv) $ pip show requests

Once you are done working with the virtual environment, you can deactivate the virtual environment using the function call 'deactivate'.

(my-project-env) $ deactivate

Summary

In a nutshell, setting up basic Python environments involves installing the required version of Python, creating virtual environments for your project based on the Python installation and installing the required third-party dependencies using “pip”.

I hope this post helps you understand how to set up basic Python environments and gives an overview of the ecosystem around packages and their management using virtual environments.

References

  • Installing Python from Sources
  • Setuptools documentation
  • pip documentation
  • Why use pip over easy_install - Stack Overflow

Ready to shape the future of tech?

Join our team of passionate and bright technologists.

Join us
  • 产品及服务
  • 合作伙伴
  • 洞见
  • 加入我们
  • 关于我们
  • 联系我们

WeChat

×
QR code to ThoughtWorks China WeChat subscription account

媒体与第三方机构垂询 | 政策声明 | Modern Slavery statement ThoughtWorks| 辅助功能 | © 2021 ThoughtWorks, Inc.