dib-lab / farm-notes

notes on the farm cluster
16 stars 9 forks source link

installing python projects from git/github using `pip` #46

Open ctb opened 2 years ago

ctb commented 2 years ago

By default, the Python package installer pip installs the latest version of packages from pypi.org, the Python package index. But what if you want to install an unreleased version (e.g. the latest version of sourmash), or a specific branch? Read on!

running pip inside a conda environment

I tend to do all of the below commands in a conda environment that contains python; then the installation is sandboxed in that conda environment. You can do the same thing with R - create and activate a conda environment containing r-base, then use standard R installation mechanisms to install. See this link for a conda environment that includes ggplot2 and tidyverse!

See this conda tutorial for some background and command lines for both Python and R.

Additional / extended details below!

installing directly from a branch

if you want to install a python project directly from a branch in a git repo, see solutions here:

https://stackoverflow.com/questions/20101834/pip-install-from-git-repo-branch

installing from a cloned git repo

you can also clone the repo and install directly with pip:

git clone https://github.com/dib-lab/genome-grist
cd genome-grist
python -m pip install .

this will install genome-grist from that directory. However, you will need to re-run pip install if you change the directory (update the branch, or whatever) and want the installation to reflect the new branch.

Is there a way around that? Yes! Read on!

installing from a cloned repo in 'editable' mode

I prefer installing projects in "editable" or "developer" mode, like so:

git clone https://github.com/dib-lab/genome-grist
cd genome-grist
python -m pip install -e .

This will tie the installation directly to the directory, so if you switch branches or edit files, the installed version will automatically reflect the changes.

(This is how I develop and test sourmash, charcoal, genome-grist, and spacegraphcats.)

camillescott commented 2 years ago

Best practice for pip it to use python -m pip install [ARGS] rather than calling pip directly; this way, if pip is not installed in the virtual environment, it will fail directly, rather than potentially calling an externally installed pip and failing in unique and spectacular ways.

ctb commented 2 years ago

Best practice for pip it to use python -m pip install [ARGS] rather than calling pip directly

fixed!