Installation, IDEs etc.
Installation, IDEs etc.
Python can be installed right away from the official site or use conda distros.
Anaconda - Full distro with hundreds of shipped packages (not needed in reality)
Miniconda - minimal distro with basic packages
Recommended as it comes with
conda
package and env manager
After installation pip
tool (Python package manager) can be used to manage 3rd party packages
Main commands:
install, upgrade package and install requirements from a file:
uninstall package:
list and export all installed packages:
Similarly you can manage packages via conda
(if installed *conda distro):
Virtual environments
Virtual environment is the way of isolation the Python installation from other environments allowing to maintain the same versions of all packages as it was planned/required/designed for. This allows to mimic the working environment on other system and contribute in the development. Also with this we can run tests of different Python programs with different dependencies locally, on remote machines or clouds.
There are quite a few possible solutions for virtualizing the environment:
Almost default solution (some part of it were integrated into Python 3.3 as module
venv
). Very simple but robust way of creating an isolated environment at a given place.
The most popular modern method of virtual environment on production is using specialized Python distribution and it's own environment manager. You can create, export or delete environments.
It is the a de facto standard for data science or AI/ML-related projects.
It has faster mimicking alternative called mamba
Modern and slick packaging, dependency resolver and virtual env management tool. You can do some magic things like having separate dev/prod requirements, project relocation, publishing helpers.
virtualenv
Virtual environment creation tool which allows to keep different version of Python with different version of packages.
You need to install it via pip:
pip install virtualenv
Usage:
virtualenvwrapper
Better tool to manage different python virtual environment is virtualenvwrapper.
Windows clone:
Main feature is controlling all available environments with easy fast switching between them.
Installation:
pip install virtualenvwrapper
(or pip install virtualenvwrapper-win
)
There are two related terms: projects and virtual environments.
Virtual environment
Environment that lives inside
$WORKON_HOME
(%WORKON_HOME%
)
Project
Directory with the code related to specific venv.
If venv was created with association to specific project dir (
-a <path/to/project/
) - after switching to it virtualenvwrapper will chande current directory to project.
virtualenvwrapper VENV commands:
Create new venv:
mkvirtualenv [mkvirtualenv-options] [virtualenv-options] VENV_NAME
mkvirtualenv options:
-a project_path
Associate existing path as project directory-i package
Install package in new environment-r requirements_file
VENV_NAME
(must be last) - the name of resulted venv
List of all venvs:
workon
RECOMMENDEDlsvirtualenv
Switch to another venv:
workon VENV_NAME
Deactivate current venv:
deactivate
virtualenvwrapper PROJECTS commands:
Associate project with current active venv:
setprojectdir <path/to/project/>
Create new project environment:
mkproject
If the environment variable
$PROJECT_HOME
(%PROJECT_HOME%
) is set, create a new project directory in PROJECT_HOME
Change into project directory:
cdproject
cd-
- change into the directory you were before runningcdproject
How to add existing virtualenv to virtualenvwrapper
Easiest way is to dump all packages from virtualenv
environment and recreate with virtualenvwrapper
:
Conda
Here I will cover main
conda
environment management commands only, full list:conda help
andconda env --help
Problems with Python installation on Windows
Mostly problems are related to incorrectly ENV variables. Some links with help to deal with this:
Can't install some module via pip
Sometimes to install additional package is impossible to do via pip because some pre-installed libraries (like MS Studio C++ or mysql/postgresql libraries) needed. So it's better to download and install precompiled packages from here:
Install of .whl package:
Problem with installing packages on Windows: Microsoft Visual C++ is missing
When you see this during installing some package with pip:
this means it is required to download and install Microsoft Visual C++ Build Tools:
go to Downloads page > "Tools for Visual Studio 2019"
Download and run installer for
Build Tools for Visual Studio 2019
Select checkbox for
Build Tools
and click "Install"
Best IDEs:
Microsoft Visual Studio Code <-- recommended
Python extension <-- required
JetBrains PyCharm (Community Edition is OK) or Fleet
Sublime Text (with lot of Python plugins)
Last updated