Skip to content

Instantly share code, notes, and snippets.

@mattsizzle
Last active March 6, 2020 21:39
Show Gist options
  • Save mattsizzle/fb2d359194804936beb7efc82c801e7a to your computer and use it in GitHub Desktop.
Save mattsizzle/fb2d359194804936beb7efc82c801e7a to your computer and use it in GitHub Desktop.
How to Bootstrap Modern Python Application

Bootstrap Modern Python Application

Install Dependencies

The following guides will help you get Python and Pip

Install Python

This is a great overview of Pipenv and Virtual Environments

Pipenv & Virtual Environments

Installing Pipenv

Install Pipenv with pip

$ pip install pipenv

Creating a new Python Project with Pipenv

Create a project root directory

mkdir python-example && cd python-example

Create a virtual environment in the directory:

pipenv --python 3.5

Install project level dependencies as needed.

Pipenv will automatically add the dependency to the [packages] section in the Pipfile. In addition, Pipenv creates a file named Pipfile.lock, which contains a hash of the exact versions used. This ensures that when other developers install the dependencies for this project, they will all end up with exactly the same versions.

This will install the pytest library as a dependency

pipenv install --dev 'pytest>=3.*'

Working In and Verifying your Virtual Environment

Now the virtual environment has been created which will be used to run your Python code isolated from other python environments and with it's own modules and interpeter. You will still need to activate this environment in any shell you are working in.

From within the directory containing the Pipfile, start a shell in the new environment:

pipenv shell

This is similar to running source env/bin/activate with virtualenv.

Exit the shell (similar to deactivating an environment with virtualenv):

exit

Locate the binary for the virtual environment:

pipenv --venv

This is handy for locating the correct settings for development in pyCharm or Intellij IDE(s)

Various Usage Examples:

Create a new project using Python 3.7, specifically:

$ pipenv --python 3.7

Remove project virtualenv (inferred from current directory):

$ pipenv --rm

Install all dependencies for a project (including dev):

$ pipenv install --dev

Create a lockfile containing pre-releases:

$ pipenv lock --pre

Show a graph of your installed dependencies:

$ pipenv graph

Check your installed dependencies for security vulnerabilities:

$ pipenv check

Install a local setup.py into your virtual environment/Pipfile:

$ pipenv install -e .

Use a lower-level pip command:

$ pipenv run pip freeze

Further Reading

Basic Usage of Pipenv

Advanced Usage of Pipenv

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