TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to make a Python package in 2021

396 pointsby giladabout 4 years ago

24 comments

zomglingsabout 4 years ago
The number of extra tools used in this article boggles my mind.<p>Are you writing a simple library? Create a setup.py. Copy paste an existing setup.py and modify it to suit your purposes. Now you have a working, pip installable python package.<p>Want to publish to PyPI? Use twine. It&#x27;s standard and it&#x27;s simple.<p>You don&#x27;t need complicated tooling for simple projects.
评论 #26737570 未加载
评论 #26735506 未加载
评论 #26736484 未加载
评论 #26734623 未加载
评论 #26736636 未加载
评论 #26734535 未加载
评论 #26736815 未加载
评论 #26736312 未加载
评论 #26737563 未加载
评论 #26734703 未加载
neologabout 4 years ago
Just use Poetry [1]. It&#x27;s popular and works well.<p>[1] <a href="https:&#x2F;&#x2F;python-poetry.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;python-poetry.org&#x2F;</a>
评论 #26734724 未加载
评论 #26735065 未加载
评论 #26736828 未加载
评论 #26734433 未加载
评论 #26734425 未加载
评论 #26734713 未加载
评论 #26758653 未加载
评论 #26734887 未加载
评论 #26734748 未加载
评论 #26734991 未加载
mikepurvisabout 4 years ago
I hadn&#x27;t heard of flit, it does seem like it&#x27;s not <i>brand new</i> on the scene, however it is primarily a single author, so expect a tool which is opinionated and for which the opinions may not necessarily reflect a broad consensus:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;takluyver&#x2F;flit&#x2F;graphs&#x2F;contributors" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;takluyver&#x2F;flit&#x2F;graphs&#x2F;contributors</a><p>With a title like this, I&#x27;d be expecting to see an article describing the latest tools and recommendations from the PyPA, which are here:<p><a href="https:&#x2F;&#x2F;packaging.python.org&#x2F;tutorials&#x2F;packaging-projects&#x2F;" rel="nofollow">https:&#x2F;&#x2F;packaging.python.org&#x2F;tutorials&#x2F;packaging-projects&#x2F;</a><p>(In short, it&#x27;s setup.cfg + pyproject.toml, `python3 -m build` to build, twine to upload.)
评论 #26734540 未加载
评论 #26736301 未加载
评论 #26734382 未加载
ThePhysicistabout 4 years ago
The author mixes different things like linting and testing into the packaging process, which (IMHO) are not really part of making a package. The process is really much easier than this article makes it seem:<p>- Write a simple setup.py file.<p>- Generate a source or binary release by e.g. running &quot;python setup.py sdist&quot;<p>- You&#x27;re done!<p>Adding a setup.py file is already enough to make your library pip-installable, so you could argue that this is a package already. The files generated by &quot;setup.py&quot; can also be pip-imported, so they are also packages. Now you might want to upload your package to a repository, for which there are different tools available. The simplest one being twine. Again, you just install it and run &quot;twine upload -r dist&#x2F;*&quot; and your packages get uploaded to PyPi (it will ask for a username and password). So why complicate things?
评论 #26736075 未加载
icyabout 4 years ago
Honestly, I don&#x27;t even bother &quot;packaging&quot; Python tools anymore. Just put it in all in a git repo, and pip can install using<p><pre><code> pip install git+https:&#x2F;&#x2F;myg.it&#x2F;repo.git</code></pre>
评论 #26735642 未加载
评论 #26734919 未加载
评论 #26735377 未加载
评论 #26737006 未加载
perrygeoabout 4 years ago
Why not to make a Python package in 2021.<p>Even as a long time Python user, the packaging ecosystem feels fragmented and error-prone at best. Honestly, it sours the experience of writing Python code knowing you might eventually need to make it work on another computer.
评论 #26737367 未加载
评论 #26737015 未加载
评论 #26735508 未加载
评论 #26734752 未加载
shadycuzabout 4 years ago
After reading this guide I would still recommend people to use this guide.<p><a href="https:&#x2F;&#x2F;cjolowicz.github.io&#x2F;posts&#x2F;hypermodern-python-01-setup&#x2F;" rel="nofollow">https:&#x2F;&#x2F;cjolowicz.github.io&#x2F;posts&#x2F;hypermodern-python-01-setu...</a><p>It&#x27;s great for beginners and experts. As a long time python veteran it completely changed how I work with python for the better. It is lengthy with lots of optional steps. Just skip the ones you don&#x27;t find relevant.
rtpgabout 4 years ago
The recommendation to set up a Makefile on top of tox is a bit odd to be honest. Tox basically &quot;just works&quot;, and you can do things like pass stuff to `pytest` by setting up `{posargs}` in the tox config (see [0])<p>I do feel like tox gets a bad rap despite having a complete feature set. I think a part of it is that the documentation is complete but not organized in the &quot;Tox user&quot;&#x27;s perspective, so for someone who shows up on a project using it, it&#x27;s hard to figure out the quickstart( though the &quot;general tips and tricks&quot; page gets somewhere [1])<p>Anyways yeah, would not recommend Make over just leaning into tox more here.<p>EDIT: also, this article reminded me of how much I really dislike Github Action&#x27;s configuration syntax. Just balls of mud on top of the Docker &quot;ball of mud&quot; strategy. I will re-iterate my belief that a CI system where the configuration system isn&#x27;t declarative but just like..... procedural Lua will be a billion dollar business. CI is about running commands one after another! If you want declarative DAGs use Bazel<p>[0]: <a href="https:&#x2F;&#x2F;tox.readthedocs.io&#x2F;en&#x2F;latest&#x2F;example&#x2F;pytest.html?highlight=posargs" rel="nofollow">https:&#x2F;&#x2F;tox.readthedocs.io&#x2F;en&#x2F;latest&#x2F;example&#x2F;pytest.html?hig...</a> [1]: <a href="https:&#x2F;&#x2F;tox.readthedocs.io&#x2F;en&#x2F;latest&#x2F;example&#x2F;general.html" rel="nofollow">https:&#x2F;&#x2F;tox.readthedocs.io&#x2F;en&#x2F;latest&#x2F;example&#x2F;general.html</a>
评论 #26735309 未加载
评论 #26736287 未加载
anticodonabout 4 years ago
I prefer to put `.PHONY` before every phony target, not gather them all in one place.<p>When phony targets are all written out in the beginning of the Makefile, it&#x27;s easy to forget to alter this list when a phony target is added or removed later.<p>This rarely causes an error, but still I&#x27;ve seen many Makefiles with outdated .PHONY lists.
tcbascheabout 4 years ago
I’m always glad to see Make being used. It’s such a powerful and simple tool that usually does the job just as well as more “bespoke” CLI’s for various frameworks and languages
评论 #26734906 未加载
评论 #26735392 未加载
评论 #26737166 未加载
评论 #26737119 未加载
评论 #26737117 未加载
simonwabout 4 years ago
I have a cookiecutter template for this at <a href="https:&#x2F;&#x2F;github.com&#x2F;simonw&#x2F;python-lib" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;simonw&#x2F;python-lib</a> (also click-app and datasette-plugin)<p>It sets up GitHub actions for publishing the package to PyPI when you create a release on GitHub - which I find way to be a really productive way of working.
评论 #26736611 未加载
wdrozabout 4 years ago
When you are doing machine learning, conda is widely used. Why? Because you can install non-python things like cudatoolkit or ffmpeg (you can even install python, so you are sure that everybody are using the same version of python)<p>Python is fantastic at gluing specialized tools&#x2F;libraries, but a lot of these require non-python dependencies (most are written in more performant languages). IMO, this is a big differences when comparing with Cargo for Rust because most of the dependencies in Rust are written in Rust.<p>The state of packaging in Python is kinda meh, the official documentation here [0] suggests to create 3 additional files in order to create a package:<p><pre><code> - pyproject.toml - setup.cfg - setup.py # optional, needed to make editable pip installs work </code></pre> if you add conda, you may need 2 additional files:<p><pre><code> - meta.yaml # to build you conda package - environment.yml </code></pre> With this much boilerplate, I understand why people are creating tools like flit.<p>[0] -- <a href="https:&#x2F;&#x2F;packaging.python.org&#x2F;tutorials&#x2F;packaging-projects&#x2F;" rel="nofollow">https:&#x2F;&#x2F;packaging.python.org&#x2F;tutorials&#x2F;packaging-projects&#x2F;</a>
midrusabout 4 years ago
2021 and still there is no clear winner or official way to install dependencies and create packages.<p>The worst part of Python.
评论 #26744180 未加载
brandmeyerabout 4 years ago
Lets see how many high-quality Python packaging guides we can cram into one discussion, shall we?<p>I have no problems using setuptools directly, as outlined in <a href="https:&#x2F;&#x2F;packaging.python.org&#x2F;guides&#x2F;distributing-packages-using-setuptools&#x2F;" rel="nofollow">https:&#x2F;&#x2F;packaging.python.org&#x2F;guides&#x2F;distributing-packages-us...</a>
darkwaterabout 4 years ago
I still don&#x27;t understand why in 2021 `pip`, which is the standard package and dependencies manager in Python, cannot 1) build a package from some spec 2) generate the scaffolding needed to build such package<p>`gem` from Ruby does that (well, it doesn&#x27;t 2 AFAIK but at least it does 1)
testtesting123about 4 years ago
Slightly off topic but does any recommend any great guides for building browser javascript &#x2F; node.js packages i.e listing recommended linters, profilers, testing strategy, documentation template, or a project structure?
alknemeyerabout 4 years ago
Somewhat related: I have an example repository which I&#x27;ve been using to keep track of the tools I use, aimed at people in a research lab who are relatively new to Python. I made it because the existing example&#x2F;template repositories I found don&#x27;t gel nicely with the way I like to set up and think about things. Here it is -- hope you find it useful:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;alknemeyer&#x2F;python-template&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;alknemeyer&#x2F;python-template&#x2F;</a>
jftugaabout 4 years ago
Which is the more standard way to use venv for Python 3.8 or newer?<p>1) venv at the same level:<p>create new project on GH<p>git clone <a href="https:&#x2F;&#x2F;github.com&#x2F;user&#x2F;myproject" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;user&#x2F;myproject</a><p>python -m venv myproject<p>cd myproject<p>(activate venv)<p>____<p>2) clone first, venv in subdirectory:<p>create new project on GH<p>git clone <a href="https:&#x2F;&#x2F;github.com&#x2F;user&#x2F;myproject" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;user&#x2F;myproject</a><p>cd myproject<p>python -m venv venv<p>(activate venv)<p>___<p>3) something completely different? if so, what?
74d-fe6-2c6about 4 years ago
Can somebody confirm that this is reasonable and state of the art? Or is it just a &quot;look what I can do&quot; type blog post?
评论 #26736596 未加载
评论 #26736578 未加载
Jukg7e3AFyskdjabout 4 years ago
Just use <a href="https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;zest.releaser&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;zest.releaser&#x2F;</a>
diimdeepabout 4 years ago
Can we have &quot;How to make a X package&quot; ? I would love to see also examples with C++(if it is possible at all) and Nim
say_it_as_it_isabout 4 years ago
What happened to setup.py and wheels?
thinker5555about 4 years ago
I work in a very small company where we are building the plane as we learn to fly, and I&#x27;m the only person there with any programming experience. I&#x27;ve been working on trying to improve my hobbyist-level (at best) knowledge of the Python ecosystem over the last year or so.<p>I&#x27;ve gotten to the point where a number of smaller tools I&#x27;ve put together can now be used in larger projects. I learned the hard way that just copying files around makes it hard to know which version is in that project, and upgrading, especially once there is more than one file, becomes a lot tougher. I learned the harder way that trying to link the same file into multiple projects is a great way to really screw things up.<p>About 4 or 5 months ago I made a real effort to try to learn how to use virtual environments (pipenv) and packaging to make it so that if I update one of the smaller tools, I don&#x27;t clobber all the downstream projects that rely on it. I wanted to make it so that when I update something to add features or change things, I can go back and fix older projects it&#x27;s used in at my leisure. I haven&#x27;t even begun to touch on unit testing, and I have no clue what linting is. Things are kind of working so far, but it feels very hacky and fragile, and I know it can (and SHOULD) be better.<p>All of this stuff around packaging and being able to install those packages very daunting, and trying to stumble on the right tutorials is extremely frustrating. The vast majority of them assume I want to share my stuff with the world on PyPI, or that I have servers available to me to create private PyPI indexes, but I don&#x27;t. Yet I still want my packages to &quot;resolve their own dependencies&quot; when I install or upgrade them.<p>And when it comes to learning things like testing, the few tutorials I&#x27;ve looked at either use different tools to do it, or their examples are so oversimplified that when I look at my own code, I don&#x27;t know where to begin.<p>I say all of this because looking at this tutorial, it&#x27;s more of the same. I want to make my code better. I want to make it easier to use those smaller projects in larger projects. But then it says things like &quot;Every solid open-source project runs cloud tests after each commit, so we will too,&quot; but it doesn&#x27;t do anything to explain what that is or why it should be done, besides &quot;everyone does it, so you should, too.&quot;<p>I think what makes it even harder is that when something like this gets shared, there are so many conflicting opinions. Some people say to just use setuptools, other say that setuptools is on its way out and to use pyproject.toml (or some other tool) instead. It&#x27;s all just so... hard!<p>I&#x27;m sorry. This is coming off a bit ranty, and that&#x27;s not what I intended. I&#x27;m just feeling frustrated and I&#x27;m not sure of a better way to express that I need help with finding help. There are even a lot of things that I&#x27;m sure I need help with, but I just don&#x27;t know what they are. It makes it really hard to verbalize what I need to another person, let alone to get the right words into a search engine to take me there.
1337shadowabout 4 years ago
Still hardcoding version numbers in 2021? Why not use setupmeta
评论 #26735980 未加载