Where should I put my own python module so that it can be imported

后端 未结 6 1819
心在旅途
心在旅途 2020-11-29 16:53

I have my own package in python and I am using it very often. what is the most elegant or conventional directory where i should put my package so it is going to be imported

6条回答
  •  感动是毒
    2020-11-29 17:36

    This is something that works for me (I have to frequently create python packages that are uploaded to a private pip repository). elaborating on the comment by @joran on the question.

    1. create a "build directory" which is used as a workspace to create packages. any directory of your choice will do
    2. Copy your python package dir there, and create a setup.py file. this should help in creating the setup.py correctly.
    3. create a virtualenv for the project you are working on. virtualenvs have a bunch of other benefits, I am not going into the details here.
    4. create a local dist package python setup.py sdist --format=tar. the package created should ideally be in the dist folder.
    5. Install the package on your virtualenv (after activating it). pip install .tar

    you can use pip install --force-reinstall if you need to play around with the libraries more and re-create the dist packages.

    I've found that this method works great for me. If you do not need to package the modules for use of other systems instead of just your local, this method might be an overkill

    Happy hacking.

提交回复
热议问题