首页 > 代码库 > python 环境之 venv
python 环境之 venv
需求
我们用Anisible 来管理Openstack 此时需要安装shade模块当有互联网时我们可以用以下方式安装shade模块
yum install python-pip pip install shade
可是目前公司没有互联网环境并且得用到shade这个模块,当时心想执行pip install shade 看下装了哪些包然后做成一个独立的源,不过无果。后来又想到 pip download shade然后离线安装shade。可是好麻烦最后找到了 virtualenv。
首先谈谈virtualenv是个什么东东
virtualenv故名思意为 虚拟的环境为python提供一个“隔离式”的环境比如操作系统层面需要使用python2和python3 此时若在系统里面设置环境变量会很不方便可能影响系统python环境的独立性。 当然python天然的支持若干个模块我们可以在系统层面去安装这些模块可是若换了一个开发环境我们还需要把这些模块重新安装。 若我们使用virtualenv并且在virtualenv 环境里装这些包安装完毕后我们可以把这个目录直接打包走就可以直接使用了。我们看下virtualenv 的使用方法。
1、安装pip和virtualenv
[root@bogon ~]# yum install python-virtualenv python-pip
2、创建一个 自己的project
[root@bogon Erick]# virtualenv -p /usr/bin/python2 venv Already using interpreter /usr/bin/python2 New python executable in /root/Erick/venv/bin/python2 Also creating executable in /root/Erick/venv/bin/python Installing setuptools, pip, wheel...done.
3、进入当前环境【执行完之后可以看到前面多一个(venv)】
[root@bogon Erick]# source venv/bin/activate (venv) [root@bogon Erick]#
4、好了此时可以在我们自己的项目中安装自己所需要的模块了比我要安装我需要的shade模块
(venv) [root@bogon Erick]# pip install shade 。。。省略 Collecting pyperclip (from cmd2>=0.6.7->cliff>=2.6.0->osc-lib>=1.5.1->python-ironicclient>=1.11.0->shade) Installing collected packages: six, munch, netifaces, funcsigs, pytz, Babel, pbr, oslo.i18n, monotonic, netaddr, pyparsing, wrapt, debtcollector, iso8601, oslo.utils, PyYAML, rfc3986, stevedore, oslo.config, chardet, certifi, idna, urllib3, requests, positional, keystoneauth1, msgpack-python, oslo.serialization, python-keystoneclient, dogpile.cache, jsonpointer, jsonpatch, requestsexceptions, decorator, appdirs, os-client-config, jmespath, futures, functools32, jsonschema, unicodecsv, PrettyTable, pyperclip, cmd2, cliff, simplejson, osc-lib, deprecation, openstacksdk, python-novaclient, warlock, python-glanceclient, python-cinderclient, python-openstackclient, python-ironicclient, ipaddress, shade Successfully installed Babel-2.3.4 PrettyTable-0.7.2 PyYAML-3.12 appdirs-1.4.3 certifi-2017.4.17 chardet-3.0.4 cliff-2.8.0 cmd2-0.7.5 debtcollector-1.16.0 decorator-4.1.1 deprecation-1.0.1 dogpile.cache-0.6.4 funcsigs-1.0.2 functools32-3.2.3-2 futures-3.1.1 idna-2.5 ipaddress-1.0.18 iso8601-0.1.11 jmespath-0.9.3 jsonpatch-1.16 jsonpointer-1.10 jsonschema-2.6.0 keystoneauth1-2.21.0 monotonic-1.3 msgpack-python-0.4.8 munch-2.1.1 netaddr-0.7.19 netifaces-0.10.6 openstacksdk-0.9.17 os-client-config-1.28.0 osc-lib-1.7.0 oslo.config-4.8.0 oslo.i18n-3.16.0 oslo.serialization-2.19.1 oslo.utils-3.27.0 pbr-3.1.1 positional-1.1.1 pyparsing-2.2.0 pyperclip-1.5.27 python-cinderclient-2.2.0 python-glanceclient-2.7.0 python-ironicclient-1.14.0 python-keystoneclient-3.12.0 python-novaclient-9.0.1 python-openstackclient-3.11.0 pytz-2017.2 requests-2.18.1 requestsexceptions-1.3.0 rfc3986-1.0.0 shade-1.22.2 simplejson-3.11.1 six-1.10.0 stevedore-1.24.0 unicodecsv-0.14.1 urllib3-1.21.1 warlock-1.2.0 wrapt-1.10.10 (venv) [root@bogon Erick]#
可以看到此时shade模块已经安装成功若失败请yum install python-devel 参考链接为https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory
5、若让Ansible 使用我们刚刚做好的项目在你的资产列表中添加ansible_python_interpreter="your dir" 例如
[controller] 192.168.1.100 ansible_python_interpreter="/root/Erick/venv/bin/python"
至此完成可以打包带走了
我们在来安装一个tornado模块
(venv) [root@bogon Erick]# pip install tornado Collecting tornado Downloading tornado-4.5.1.tar.gz (483kB) 100% |████████████████████████████████| 491kB 28kB/s Collecting backports.ssl_match_hostname (from tornado) Downloading backports.ssl_match_hostname-3.5.0.1.tar.gz Collecting singledispatch (from tornado) Downloading singledispatch-3.4.0.3-py2.py3-none-any.whl Requirement already satisfied: certifi in ./venv/lib/python2.7/site-packages (from tornado) Collecting backports_abc>=0.4 (from tornado) Downloading backports_abc-0.5-py2.py3-none-any.whl Requirement already satisfied: six in ./venv/lib/python2.7/site-packages (from singledispatch->tornado) Building wheels for collected packages: tornado, backports.ssl-match-hostname Running setup.py bdist_wheel for tornado ... done Stored in directory: /root/.cache/pip/wheels/84/83/cd/6a04602633457269d161344755e6766d24307189b7a67ff4b7 Running setup.py bdist_wheel for backports.ssl-match-hostname ... done Stored in directory: /root/.cache/pip/wheels/5d/72/36/b2a31507b613967b728edc33378a5ff2ada0f62855b93c5ae1 Successfully built tornado backports.ssl-match-hostname Installing collected packages: backports.ssl-match-hostname, singledispatch, backports-abc, tornado Successfully installed backports-abc-0.5 backports.ssl-match-hostname-3.5.0.1 singledispatch-3.4.0.3 tornado-4.5.1 (venv) [root@bogon Erick]#
最后看下我们装的模块在哪里
(venv) [root@bogon site-packages]# pwd /root/Erick/venv/lib/python2.7/site-packages (venv) [root@bogon site-packages]# ls -d shade tornado shade tornado
总之需要什么装什么即可最后可以一并打包带走。和去饭店吃饭是一样一样的
参考链接
https://help.dreamhost.com/hc/en-us/articles/218925908-How-to-launch-a-DreamCompute-server-with-Ansible
http://docs.python-guide.org/en/latest/dev/virtualenvs/
本文出自 “完美世界!” 博客,请务必保留此出处http://shyln.blog.51cto.com/6890594/1947956
python 环境之 venv