When developing in Python you most likely will need to install Python modules that will provide some type of functionality, the process is very simple:
Install the Python package manager:
sudo yum install python-pip -y
Install modules, for example ‘yaml’, this is without virtual environments, this is basically installing a Python module system-wide:
sudo pip install pyaml
The above will install Python modules for the system, but to do so you need ‘root’ privileges (e.g. sudo), and there is a possibility that developers don’t have ‘root’ privileges.
Another important scenario to consider is that installing/updating/modifying system-wide Python modules could affect other existing Python projects in the same system.A way to overcome the system-wide python dependencies is to work with your own virtual environment, which allows you to install Python modules in a Python virtual environment and without the need for ‘root’ privileges.
What this accomplishes is to have your Python projects have their specific dependencies met without affecting anyone else.
To develop using Python virtual environments, the system should have the virtualenv Python package first:
[vagrant@vagrant-box ~]$ sudo yum install python-virtualenv
Now as a regular user (no root or sudo needed):
# Create (or cd into) your Python project folder
$ mkdir my_python_project $ cd my_python_project
# Create Python Virtual Environment, you can name it what you want, I chose to name it ‘env’
[vagrant@vagrant-box my_python_project]$ virtualenv env New python executable in env/bin/python Installing Setuptools..............................................................................................................................................................................................................................done. Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
# Activating the virtual environment
To use the virtual environment (as opposed to the system-wide Python environment) you need to activate it first. After it is activated you will see it in the left your command prompt.
# System-wide Python
[vagrant@vagrant-box my_python_project]$ which python /usr/local/bin/python
# Activate Virtual Env
[vagrant@vagrant-box my_python_project]$ source env/bin/activate
# Ready to use Python Virtual Env
(env)[vagrant@vagrant-box my_python_project]$ which python /vagrant/my_python_project/env/bin/python
# Installing modules in the virtual environment
[vagrant@vagrant-box my_python_project]$ pip install pyvmomi Downloading/unpacking pyvmomi Downloading pyvmomi-5.5.0.2014.1.1.tar.gz (198kB): 198kB downloaded Running setup.py egg_info for package pyvmomi Downloading/unpacking requests>=2.3.0 (from pyvmomi) Downloading requests-2.5.1.tar.gz (443kB): 443kB downloaded Running setup.py egg_info for package requests Downloading/unpacking six>=1.7.3 (from pyvmomi) Downloading six-1.9.0.tar.gz Running setup.py egg_info for package six no previously-included directories found matching 'documentation/_build' Installing collected packages: pyvmomi, requests, six Running setup.py install for pyvmomi Running setup.py install for requests Running setup.py install for six no previously-included directories found matching 'documentation/_build' Successfully installed pyvmomi requests six Cleaning up...
# Uninstalling modules in the virtual environment
[vagrant@vagrant-box my_python_project]$pip uninstall pyvmomi Uninstalling pyvmomi: /vagrant/my_python_project/env/LICENSE.txt /vagrant/my_python_project/env/MANIFEST.in /vagrant/my_python_project/env/NOTICE.txt /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVim/__init__.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVim/__init__.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVim/connect.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVim/connect.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Cache.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Cache.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/CoreTypes.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/CoreTypes.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Differ.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Differ.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/DynamicTypeManagerHelper.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/DynamicTypeManagerHelper.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Iso8601.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Iso8601.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/ManagedMethodExecutorHelper.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/ManagedMethodExecutorHelper.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/ServerObjects.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/ServerObjects.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/SoapAdapter.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/SoapAdapter.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/StubAdapterAccessorImpl.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/StubAdapterAccessorImpl.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Version.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/Version.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/VmomiSupport.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/VmomiSupport.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/__init__.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/__init__.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/pyVmomiSettings.py /vagrant/my_python_project/env/lib/python2.6/site-packages/pyVmomi/pyVmomiSettings.pyc /vagrant/my_python_project/env/lib/python2.6/site-packages/pyvmomi-5.5.0.2014.1.1-py2.6.egg-info /vagrant/my_python_project/env/setup.cfg /vagrant/my_python_project/env/setup.py /vagrant/my_python_project/env/tox.ini Proceed (y/n)? y Successfully uninstalled pyvmomi
# Install multiple python modules from a requirements file
If your project has multiple Python modules required, it is better to create a requirements.txt file with the list of Python modules.
[vagrant@vagrant-box my_python_project]$ cat requirements.txt argparse pyaml
Then you can install all the listed Python modules in your virtual environment as follows:
[vagrant@vagrant-box my_python_project]$ pip install -r requirements.txt
# Deactivate virtual environment (go back to using system-wide Python)
[vagrant@vagrant-box my_python_project]$ deactivate
Comments
Leave a comment Trackback