Skip to content

Instantly share code, notes, and snippets.

@ynericli
Last active November 15, 2020 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ynericli/928ce2da9eadfa4031ca5657620eac31 to your computer and use it in GitHub Desktop.
Save ynericli/928ce2da9eadfa4031ca5657620eac31 to your computer and use it in GitHub Desktop.
The markdown file of medium blog: Virtual environment: What, Why, How?

Virtual environment: What, Why, How?

Mac is pre-installed with python 2. More python environments can be installed on your Mac in different directories with different versions of python and different packages installed. It is important to be aware of which python you are working with.

You can check the python you are currently working with by typing the following code in your terminal:

ericli@ERICYNLI-MB0 ~ % which python
/usr/bin/python

In the example, the python I'm currently working with is the pre-installed python 2. If the python isn't the one you want to use, then you might have a little problem here. You can resolve this problem by putting the directory of the desired python at the beginning of the path variable on your Mac. But this method is not recommended, since if you need to use different versions of python or python package for different projects, you will have to upgrade or downgrade your python or python packages consistently. In this situation, it is highly recommended to use virtual environments.

Virtual environment

A virtual environment is a directory that contains a specific version of python and its related packages. The virtual environments are perfectly isolated, so you may have as many virtual environments as you want on your Mac, and always be crystal clear about which one you are working with.

I highly recommend everyone to install Anaconda before they proceed to the next step for its simplicity. After you install Anaconda (https://docs.anaconda.com/anaconda/install/), you need to add the directory of the Anaconda bin to the paths. To add the directory, simply input the following code and type in your Mac password:

ericli@ERICYNLI-MB0 ~ % sudo vim /etc/paths
Password:

For those of you that are interested, "sudo" means "superuser do", it grants the current user administrator privilege. After you input your password and hit enter, you will see something like this:

/usr/local/bin
/usr/bin
/bin
/usr/sbin 
/sbin

In the file, create a new line, and add the Anaconda bin directory to the file.

/usr/local/bin
/usr/bin
/bin
/usr/sbin 
/sbin
/opt/anaconda3/bin

Once the file is saved, you can use the "conda" command in your terminal.

Vim command hints:
"o": open a new line under the cursor
"O": open a new line above the cursor
":wq": save and quit
":q": quit
":q!": force to quit(when there are unwanted changes)

With conda command, you can create virtual environments with your desired python versions and python packages. By activating the virtual environment, your path on your Mac is modified - the directory of your virtual environment is placed at the beginning of the path file, so any command you input will be searched and executed once found in the virtual environment.

To create a virtual environment with python version 3.8, and activate it:

ericli@ERICYNLI-MB0 ~ % conda create --name py38 python=3.8
ericli@ERICYNLI-MB0 ~ % source activate py38

To check the virtual environment list and current environment (marked with a star):

(py38) ericli@ERICYNLI-MB0 ~ % conda env list
# conda environments:
#
base                     /opt/anaconda3
py38                  *  /opt/anaconda3/envs/py38

To check what packages are installed in the current environment, and install other packages:

(py38) ericli@ERICYNLI-MB0 ~ % conda list
(py38) ericli@ERICYNLI-MB0 ~ % conda install PACKAGENAME(pandas/matplotlib/jupyter/jupyterlab/etc.)

After activating the new environment, you can find the python you are using is the one in your virtual environment.

(py38) ericli@ERICYNLI-MB0 ~ % which python
/opt/anaconda3/envs/py38/bin/python

Now your new virtual environment is all set. You can begin to work with this environment by input "python" in the terminal, or "jupyter lab" if you installed jupyterlab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment