Skip to content

Instantly share code, notes, and snippets.

@fhpriamo
Forked from rubencaro/README.md
Created January 28, 2021 16:51
Show Gist options
  • Save fhpriamo/f22684a1cdc2f5550acb3d5f88b54e0a to your computer and use it in GitHub Desktop.
Save fhpriamo/f22684a1cdc2f5550acb3d5f88b54e0a to your computer and use it in GitHub Desktop.
Python installation guide

Python installation guide

These are my notes, not a generic solution. They are not meant to work anywhere outside my machines. Update version numbers to whatever are the current ones while you do this.

Install asdf and its python plugin, then install Python

asdf lives in https://github.com/asdf-vm/asdf

Follow its installation instructions, which at the moment of writing were:

cd
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.7.4

# For Ubuntu or other linux distros
echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc

On a new terminal, install Python plugin:

asdf plugin-add python

You may need some libraries on your system to ensure all parts you need of Python are compiled:

sudo dnf install sqlite-devel

Then install Python:

asdf install python 3.7.4

Set some Python verion as global. Usually you want this to be your own system's version:

asdf global python system

Routine processes

Here I use venv to isolate the project environment (virtualenv for Python2) and pip to track dependencies.

Setup a new project

Create a folder for the project and cd into it. Then fix your Python version asdf local python 3.7.4. Once you have that, you can create the project's environment python -m venv env. That will create an env folder that you should add to your .gitignore.

Python2

Create a folder for the project and cd into it. Then fix your Python version asdf local python 2.7.16. Then you must install virtualenv by running pip install virtualenv. Once you have that, you can create the project's environment virtualenv env. That will create an env folder that you should add to your .gitignore.

Activate the environment

You must always activate the environment before working on your code. It's simple, but vital. Activation is what ensures that the paths for your code dependencies are local to your project, and not shared with others. Just run source env/bin/activate. You should see a (env) prefix on your prompt showing it is activated.

Once activated, calls to python or pip will use your project's local binaries, and your code will import libraries present only in your local project's environment.

Some editors, such as VSCode, do this automatically for you when they detect an env folder in the root of a Python project. You must keep it in mind though, being aware of the actual Python environment you are in.

You can make you shell to do that automatically too by adding this to your .bashrc:

# auto activate virtualenv when entering its root path
function auto_activate_virtualenv {
  if [[ "$VIRTUAL_ENV" = "" && -f "$PWD/env/bin/activate" ]]; then
    source "$PWD/env/bin/activate"
  fi
}

function prompt_command {
  # other stuff
  # ...
  auto_activate_virtualenv
}

export PROMPT_COMMAND=prompt_command

Start working on an existing project

Provided the project was created following this guide, it is straightforward to start working on it. You should clone it, then cd into its folder, run asdf install to get the right Python version. Then python -m venv env to create a new environment and source env/bin/activate to activate it. Then run pip install -r requirements.txt to install all dependencies. If any dependencies include binaries, you may need to run asdf reshim python to ensure they are accessible from PATH.

Python2

You should clone it, then cd into its folder, run asdf install to get the right Python version. Then you must install virtualenv by running pip install virtualenv. Then virtualenv env to create a new environment and source env/bin/activate to activate it. Then run pip install -r requirements.txt to install all dependencies. If any dependencies include binaries, you may need to run asdf reshim python to ensure they are accessible from PATH.

Add new depencencies

Activate the enviroment. Then simply install your dependency with pip install yourdependency. Once it's installed, freeze project's dependencies by running pip freeze > requirements.txt. If any dependencies include binaries, you may need to run asdf reshim python to ensure they are accessible from PATH.

Remove a dependency

Activate the enviroment. Then simply uninstall your dependency with pip uninstall yourdependency. Once it's uninstalled, freeze project's dependencies by running pip freeze > requirements.txt.

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