# Getting started with Python

Python is a very popular programming language these days. It is commonly used to develop websites, software, task automation, data analysis and data visualization.

In this blog, we will cover how to set up a development environment for Python.

# Installing Python on MacOS

There are multiple ways to install python these days. However, we will use [**Homebrew**](https://brew.sh/)**,** a package manager for MacOS to install Python on MacOS.

## **Install Homebrew**

Paste the following command in the MacOS terminal to install Homebrew

```bash
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

## **Install pyenv**

The best way to manage python versions on Mac is by installing a simple python version management tool which can be installed on many operating systems mentioned [here](https://github.com/pyenv/pyenv/wiki).

To install it on MacOS using Homebrew use the following command:

```bash
$ brew install pyenv
```

You want **pyenv** to run every time you open your prompt, so include the following in your configuration files (by default on MacOS, this is **.bash\_profile** in your home directory):

Use the following command to edit the bash profile on MacOS

```bash
$ vi ~/.bash_profile
```

By adding these lines, every new terminal will initiate **pyenv** to manage the **PATH** environment variable in your terminal and insert the version of Python you want to run

```bash
$ cd ~/
$ echo 'eval "$(pyenv init -)"' >> .bash_profile
```

Before installing your preferred version of Python, you'll want to install a couple of helpful tools:

```bash
$  brew install zlib sqlite
```

The [**zlib**](https://zlib.net/) compression algorithm and the [**SQLite**](https://www.sqlite.org/index.html) database are dependencies for **pyenv** and often [cause build problems](https://github.com/pyenv/pyenv/wiki/common-build-problems#build-failed-error-the-python-zlib-extension-was-not-compiled-missing-the-zlib) when not configured correctly.

Add these exports to your current terminal window to ensure the installation completes:

```bash
$ export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/sqlite/lib"
$ export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/sqlite/include"
```

Now after the above pre-requisite are complete, it's time to install a preferred version of Python.

## **Install python**

We will install the latest version available which is 3.10.10 as of writing.

```bash
$ pyenv install 3.10.10
```

## Add Virtual Environment

Next, make this version as default version to be used globally:

```bash
$ pyenv global 3.10.10
# Be sure to keep the $() syntax in this command so it can evaluate
$ $(pyenv which python3) -m pip install virtualenvwrapper
```

Open your **.bash\_profile** again and add the following to be sure it works each time you open a new terminal:

```bash
# Virtual environment directory
$ echo 'export WORK_HOME=~/.virtualenvs' >> .bash_profile
# Make a virtual environment directory, If one does not already exist
$ echo 'mkdir -p $WORK_HOME' >> .bash_profile
# Activate the new virtual environment by calling this script
$ echo '. ~/.pyenv/versions/3.10.10/bin/virtualenvwrapper.sh' >> .bash_profile
```

Close the terminal and open a new one (or run **exec /bin/bash -l** to refresh the current terminal session), and you'll see **virtualenvwrapper** initializing the environment:

```bash
exec /bin/bash -l
```

![Virtual Environment Setup - Installing virtualenvwrapper](https://cdn.hashnode.com/res/hashnode/image/upload/v1680493353967/7f4e5edc-25b1-435b-ba02-b8e1f10b314c.png align="center")

It will show output like the above to set up a virtual environment. Now we can set up a test environment.

```bash
mkvirtualenv test1
```

The above command will create a test1 environment and you can also switch between multiple virtual environments.

![WORK_HOME directly Setup on Terminal](https://cdn.hashnode.com/res/hashnode/image/upload/v1680494087874/23170a50-2bcb-4463-82c4-8ea0d6a44097.png align="center")

To exit from the current virtual environment, use **deactivate** command and remove any existing virtual environment use **rmvirtualenv** command.

To work on an existing virtual environment use **workon** command to switch to that virtual environment.

```bash
$ workon venv_name
```

That's it, the development environment setup is complete. In case, you would like to use any specific python version in another python virtual environment. Use the following command:

```bash
mkvirtualenv -p python3.9 test3
```

Thank you. See you in the next blog.

### **References:**

* [https://en.wikipedia.org/wiki/Python\_(programming\_language)](https://en.wikipedia.org/wiki/Python_(programming_language))
    
* [https://opensource.com/article/19/5/python-3-default-mac#what-to-do](https://opensource.com/article/19/5/python-3-default-mac#what-to-do)
    
* [https://opensource.com/article/19/6/python-virtual-environments-mac](https://opensource.com/article/19/6/python-virtual-environments-mac)
