Skip to content

Instantly share code, notes, and snippets.

@wcarhart
Last active February 6, 2020 23:50
Show Gist options
  • Save wcarhart/23d0dc32ad4130254b92cbd78ed166bc to your computer and use it in GitHub Desktop.
Save wcarhart/23d0dc32ad4130254b92cbd78ed166bc to your computer and use it in GitHub Desktop.
How to install a Python package with setup.py + tox
#!/usr/bin/env bash
ENV_PATH="$(dirname "$(dirname "$(which pip)")")"
SYSTEM_VIRTUALENV="$(which -a virtualenv|tail -1)"
BAD_ENV_PATHS="/usr/local"
echo "Ensure the root of the broken virtualenv:"
echo " $ENV_PATH"
if [[ -z "$ENV_PATH" ]] || [[ "$ENV_PATH" = *"$BAD_ENV_PATHS"* ]]; then
echo "The root path above doesn't seems to be a valid one."
echo "Please make sure you ACTIVATED the broken virtualenv."
echo "‼️ Exiting for your safety... (thanks @laymonk for reporting this)"
exit 1
fi
read -p "‼️ Press Enter if you are not sure (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "♻️ Removing old symbolic links......"
find "$ENV_PATH" -type l -delete -print
echo "💫 Creating new symbolic links......"
$SYSTEM_VIRTUALENV "$ENV_PATH"
echo "🎉 Done!"
fi
# change into project directory
cd project_name
# build the package
python3 setup.py build
# activate virtualenv, or make one if doesn't exist
virtualenv -p `which python3` ~/venv
source ~/venv/bin/activate
# install tox, if it is not already installed
pip3 install tox
# run tox
tox
# if tox reports errors (i.e., with flake8), fix and redo previous step
# if tox exits cleanly, install
./setup.py install
# verify installation by using `which command_name`
# should point to ~/venv/bin/project_name
which command_name
# if this error appears (or similar):
# dyld: Library not loaded: @executable_path/../.Python
# Referenced from: ~/venv/bin/python3
# Reason: image not found
# Abort trap: 6
#
# fix: https://gist.github.com/tevino/1a557a0c200d61d4e4fb
# if the above Gist ever gets deleted, its contents are replicated in the file fix_virtualenv.sh in this Gist
# run the following:
sh <(curl -sL https://gist.github.com/tevino/1a557a0c200d61d4e4fb/raw/e5932f9923e57dcb41db32fe5bed9acc6d584be4/fix_virtualenv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment