Skip to main content

Virtual Environment Python

https://medium.freecodecamp.org/why-you-need-python-environments-and-how-to-manage-them-with-conda-85f155f4353c


Why use virtual environments?

- Python applications from GitHub, Kaggle or other sources  may need other versions of Python/packages than the ones you have been currently using.

- Due to a change in one package, other package suddenly stopped working. So set up a new environment, that contains the Python version and the packages that are compatible.

- Make sure your application works on other's computer.

- So develop or use applications with different Python or package version requirements, you need to set up different environments.

Setting up Virtual Environment - Two Ways
1. PIP  with virtualenv
2. Conda

Conda Preferred:
- Clear Structure : Easy to understand its directory structure
- Transparent File Management: It doesn’t install files outside its directory
- Flexibility: Contains a lot of packages (PIP packages are also installable into Conda environments)
- Multipurpose: Not only for managing Python  - can for R as well

First download Anaconda / Miniconda
Miniconda is lightweight with less packages - I don't want to take risk, I might need packages so I installed Anaconda

Either of them will set up:
- the Conda (the package & environment management system) and
- the so-called “root environment” - the one that is created during the installation process and it’s activated by default.

Install x64 Package and Python version 3 - Tensorflow doesn't support 32 bit and Python 2

You can begin with Python 3, if you need Python 2 you can use virtual environment as well.
- Like once you needed Python 2 beacuse a package/fules from github did not run on Python 3, although you can convert a py file from 3 to 2 using sth like 3to2.py in Anconda, but sometimes doing that doesn't work either.

Go with Version 3 of Python

- Also since the 3.x is newer, this should be your default choice.
- Leave the installation path as it is.

So on installation Anaconda creates -
Conda - the package and environment management tool,
then creates a root env with your selected python version and some packages.

Beyond the root, you can create as many envs as you want with any python ver.

Conda system is installed in a single directory 
like:
home/xyz/anaconda3
inside the folder there are two main folders - /pkgs and /envs
- \pkgs (it contains the cached packages in compressed and uncompressed formats)
- \envs (it contains the environments — except for the root environment — in separate subdirectories)

Adding a new environment
conda create --name mynewenv python=3.4

Activating env
activate mynewenv [Windows]
source activate mynewenv [Linux/Mac]
deactivate
source deactivate
-in Windows it is a good practice to deactivate an environment before activating another.

conda env list

conda --version

conda info
Gives:
Conda version,
platform (operating system and bit count — 32- or 64-bit),
Python version,
environment directories,

Changing an environment’s Python version
Python is also a package
list out the available Python versions:
conda search -f python

conda install python=3.4.2

conda update python

Recommended using Conda as your package and environment manager (and not PIP). But, PIP packages are also installable into Conda environments

Therefore, if a package is unavailable through the Conda channels, you can try to install it using pip
pip install lightgbm


Use conda instead of pip

Channels are the locations of the repositories (storages) where Conda looks for packages.