Search This Blog

Showing posts with label virtualenv. Show all posts
Showing posts with label virtualenv. Show all posts

Saturday, April 20, 2013

bpython supports virtualenv

With the help of virtualenv you can create different virtual environments for Python. Each environment can have installed different modules or have the same module but in different version.

Problem

Does bpython support virtualenv by default.

Solution

Yes, bpython supports virtualenv environments by default. Below is how you can verify it.
 
root@manage2:~# python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7']
>>>

Virtualenv local configuration (see the (novaclinet) string representing the name of the environment at the beginning)
 
root@manage2:~/virtualenv.d/novaclient/bin# source  activate
(novaclient)root@manage2:~/virtualenv.d/novaclient/bin# python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7', '/root/virtualenv.d/novaclient/lib/python2.7/plat-linux2', '/root/virtualenv.d/novaclient/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/lib/python2.7/lib-old', '/root/virtualenv.d/novaclient/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages', '/root/virtualenv.d/novaclient/lib/python2.7/site-packages']

Global, system wide configuration
 
root@manage2:~# bpython
>>> import sys
>>> sys.path
['', '/usr/bin', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7']
>>>

Virtualenv local configuration
 
(novaclient)root@manage2:~/virtualenv.d/novaclient/bin# bpython
>>> import sys
>>> sys.path
['', '/root/virtualenv.d/novaclient/bin', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7', '/root/virtualenv.d/novaclient/lib/python2.7/plat-linux2', '/root/virtualenv.d/novaclient/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/lib/python2.7/lib-old', '/root/virtualenv.d/novaclient/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages']
>>>

Friday, April 5, 2013

How to create two isolated Python environment to develop with Openstack nova

This is a natural choice to relay on the package mechanism that is available for you in Linux. You don't have to worry about dependencies, compilation, some basic configuration etc. You simply run rpm -i or apt-get install or pip install to install a package.

Problem

How to create two separate Python environment to to develop and test Python Openstack nova python-novaclient

Solution

We can use a Python package called virtualenv.

To create our two separate and isolated Python environments we need to execute these commands below (the logs are from powershell).
 
PS C:\Users\radoslaw\workspace> virtualenv.exe nova-git
New python executable in nova-git\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.

PS C:\Users\radoslaw\workspace> virtualenv.exe nova-stable
New python executable in nova-stable\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.

Once the env are created we have to enabled and switch to them. It is important to enable an environment before trying to run any pip commands. If you forget the requested changes are going to be performed on your default system configuration.

In every env we will install different version of the python-novaclient package. In first we are going to use the version from official Python PyPI repository. In seconds we use the latest source code from github.
 
PS C:\Users\radoslaw\workspace> cd .\nova-stable
PS C:\Users\radoslaw\workspace\nova-stable> cd .\Scripts
PS C:\Users\radoslaw\workspace\nova-stable\Scripts> .\activate.ps1
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable\Scripts>
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable\Scripts>
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable\Scripts> pip install  python-novaclient
Downloading/unpacking python-novaclient

To install the version directly from github under the second environment.
 
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable> deactivate
PS C:\Users\radoslaw\workspace> cd .\nova-git
PS C:\Users\radoslaw\workspace\nova-git> .\Scripts\activate.ps1
(nova-git) PS C:\Users\radoslaw\workspace\nova-git> pip install -e git+https://https://github.com/openstack/python-novaclient.git#egg=
python-novaclient

To list installed versions
 
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable> pip list
iso8601 (0.1.4)
prettytable (0.7.1)
python-novaclient (2.13.0)
requests (1.2.0)
simplejson (3.1.2)

(nova-git) PS C:\Users\radoslaw\workspace\nova-git> pip list
iso8601 (0.1.4)
prettytable (0.7.1)
python-novaclient (2.13.0.4.g0fc4e12, c:\users\radoslaw\workspace\nova-git\src\python-novaclient)
requests (1.2.0)
simplejson (3.1.2)

To upgrade the packages in the environments
 
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable> pip install -U python-novaclient
(nova-git) PS C:\Users\radoslaw\workspace\nova-git> pip install -U python-novaclient

References
  1. http://www.rackspace.com/knowledge_center/article/using-python-novaclient-with-the-rackspace-cloud
  2. https://github.com/openstack/python-novaclient
  3. http://www.virtualenv.org/en/latest/
  4. http://www.pip-installer.org/en/latest/usage.html#pip-install