Creating and installing my own packages

Asked

Viewed 51 times

1

Hello. I’ve only been learning Python for a short time. I’m now trying to create my own packages. This helps organize the code.

But I had a question: would anyone know how to install my packages so that they are available for all my projects?

An idea would be to copy them manually to the directory of each project, but besides not being anything elegant, I’ll have to copy them again when there are changes in the packages. Certainly not a good solution.

So, is it possible to use the Pip to install my packages in my projects and update them when necessary?

Could someone clarify on this subject? Is there any other procedure more suitable to do this management of my own packages?

Thanks for the help.

PS.: using Python 3.9.1, Pycharm and Win10.

1 answer

2


if you write a setup.py for your projects, even without putting the packages in public Pypi, or in a private repository, you can simply activate the virtualenv of another project, and type pip install <caminho_do_setup_py_do_subprojeto> . If you plan to evolve the subprojects over time and want larger projects to always see the latest code, put the "e" option in the "Pip": pip install -e <caminho_do_setup_py_do_subprojeto>

So the secret is just: always use virtualenvs for your projects (or equivalent like pyenv, or the similar ones that are maintained by Ides themselves) - and create a minimal setup.py for your projects. With time you evolve these setup.py to have the necessary metadata to publish your packages in the public repository if you want.

Here are some examples of the package structure and what to have in the archive setup.py: https://python-packaging.readthedocs.io/en/latest/minimal.html

  • Great explanation. I have a question about "Pip install -e": with this option it is unnecessary to use "Pip install -U" to update the installation of a package?

  • 1

    The pip install -e makes Python use the project of the folder it is currently in. Without the -e it copies the files to the folder lib/site-packages of the virtual-env, and now uses the copy made at that moment. As with -e it uses the files you are editing, you do not need to be using the "-U" all the time, but when you upload the version, change the prerequisite version, or change the setup.py, then you need the -U yes, which may be along with the -e: pip install -U -e .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.