python创建新环境_10.14 创建新的Python环境

问题

You want to create a new Python environment in which you can install modules andpackages. However, you want to do this without installing a new copy of Python ormaking changes that might affect the system Python installation.

解决方案

You can make a new “virtual” environment using the pyvenv command. This commandis installed in the same directory as the Python interpreter or possibly in the Scriptsdirectory on Windows. Here is an example:

bash % pyvenv Spam

bash %

The name supplied to pyvenv is the name of a directory that will be created. Uponcreation, the Spam directory will look something like this:

bash % cd Spam

bash % ls

bin include lib pyvenv.cfg

bash %

In the bin directory, you’ll find a Python interpreter that you can use. For example:

bash % Spam/bin/python3

Python 3.3.0 (default, Oct 6 2012, 15:45:22)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> from pprint import pprint

>>> import sys

>>> pprint(sys.path)

['',

'/usr/local/lib/python33.zip',

'/usr/local/lib/python3.3',

'/usr/local/lib/python3.3/plat-darwin',

'/usr/local/lib/python3.3/lib-dynload',

'/Users/beazley/Spam/lib/python3.3/site-packages']

>>>

A key feature of this interpreter is that its site-packages directory has been set to thenewly created environment. Should you decide to install third-party packages, they willbe installed here, not in the normal system site-packages directory.

讨论

The creation of a virtual environment mostly pertains to the installation and managementof third-party packages. As you can see in the example, the sys.path variablecontains directories from the normal system Python, but the site-packages directory hasbeen relocated to a new directory.

With a new virtual environment, the next step is often to install a package manager,such as distribute or pip. When installing such tools and subsequent packages, youjust need to make sure you use the interpreter that’s part of the virtual environment.This should install the packages into the newly created site-packages directory.

Although a virtual environment might look like a copy of the Python installation, itreally only consists of a few files and symbolic links. All of the standard library files andinterpreter executables come from the original Python installation. Thus, creating suchenvironments is easy, and takes almost no machine resources.

By default, virtual environments are completely clean and contain no third-party addons.If you would like to include already installed packages as part of a virtual environment,create the environment using the –system-site-packages option. For example:

bash % pyvenv --system-site-packages Spam

bash %

More information about pyvenv and virtual environments can be found inPEP 405 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值