Manage python environment with virtualenv

Introduction

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

Try using

Create

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)$

Check the created environment directory

--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

Try installing the module

--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.

List of frequently used commands

deactivate: Deactivate the environment

#Environmental outage
(hoge)$ deactivate
$

workon: Move to environment

If 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 environment

You can delete the entire folder, but of course there is also a command to delete the environment.

$ rmvirtualenv hoge

The end

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

Manage python environment with virtualenv
Manage Python multiple version environment with Pythonz, virtualenv
Build python virtual environment with virtualenv
Python environment with docker-compose
Clean python environment with pythonz and virtualenv
Virtual environment with Python 3.6
Building a python environment with virtualenv and direnv
Commands for creating a python3 environment with virtualenv
Work in a virtual environment with Python virtualenv.
Manage cron jobs with python
Create an environment with virtualenv
Build python3 environment with ubuntu 16.04
Prepare python3 environment with Docker
Build python environment with direnv
Specify python version with virtualenv
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python virtual environment with virtualenv and virtualenvwrapper
Manage Python runtime packages and development environment packages with Poetry
[Package cloud] Manage python packages with package cloud
Get started with Python! ~ ① Environment construction ~
Setup modern Python environment with Homebrew
Build Mysql + Python environment with docker
Create a virtual environment with Python!
Building a virtual environment with Python 3
Use rospy with virtualenv in Python3
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Manage each Python version with Homebrew
pytorch @ python3.8 environment construction with pipenv
Prepare Python development environment with Atom
When using MeCab with virtualenv python
[Pyenv] Building a python environment with ubuntu 16.04
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
FizzBuzz with Python3
Competitive programming with python Local environment settings
Scraping with Python
Change Python 64bit environment to 32bit environment with Anaconda
Building a Python3 environment with Amazon Linux2
Build Jupyter Lab (Python) environment with Docker
[Development environment] Python with Xcode [With screen transition]
Statistics with python
Use smbus with python3 under pyenv environment
Python environment construction
python environment settings
Scraping with Python
Python with Go
python windows environment
Note when creating an environment with python
[Python] Create a virtual environment with Anaconda
Environment construction (python)
Twilio with Python
Building a Python 3.6 environment with Windows + PowerShell
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
Create Python + uWSGI + Nginx environment with Docker
python starts with ()
Build Python environment with Anaconda on Mac
python environment construction
with syntax (Python)
Python --Environment construction