There is a better alternative, please see
<<VSC Developing inside a Container>>
.
To use and manage third-party libraries without messing up python environment, organizing different project that has its own unique dependencies:
pip
: package managementvirtualenv
: project and dependenciesvirtualenvwrapper
: making venv more convenient
Does not talk the package that is the form of __init__.py
under a folder, we are talking python distribution package.
Pip
Best practice:
- always work inside a virtual environment, keep things nice and clean.
- be careful using pip with
sudo
when install packages, otherwise the installation is system-wide.
Mac’s pre-installed Python is not meant for development, you can use Homebrew
to install or download Python from python.org, that will go along with pip
. For Linux, adhere to system manager to install pip or python. In Mac, try check if pip
is there and it’s version.
To install pip(2/3) on Linux:
1 | # search pip2 or pip3 package |
pip
commonly use commands:
1 | # local or global config info |
pip
is actually fetching packages from Python package index (or your own package repo)
https://pypi.org/
How to work:
- search key work directly.
- go to Browse projects -> Operating system -> Linux, then select other classifier (but this is still hard to search what is exactly needed).
- check
development status
, select package in production/stable version.
Pip install from specified repo:
1 | # use additional repo |
Virtual Environment
Combining with virtualenvwrapper
is good, recommended.
Install virtualenv
:
1 | # install system-widely |
Create virtualenv:
1 | mkdir ~/virtualenvs && cd ~/virtualenvs |
Other similar tool, this venv may pre-installed or need to pip install globally:
1 | # python >= 3.3, may popular in furture |
Syncing packages with colleagues, put this requirement file in version control to share and update:
1 | # fist activate the virtual environment |
You can specify version in pip install:
1 | pip install flask==1.0.0 |
How to manage the project and virtual environment? Separating project with virtual environment! 放在不同的文件夹中,使用时激活就行了,一般一个venv对应一个project, 但如果要测试多个不同的环境,也可以多个venvs map to one project.
1 | --dev |
Real-world example, when develop flask framework, use setup.py
with editable
pip to install packages in virtual environment, so you can edit the flask source code and it will reflect in real-time:
When would the -e, --editable option be useful with pip install?
1 | git clone https://github.com/pallets/flask |
Now have developing env for flask.
You can also see tox.ini
file in flask git repo, it is used for testing against different python versions in different virtual environments.
Virtualenvwrapper
A user-friently wrapper around virtualenv, easy creation and activation, bind projects to virtualenvs.
Setup:
1 | # install system-widely |
Operations:
1 | # show virtual environments list |
Other Future Tools
New projects: