Skip to content

Instantly share code, notes, and snippets.

@inadarei
Last active May 15, 2020 12:17
Show Gist options
  • Save inadarei/f15449b78d53feaae415a0bcc8da3fc9 to your computer and use it in GitHub Desktop.
Save inadarei/f15449b78d53feaae415a0bcc8da3fc9 to your computer and use it in GitHub Desktop.
Setting up Python3 Environment on MacOS High Sierra+ and/or Ubuntu

Installing Python 3.x and Pip3 on Mac

# Install Homebrew itself
> brew install python3
> python -V
Python 2.7.10
> python3 -V
Python 3.7.0
# Eureka!

Installing Python 3.x and Pip3 on Ubuntu

> sudo apt update
> sudo apt-get install python3
> python3 --version
> sudo apt install software-properties-common
> sudo apt-get install python3-pip

Installing VirtualEnv on Mac or Ubuntu

> sudo -H pip3 install --upgrade pip setuptools wheel
> sudo -H pip3 install virtualenv
# Create new virtualenv, Python3-way:
> python3 -m venv p3-proj-macondo
# The last command creates a folder named `p3-proj-macondo` in the current path. To activate corresponding environment run:
> source p3-proj-macondo/bin/activate
# After which:
> python -V
Python 3.7.0
# To exit virtual environment:
> deactivate

Important: Comfort Mode with "workon"

In addition to above stuff:

> sudo -H pip3 install virtualenvwrapper
> mkdir ~/.virtualenvs

Add following to your ~/.bash_profile file:

if [ -f "/usr/local/bin/virtualenvwrapper.sh" ]; then
  export WORKON_HOME=~/.virtualenvs
  export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
  export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv

  source /usr/local/bin/virtualenvwrapper.sh
fi

Please note that on Ubuntu Python3 is under /usr/bin/python3, so you need to change line 3 accordingly.

then execute:

> source .bash_profile
# Create new virtual environment using Python 3:
> mkvirtualenv --python=/usr/local/bin/python3 p3-default
# Creation of new environment also activates it. To deactivate current Python environment (any), run:
> deactivate
# and to activate any one ofthe environment created this way (under ~/.virtualenvs) run:
> workon p3-default
# check:
> python -V
Python 3.7.0
# install pylint in the environment:
> python -m pip install -U pylint
# install other essenstial tools:
> pip install wheel twine setuptools --upgrade

Bonus: VS Code integration

If you have the python extension in VS Code installed, following setting can make working in VS Code with python3 a breeze:

"python.venvPath" : "~/.virtualenvs",

Make sure you don't have "python.pythonPath" setting set, and to switch between environments click on the Python ... label in the bottom, blue status bar of VS Code, in the left bottom corner.

ATTENTION: typically you do need to restart VS Code after adding any new virtualenvironment since it seems to only scan those on startup (?)

Virtual Environments in Current Folder Using Venv

create:

python3 -m venv venv

To activate the virtual environment On Mac and Linux:

source venv/bin/activate

on Windows:

call venv/scripts/activate.bat

We also need autoenv which allows us to set commands that will run every time we cd into our directory. In order to use it, we will need to install it globally. First, exit out of your virtual environment in the terminal, install autoenv, then and add a .env file:

> deactivate
> sudo apt install python3-pip
> pip3 install autoenv
> touch .env

Next, in the .env file, add the following:

source env/bin/activate
export APP_SETTINGS="config.DevelopmentConfig"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment