Everything Python Environments: Setting Up VSCode, Poetry, Pyenv & more
AWS Setup
Setting Up Your EC2 Dev Environment
- Open your git terminal
- clone into
monohelix
where "<USER>
" is your GitHub username:
git clone https://<USER>@github.com/workhelix/monohelix
- Enter your password into the prompt
- cd into
monohelix
- Setup Homebrew:
make linux-brew
- Setup Pyenv (takes about 10 minutes):
make linux-pyenv
- Setup Poetry:
make linux-poetry
Setup a New Project
- cd into
monohelix/projects
- Create a new project:
make project
- Fill out the terminal prompt
Working with Pyenv + Poetry
- Install a Python version you’d like (skip this step if it’s already installed):
e.g.pyenv install 3.9.1
- Assign python to your project:
pyenv local 3.9.1
- Couple pyenv and the poetry dependency manager:
make activate
Or manually:
poetry env use $(pyenv which python)
Local Setup
Setting Up Homebrew
Mac
To install Homebrew, enter the following lines of code into your terminal:
# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to path, replace <USER> with your username
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/<USER>/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
Next, install Xcode if you haven’t already installed it.
Linux
To install Homebrew, enter the following lines of code into your terminal:
# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to path
test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
source ~/.bash_profile
Setting Up Python with Pyenv
Mac
Before you install Python, install Pyenv which manages global- and project-level python versions without interfering with your system’s python version. Enter the following lines of code into your terminal:
# Install penv
brew install pyenv
# Add pyenv initializer to shell startup script (copy/paste all lines at once)
echo -e 'export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
eval "$(pyenv init -)"
' >> ~/.zshrc
# Reload your profile.
source ~/.zshrc
<aside>
💡 With `pyenv` installed, you can install any python version that you would like. Depending on which directory you’re in, `pyenv` will point to your specified python version.
</aside>
To install Python on your computer, enter the following line of code into your terminal:
# See all available python installations.
pyenv install --list
# Install some python versions.
pyenv install 3.9.1
pyenv install 3.7.6
# See all python installations that you have installed.
pyenv versions
# Set the default/global from one of the python versions.
pyenv global 3.9.1
# In the current directory, set the python version. This creates the file .python-version.
pyenv local 3.9.1
# To see which python is currently being used. Ensure it's above 3.7.X
pyenv versions
Linux
Before you install Python, install Pyenv which manages global- and project-level python versions without interfering with your system’s python version. Enter the following lines of code into your terminal:
# Install penv
brew install pyenv
# Add pyenv initializer to shell startup script (copy/paste all lines at once)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# Reload your profile.
source ~/.bash_profile
<aside>
💡 With `pyenv` installed, you can install any python version that you would like. Depending on which directory you’re in, `pyenv` will point to your specified python version.
</aside>
To install Python on your computer, enter the following line of code into your terminal:
# See all available python installations.
pyenv install --list
# Install some python versions.
pyenv install 3.9.1
pyenv install 3.7.6
# See all python installations that you have installed.
pyenv versions
# Set the default/global from one of the python versions.
pyenv global 3.9.1
# In the current directory, set the python version. This creates the file .python-version.
pyenv local 3.9.1
# To see which python is currently being used. Ensure it's above 3.7.X
pyenv versions
Setting Up Your Dependency Manager w/ Poetry
To install Poetry, enter the following line of code into your terminal:
# Install poetry via curl
curl -sSL https://install.python-poetry.org | python3 -
# Add poetry to your shell
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
# Update current shell session with changes made to ~/.zshrc
source ~/.zshrc
# Configure poetry to create virtual environments inside the project's root directory
poetry config virtualenvs.in-project true
To set up specific python versions in Poetry, see the following code examples:
# cd into the workspace where your repos will live locally, e.g.
cd Documents/Code
# Make the folder that will act as your repo
mkdir super-important-project
# cd into the new folder
cd super-important-project
# Specify the python version for the local directory using pyenv
pyenv local 3.9.1
# Initialize your new poetry project and fill out the respective info
poetry init
# Connect poetry to the pyenv environment
poetry env use $(pyenv which python)
# Add some libraries
poetry add pandas numpy scipy matplotlib scikit-learn jupyter ipykernel
# Specify some dev libraries
poetry add --dev black flake8
With the above commands, I have created a pyproject.toml
file as well as a poetry.lock
file. poetry
has installed the virtual environment for this project in super-important-project.venv
. The pyproject.toml
file looks as follows:
[tool.poetry]
name = "super-important-project"
version = "0.1.0"
description = ""
[tool.poetry.dependencies]
python = "3.10.1"
pandas = "^0.25.3"
numpy = "^1.18.0"
scipy = "^1.4.1"
matplotlib = "^3.1.2"
scikit-learn = "^0.22"
jupyter = "^1.0.0"
ipykernel = "^5.1.3"
[tool.poetry.dev-dependencies]
pytest = "^5.2"
black = "^19.10b0"
flake8 = "^3.7.9"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
To run your script, write the following in your terminal:
# Run the script within your virtual environment, using the 'run'-command.
poetry run python super-important-project/<EXAMPLE>.py
# Spawn a shell within your virtual environment.
poetry shell
# Try running the script again, after having spawned the shell within your virtual environment.
python super-important-project/<EXAMPLE>.py
To run jupyter
in your poetry
environment, see the following code:
# Make sure to run the command within your virtual environment. The 'poetry run' command ensures this
# Best practice is to use the same name for your kernel as the project.
poetry run ipython kernel install --user --name=super-important-project-demo
Setting Up VSCode
- Download VSCode
- Install our base-extensions