Python has a module called virtualenv
that makes it easy to create a virtual environment. If you want to install some module as a trial, you can create an environment with virutalenv
, install it there, and try it, and the system environment can be used intact. Since you can create multiple environments, it is convenient to divide by python version.
If you don't like it, just erase or use the environment.
Note that it is convenient to install virtualenv
and virtualenvwrapper
, which can easily manage it, as a set.
install and setup
ubuntu
#Install without pip
$ sudo apt-get install python-pip
#If you use python, you may get an error due to the dependency, so install this as well
$ sudo apt-get install python2.7-dev
# virtualenv /Install virtualenvwrapper
$ sudo pip install virtualenv
$ sudo pip install virtualenvwrapper
mac
Mac should be able to use pip
from the beginning.
# virtualenv /Install virtualenvwrapper
$ sudo pip install virtualenv
$ sudo pip install virtualenvwrapper
setup
To use virtualenvwrapper
, you need to add the following script to bashrc
.
$ vim ~/.basrhc #mac is`~/.bash_profile`
source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME=~/.virtualenvs
There are various options, but I want to use it in a clean state of the system, so basically I only add --nosite-package
.
# mkvirtualenv <option> <Environment name>
$ mkvirtualenv --no-site-package hoge
# --never-download :Do not DL from the network
# --system-site-packages :Use installed modules
# --no-site-packages :A virtual environment is created with all installed modules removed
#When created, the environment name will be added to the leftmost at the command prompt
(hoge)$
--Move to environment directory
(hoge)$ cdvirtualenv
#Check the directory
(hoge)$ pwd
/home/hoge/.virtualenvs/hoge
#Contents is like this
(hoge)$ ls
bin include lib local
--Back up with git
and try installing other modules
#Commit the entire environment to local git
(hoge)$ git init; git add -A; git commit -a -m "init"
#Something useful`Twisted`Try installing the module
(hoge)$ pip install twisted
--If you look at the difference with git
, various new additions have been made.
(hoge)$ git status
On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bin/cftp
# bin/ckeygen
# bin/conch
# bin/lore
# bin/mailmail
# bin/manhole
# bin/pyhtmlizer
# bin/tap2deb
# bin/tap2rpm
# bin/tapconvert
# bin/tkconch
# bin/trial
# bin/twistd
# lib/python2.7/site-packages/Twisted-15.1.0-py2.7.egg-info/
# lib/python2.7/site-packages/twisted/
# lib/python2.7/site-packages/zope.interface-4.1.2-py2.7-nspkg.pth
# lib/python2.7/site-packages/zope.interface-4.1.2-py2.7.egg-info/
# lib/python2.7/site-packages/zope/
# pip-selfcheck.json
#
--Try using the installed module immediately
#Can be imported
(hoge)$ python -c "import twisted"
--Try using it away from the environment
#Environmental outage
(hoge)$ deactivate
#I get an error when I try to use it on the original system
$ python -c "import twisted"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named twisted
This shows that it is not installed in the system environment.
deactivate
: Deactivate the environment#Environmental outage
(hoge)$ deactivate
$
workon
: Move to environmentIf you already have an environment, you can move to that environment with the workon
command.
$ workon hoge
(hoge) $
lssitepackages
: List of installed packages(hoge)$ lssitepackages
Twisted-15.1.0-py2.7.egg-info pip-6.1.1.dist-info zope
_markerlib pkg_resources zope.interface-4.1.2-py2.7-nspkg.pth
easy_install.py setuptools zope.interface-4.1.2-py2.7.egg-info
easy_install.pyc setuptools-15.0.dist-info
pip twisted
rmvirtualenv
: Erase the environmentYou can delete the entire folder, but of course there is also a command to delete the environment.
$ rmvirtualenv hoge
You can use this, even if you try various things, you can clean it up by deleting the entire environment. It's a perfect environment for me as a clumsy person (; ^ _ ^
Recommended Posts