lost and found ( for me ? )

Python : how to use virtualenv

Reference


Small tips.
With virtualenv, you can create python virtual environment separately for each projects.


install virtualenv
root@ubuntu:~# apt-cache install python-pip

root@ubuntu:~# pip install virtualenv

# virtualenv --version
1.11.6


create python virtual environment under dest_dir1 directory.
root@ubuntu:/home/hattori/Python_works# virtualenv dest_dir1
New python executable in dest_dir1/bin/python
Installing setuptools, pip...done.


python environment has been created under dest_dir1.
root@ubuntu:/home/hattori/Python_works# ls dest_dir1/
bin  include  lib  local

root@ubuntu:/home/hattori/Python_works# ls dest_dir1/bin/
activate      activate.fish     easy_install      pip   pip2.7  python2
activate.csh  activate_this.py  easy_install-2.7  pip2  python  python2.7


use this env and install requests module within that environment
python env has changed from “/usr/bin/python” to “dest_dir1/bin/python”
root@ubuntu:/home/hattori/Python_works# which python
/usr/bin/python


root@ubuntu:/home/hattori/Python_works# source dest_dir1/bin/activate
(dest_dir1)root@ubuntu:/home/hattori/Python_works#

(dest_dir1)root@ubuntu:/home/hattori/Python_works# which python
/home/hattori/Python_works/dest_dir1/bin/python

(dest_dir1)root@ubuntu:/home/hattori/Python_works# which pip
/home/hattori/Python_works/dest_dir1/bin/pip

(dest_dir1)root@ubuntu:/home/hattori/Python_works# pip install requests


“activate” file is a bash script, so you can check that to know the script executes what kind of things.
# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="/home/hattori/Python_works/dest_dir1"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH


exit this env
(dest_dir1)root@ubuntu:/home/hattori/Python_works# deactivate
root@ubuntu:/home/hattori/Python_works#
root@ubuntu:/home/hattori/Python_works# which python
/usr/bin/python


You can use different python version.
root@ubuntu:/home/hattori/Python_works# virtualenv -p /usr/bin/python3.4 dest_dir2
Running virtualenv with interpreter /usr/bin/python3.4
Using base prefix '/usr'
New python executable in dest_dir2/bin/python3.4
Also creating executable in dest_dir2/bin/python
Installing setuptools, pip...done.


(dest_dir2)root@ubuntu:/home/hattori/Python_works# which python
/home/hattori/Python_works/dest_dir2/bin/python

(dest_dir2)root@ubuntu:/home/hattori/Python_works# python --version
Python 3.4.0

(dest_dir2)root@ubuntu:/home/hattori/Python_works# deactivate
root@ubuntu:/home/hattori/Python_works#

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.