未加星标

Lintel Technologies: Python virtualenv Virtual Environments

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
What is virtualenv

A Virtual Environment is a tool for python to keep the dependencies required by different projects isolated in separate places, by creating virtual Python environments.

It solves the problem “ProjectX depends on libraryA of version 1.0 but ProjectY needs library of version3.0” dilemma. Keeps the global site-packages directory clean and manageable.

Without virtualenv you would need to install/uninstall dependencies while you switch between projects that are having dependencies of same libraries but different versions.

For example, with virtualenv you can work on a project which requires Django 1.8 while also maintaining a project which requires Django 1.6.

Install virtualenv

The virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables and packages that a Python project would need.

Install virtualenv usingpip:

$ pipinstallvirtualenv virtualenv syntax $ virtualenv --help Usage: virtualenv [OPTIONS] DEST_DIR Options: --versionshowprogram's versionnumberand exit -h, --helpshowthis helpmessageand exit -v, --verboseIncreaseverbosity. -q, --quietDecreaseverbosity. -p PYTHON_EXE, --python=PYTHON_EXE ThePythoninterpreterto use, e.g., --python=python2.5 willuse thepython2.5 interpreter to createthenew environment.Thedefault is the interpreterthatvirtualenvwasinstalledwith (/usr/bin/python) --clearClearoutthenon-rootinstalland startfromscratch. --no-site-packagesDEPRECATED. Retainedonlyfor backwardcompatibility. Not havingaccessto global site-packagesis nowthe default behavior. --system-site-packages Givethevirtualenvironmentaccessto theglobal site-packages. --always-copyAlwayscopyfilesratherthansymlinking. --unzip-setuptoolsUnzipSetuptoolswheninstallingit. --relocatableMakeanEXISTINGvirtualenvenvironmentrelocatable. This fixesupscriptsand makesall .pthfiles relative. --no-setuptoolsDo not installsetuptools (or pip) in thenew virtualenv. --no-pipDo not installpipin thenew virtualenv. --no-wheelDo not installwheelin thenew virtualenv. --extra-search-dir=DIR Directoryto lookfor setuptools/pipdistributionsin. This optioncanbeusedmultipletimes. --never-downloadDEPRECATED. Retainedonlyfor backwardcompatibility. This optionhasnoeffect. Virtualenvneverdownloads pipor setuptools. --prompt=PROMPTProvidesanalternativepromptprefixfor this environment. --setuptoolsDEPRECATED. Retainedonlyfor backwardcompatibility. This optionhasnoeffect. --distributeDEPRECATED. Retainedonlyfor backwardcompatibility. This optionhasnoeffect. How to use virtualenv

Create a virtual environment for a project:

$ cd workspace $ virtualenvvenv

Command virtuale venv will create a folder in the current directory with name venv and will put all necessary python libraries required to work virtual environment isolated. That is, which will contain the Python executable files, and also the pip library whereyou can use to install other packages.

Here the name of the virtual environment is venv as we have given that name to virtualenv command. If you don’t specify a name or directory, current directory will be used to create virtual environment.

You can also specifya Python interpreter of your choice.

$ virtualenv -p /usr/bin/python2.7 venv

This command will tell python virtualenv to use the Python interpreter from location specified /usr/bin/python2.7

To Startusing the virtual environment, it needs to be activated: $ sourcevenv/bin/activate

To activate virtual environment, we use the command source with argument as a file activate which will reside in bin directory of created virtual environment directory. The name of the current virtual environment will now appear on the left of the prompt (something like (venv)[email protected]:/path/to/directory $ ) to let you know that it is activated. As long as this virtual environment activated, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.

You can Install packages as usualusing pip:

$ pipinstallrequests Once you are done working with virtual environment, you can deactivate it: $ deactivate

deactivate will be aviable as a bash command once you activate virtual environment. It can be used to deactivate(exit from) virtual environment.

So, this puts you back to the system’s default Python interpreter with all its installed libraries.

If you want to delete a virtual environment, just delete its folder. (here in this case, it would be rm -rf venv .)

virtualenvOptions Creating virtual environment with the option --no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default behavior for virtualenv 1.7 and later.] Freeze Requirements

In order to keep your environment consistent or reinstall virutal environment or distriubute as package, it’s a good idea to have a list of dependencies/requirements. You can “freeze” the current state of the environment packages. To do so, run

$ pipfreeze > requirements.txt

This will create a requirements.txtfile, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using “pip list”. Later it will be easier for you or other developer to recreate or reuse environment and install dependencies.

$ pipinstall -r requirements.txt

This can help make sure consistency across installations, across deployments, and across developers.

Other environment virtualization tools for python p Dead simple interactive Python version management. pyenv Simple Python version management.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: PythonDjangoTIOPT
分页:12
转载请注明
本文标题:Lintel Technologies: Python virtualenv Virtual Environments
本站链接:http://www.codesec.net/view/483677.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(29)