Customize module search path (PYTHONPATH) via pipenv

前端 未结 2 760
慢半拍i
慢半拍i 2020-12-15 16:53

I have a Python project consisting of a Jupyter notebook, several scripts in a bin directory and modules in a src directory, with dependencies in a

2条回答
  •  温柔的废话
    2020-12-15 17:09

    I'm not sure there's a perfect solution for this, but in the interest of being explicit rather than implicit (PEP 20), I've decided to check in a file that needs to be sourced before running any script. This is one extra manual step but you can put this in a Makefile for instance.

    env.sh

    export PYTHONPATH=${PYTHONPATH}:${PWD}
    

    Makefile

    bar:
        source env.sh && pipenv run python scripts/bar.py
    .PHONY: migrate
    

    The solution is a bit similar to the approach Go takes with its GOPATH.

    I think the other solutions are not as good:

    • pipenv aims to solve dependencies, I could be wrong but I did not find anything related to the problem of the PYTHONPATH.
    • Linking folders won't scale really well if you start having other scripts folder.

提交回复
热议问题