0%

python virtualenv tutorial

Guide

install python

install commands

1
2
3
4
sudo apt-get install python3-pip python3-dev

pip3 -V
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

change pip source

ubuntu

edit .pip/pip.conf

1
2
3
4
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

windows

edit C:\Users\zunli\AppData\Roaming\pip\pip.ini

1
2
3
4
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

temp solutions

1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.4.0

install virtualenv

1
sudo pip3 install virtualenv virtualenvwrapper

vim .bashrc

1
2
3
4
# for virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.local
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

source .bashrc

mkvirtualenv

1
2
kezunlin@ke: mkvirtualenv py3 -p python3
(py3) kezunlin@ke:~$

commands

1
2
3
4
5
6
7
8
9
10
ls $WORKON_HOME
mkvirtualenv py3 -p python3
mkvirtualenv py2 -p python2
rmvirtualenv py3

lsvirtualenv
lssitepackages

workon py3
deactivate

Opencv with virtualenv

python2

OpenCV should now be installed in

1
2
locate cv2.so
/usr/local/lib/python2.7/dist-packages/cv2.so

However, our py2 virtual environment is located in our home directory — thus to use OpenCV within our py2 environment, we first need to sym-link OpenCV into the site-packages directory of the py2 virtual environment:

1
2
3
4
5
6
7
8
9
10
11
12
13
cd ~/.local/py2/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
```

import opencv

```bash
workon py2
python
>import cv2
>print(cv2.__version__)
'3.1.0'

python3

you may get error

ImportError: dynamic module does not define init function (PyInit_cv2) 

when import cv2 in python3 (no such problem in python2).

install opencv-python

1
2
workon py3
pip3 install opencv-contrib-python

test version

1
2
3
4
5
workon py3
python
import cv2
print(cv2.__version__)
'3.4.2'

install pycharm

apt-get (slow)

1
2
3
4
5
6
7
8
9
10
11
12
sudo add-apt-repository ppa:mystic-mirage/pycharm

sudo apt update

# no free
sudo apt install pycharm

# free
sudo apt install pycharm-community

# remove
sudo apt remove pycharm pycharm-community && sudo apt autoremove

offical (faster)

download from here

start by

1
sh pycharm.sh

Reference

History

  • 20180920: created.