WRAP-UP:
Python Environment

TOC

venv

pipenv

conda env


In many cases, different applicaations require different version of python and package version. The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

venv

[TOP]

venv is a lightweight built-in module in python3.3 or higher.

Pros:
・Build-in module no need package installation.

Cons:
・Cannot create environment in different python version.
・Need to activate from the path location.

python -m venv <env_name>
・To create new virtual environment in current path.

<env_name>\Scripts\activate.bat
・To activate virtual environment.

deactivate
・Deactivate environment (remove directory).

rmdir <env_name> /s
・Delete Environment.



pipenv

[TOP]

Pipenv is quite a new tool to help manage environment and packages similar to npm, yarn, etc. User no longer need to use pip and virtualenv separately.

Managing a requirements.txt file can be problematic, so Pipenv uses Pipfile and Pipfile.lock to separate abstract dependency declarations from the last tested combination.

Hashes are used everywhere, always. Security. Automatically expose security vulnerabilities.

Pros:
・Combination of pip and virtualenv.
・No need to mannually create virtual environment.
・Security check
・It can create variable in .env.

Cons:
・Need to install package.

pip install pipenv
・To install pipenv.

pipenv install <package>
pipenv install requests
・To install package via pipenv.

pipenv install -r ./requirements.txt
・To install package via pipenv from requirements.txt.

pipenv install pytest --dev
・To pytest only for dev environment.

pipenv uninstall <package>
pipenv uninstall request
・To uninstall package from pipenv.

pipenv shell
・To activate pipenv environment.

exit
・To deactivate pipenv environment.

pipenv run python
・To run python in pipenv environment.

pipenv run python script.py
・To run python script in pipenv environment.

pipenv lock -r > requirements.txt
・To create lock for requirements.txt.

pipenv --python 3.6
・To change python version in pipenv.

pipenv --rm
・To remove pipenv environment. (Pipfile is not deleted)

pipenv install
・To create pipenv from Pipfile.

pipenv check
・To run security check.

pipenv graph
・To show dependency tree.

pipenv lock
・To update pipfile.lock.

pipenv install --ignore-pipfile
・To install from pipfile.lock.



conda env

[TOP]

With conda, user can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them.

Pros:
・User can control both python version and package version.
・User can create environment yml file.

Cons:
・It requires to install anaconda.

conda create -n <env_name>
・To create new virtual environment.

conda create -n <env_name> python=3.6
・To create new virtual environment with specific python version.

conda create -n <env_name> python=3.6 numpy==1.17.4
・To create new virtual environment with specific python version and package.

conda env list
conda info --envs
・To list all environments.

conda activate <env_name>
・To activate virtual environment.

conda deactivate
・Deactivate environment (remove directory).

conda remove -n <env_name> --all
・Delete Environment.

conda env export > environment.yml
・Exporting the environment.yml file.

conda env create -f environment.yml
・Create environment with yml file.

conda -h
・Conda help



Appendix

Python command line

Code Explaination
cls Clear screen
pip list To show all current packages including pip and setuptools.
pip freeze To show all current packages in requirements format.
pip freeze > requirements.txt To create requirements file.
where python To get path of current python command.
pip install <package name>
Ex:
pip install nampy
To install package in lastest version.
pip install <package name>==<version>
Ex:
pip install numpy==1.17.4
To install python package with specific version.
pip install -r requirements.txt
To install from requirements.txt.
python -m pip install --upgrade pip
To update pip.
python --version
python -V
To check python version.
python Open python
exit() Exit python


Python Snippet

Print python version:
import sys
print('Python environment : ' + str(sys.version))
Python environment : 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]

Print package version:
import tensorflow as tf
print('Tensorflow version : ' + str(tf.__version__))
Tensorflow version : 2.3.0

Print current working directory:
import os
print('Working Directory : ' + str(os.getcwd()))
Working Directory : C:\Users\jingw\PycharmProjects\tfexam-t1

To add working directory relative to the current running script:
import os, sys
sys.path.append(os.getcwd())

Print Execution time
from datetime import datetime
start_time = datetime.now()
print('Execution time:' + str(datetime.now() - start_time))
Execution time:0:00:00