setup.py some unknown tricks in Python

Source: Internet
Author: User
Tags git commands

http://python.jobbole.com/80912/

Before I start, I want to make it clear that I'm going to explain some of the "tricks". They are not "best practices", at least in one case it is undesirable.

Speaking of undesirable practices, I will write a "setup.py trap" blog post, which I believe you will not do in the setup.py module.

Trick

These tips make it easier for me to use Python for packaging management. Before you refine them, I suggest you have at least some basic experience in creating new packages. The two ways to learn Python packaging are new Library Sprint (beginner) and Python Packaging User Guide (Advanced).

' Python setup.py publish '

It all started here. One day when I was looking at Tom's code, I found the Python setup.py Publish command inside the setup.py module in the Django Rest Framework . It looks like this:

Python
12345678910111213141516 # setup.pyimport os import sys # I ' ll discuss version tricks in a future blog post.version = "42.0.0" if sys. argv[-1] = = ' publish ': os. System("python setup.py sdist upload") os. System("python setup.py bdist_wheel upload") Print("You probably want to also tag, the version now:") Print("git tag-a%s-m ' version%s '" % (version, version) ) Print("git push--tags") sys. Exit() # Below This is the rest of the setup () function

It's awesome, I don't need to look for some obscure python setup.py sdist upload commands, or really confusing python setup.py bdist_wheel upload The order. Instead, when it comes to posting packages on PyPI, I just need to lay down:

Python
1 $ python setup. PY Publish

It's so much more!

' Python setup.py tag '

The problem with Tom's Python setup.py publish directive is that he forced me to play the git tag command. Well, honestly, he asked me to copy/paste the output on my screen. So, all by myself, I "invented" the Python setup.py tag directive:

Python
123456 # setup.pyif sys. argv[-1] = = ' tag ': os. System("git tag-a%s-m ' version%s '" % (version, version)) os. System("git push--tags") sys. Exit()

It's beautiful, huh? Now I don't need to remember so many vague git commands. I got the short version of the Python setup.py Publish command:

Python
1234 if sys. argv[-1] = = ' publish ': os. System("python setup.py sdist upload") os. System("python setup.py bdist_wheel upload") sys. Exit()

When I needed to make a version, I used my code and then typed it:

Python
12 $ python setup. PY Publish$ python setup. PY Tag

Why don't I merge those codes? Well, you can not use "RC1" or "-alpha" as your PyPI version name. By separating these commands, I can have more granular control over the release of my package. I was encouraged to release participants with Alpha, beta, and git tag, rather than the official pypi.

' Python setup.py test '

I'm pretty sure some of my readers are having a serious problem with this trick. In fact, according to the people who manage the basic building of Python packages, this will be blog post in my Next "trap".

So then ...

I like Py.test. I have written about the use of Py.test blog. I try to use it everywhere. However, I really don't have to use python setup.py test fanatics. The moment I felt uncomfortable with Py.test was when it made me add special classes to the setup.py.

Unfortunately, there is another way:

Python
1234567891011121314 if sys. argv[-1] = = ' Test ': test_requirements = [ ' pytest ', ' Flake8 ', ' coverage '     ] try: modules = map(__import__, test_requirements) except importerror as e:         err_msg = e message. Replace "No module named" ") msg = "%s is not installed." Install your test requirments. " % err_msg raise importerror(msg) os. System(' py.test ') sys. Exit()

Just means I'm going to add a simple code to use Py.test and Python setup.py test:

Python
1 $ python setup. PY Test

Theoretically, you can run the pip Install command to install a missing dependency package, or to call from a requirements file. However, since this is the "trick", I want it to keep it simple and useful. If I get enough good results with this, I'll update this example of a PIP call that includes a missing request.

Note: This is not to say I don't have to tox. In fact, I used Tox to invoke my version of Python setup.py test.

What about the subprocess module?

Some people ask, "Why don't you use the sub-process library to use these shell commands?" ”

My answer is, "because if I had to kill the chickens, I would have to kill sledgehammer." "For these simple tricks, theos.system () function is sufficient.

Why not just use makefile?

I started programming on Mac OSX and Linux, and many of my open source packages use Windows. Thanks to Appveyor, I'm constantly testing the code in that environment. In fact, I will improve my "tips" for Windows users.

Trap!

The "Traps" blog will be released in early 2015, so please expect

Updates
    • 2014/12/21–added a note about using Tox.
    • 2014/12/21–added a note about Makefile and Windows

setup.py some unknown tricks in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.