python virtualenv tutorial

Guide

install python

install commands

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

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

windows

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

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

temp solutions

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

install virtualenv

    sudo pip3 install virtualenv virtualenvwrapper

vim .bashrc

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

source .bashrc

mkvirtualenv

[email protected]: mkvirtualenv py3 -p python3
(py3) [email protected]:~$ 

commands

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

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:

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

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

workon py3
pip3 install opencv-contrib-python

test version

workon py3
python
import cv2
print(cv2.__version__)
'3.4.2'

install pycharm

apt-get (slow)

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

sh pycharm.sh

Reference

History

  • 20180920: created.

Author: kezunlin
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source kezunlin !
评论
  TOC