Customize module search path (PYTHONPATH) via pipenv

前端 未结 2 762
慢半拍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:27

    (Came here for an answer, ended up giving one instead)

    I have a similar project folder structure, so I had the same problem.
    Thanks to your tip, my solution was to add an file .env at the same level as the Pipfile with the following content:

    $ cat .env
    PYTHONPATH=${PYTHONPATH}:src
    

    Now, launching my app with something like

    $ pipenv run python -m package.subpackage.app
    

    seems to work ok from inside my project's folder and also from it's subfolders.

    Side note(although is not a good/clean way to do things):
    for your ModuleNotFoundError: No module named 'src' problem ... the "problem" is that the src (folder) is not a package, in order to fix that you could easily add an (empty) __init__.py file inside the src folder, thous making it a "package"; which in turn would make import src.baz possible.

    (Later edit)
    Actually this adds a record /${PYTHONPATH} in sys.path, which is useless, so the correct content of the .env file should be only PYTHONPATH=src.

提交回复
热议问题