Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
Created July 23, 2021 15:53
Show Gist options
  • Save liquidgenius/c6173b4f55da24b7e74e10ca0217de70 to your computer and use it in GitHub Desktop.
Save liquidgenius/c6173b4f55da24b7e74e10ca0217de70 to your computer and use it in GitHub Desktop.
Create a Python project utilizing pyenv and pipenv. Easily open the project with Pycharm with the interpreter located in-project.
#! /bin/bash
# License: MIT; https://opensource.org/licenses/MIT
# Version: 0.0.1
# Maintainer: https://gist.github.com/liquidgenius
#*******************************************************************************
# Pycharmer; A Pycharm Python project creator
#*******************************************************************************
# Purpose:
# Create a Python project utilizing pyenv and pipenv. Easily open the project with
# Pycharm with the interpreter located in-project.
# Usage:
# sh pycharmer.sh --name test -pyver 3.8.7 --mods "pandas numpy"
# sh pycharmer.sh --mods "pandas numpy"
# Assert for required apps
has_pyenv="$(which pyenv)"
if [ "$has_pyenv" == "" ] ; then
echo "\nPyenv not found\nPlease install pyenv"
echo "\nPyenv: https://github.com/pyenv/pyenv\n\
Install: https://github.com/pyenv/pyenv-installer"
echo "\nQuitting\n"
exit 1 ; fi
has_pipenv="$(which pipenv)"
if [ "$has_pipenv" == "" ] ; then
echo "\nPipenv not found\nPlease install pipenv"
echo "\nPipenv: https://pipenv.pypa.io/en/latest/"
echo "\nQuitting\n"
exit 1 ; fi
# Arguements
name=${name:-""}
pyver=${pyver:-""}
src=${src:-true}
mods=${mods:-""}
# Variables
cwd=$(pwd)
verbosity=-1
proj_venv=1
divider="**********************************************************************"
# Catch required fields if empty
if [ $name = ""] ; then
echo "Project name?"
read name ; fi
if [ $pyver = ""] ; then
pyenv versions
echo "Use which python?"
read pyver ; fi
# Parse provided arguements
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare $param="$2" ; fi
shift
done
# Install path based on src flag
if [ $src = true ] ; then
projpath="$cwd/$name/src"
else
projpath="$cwd/$name" ; fi
echo "\n\n$divider"
echo "Creating Project: $name"
echo "$divider"
echo "\nRequires pyenv, pipenv installed. PyCharm compatible. Simply open the \
created \ndirectory and assign the in-project .venv folder as the interpreter."
if [ $mods != "" ] ; then
echo "\nThe following modules will be installed:\n$mods" ; fi
# Setup directories
if [ $src = true ] ; then
echo "\nUse src subfolder"
echo "\nMaking directory: $cwd/$name\nMaking directory: $projpath"
mkdir "$cwd/$name" "$projpath"
echo "Changing to: $cwd/$name"
cd "$cwd/$name"
echo "Pyenv setting local (and subfolder's) python: $pyver"
pyenv local $pyver
echo "Changing to: $projpath"
cd "$projpath"
else
echo "\nUse root project folder (no src subfolder)"
echo "\nMaking directory: $cwd/$name"
mkdir "$cwd/$name"
echo "Changing to: $projpath"
cd "$projpath"
echo "Pyenv setting local (and subfolder's) python: $pyver\n"
pyenv local $pyver ; fi
# Create Pipenv environment
PIPENV_VERBOSITY=$verbosity PIPENV_VENV_IN_PROJECT=$proj_venv \
pipenv install --python $pyver
# Install required modules
pipenv install $mods
# Move key files and interpreter to project root if using src
if [ $src = true ] ; then
file1=".venv"
file2=".gitignore"
mv "$file1" "$cwd/$name/"
mv "$file2" "$cwd/$name/"
mv Pipfile "$cwd/$name/"
mv Pipfile.lock "$cwd/$name/" ; fi
# Final instructions
echo "\n$divider"
echo "Removing scaffolding, $name ready"
echo "$divider"
echo "\nProject: $cwd/$name"
echo "Interpreter: $cwd/$name/.venv/bin/python"
echo "Delete project: rm -fr $cwd/$name"
printf "\ncd to the project directory, where you may run commands:\n\
pipenv install numpy\n\
pipenv shell\n\
pipenv run main.py"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment