What's the best practice for writing an "execute only" Python module?
NickName:orome Ask DateTime:2014-01-05T00:59:46

What's the best practice for writing an "execute only" Python module?

I have a Python module that is intended exclusively for running as a script and never as something that should be imported, and I'd like to enforce (and communicate) that intention in my code.

What is the best practice for accomplishing this?


I can imagine a few options such as wrapping the whole file in

if __name__ == '__main__':
    # All the code in the module

or aborting at the start

if __name__ != '__main__':
    exit()

# All the code in the module

perhaps with a warning

if __name__ != '__main__':
    print('You should not import this')
    exit()

# All the code in the module

or even an assertion

assert __name__ == '__main__', 'You should not import this'

But I'm not sure which (if any) is appropriate, stylistically or technically.

Copyright Notice:Content Author:「orome」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20923892/whats-the-best-practice-for-writing-an-execute-only-python-module

More about “What's the best practice for writing an "execute only" Python module?” related questions

What is AWS CodeArtifact? What is it and what is it useful for

I have some question to a service called AWS CodeArtifact? What is it and what is it useful for. Any links to the documentation?

Show Detail

What is AttributeSet and what is it used for?

I have read the docs, but the docs seem vague to me and I'm having a hard time understanding what it is exactly and what it's used for. Two question regarding this: What exactly is the AttributeSet

Show Detail

What is preemption / What is a preemtible kernel? What is it good for?

Explained in your own words, what is preemption and what does it mean to a (linux) kernel? What are advantages and disadvantages in having a preemptible kernel?

Show Detail

What is GLIBC? What is it used for?

I was searching for the source code of the C standard libraries. What I mean with it is, for example, how are cos, abs, printf, scanf, fopen, and all the other standard C functions written, I mean ...

Show Detail

What is CUDA like? What is it for? What are the benefits? And how to start?

I am interested in developing under some new technology and I was thinking in trying out CUDA. Now... their documentation is too technical and doesn't provide the answers I'm looking for. Also, I'd...

Show Detail

enCapsa -what is it and what is used for?

It may not be a pure programming question but I'm looking for information about enCapsa. Do you know what it is, have you ever used it? I'm reading some papers about it but I can't really see how it

Show Detail

Docker, what is it and what is the purpose

I've heard about Docker some days ago and wanted to go across. But in fact, I don't know what is the purpose of this "container"? What is a container? Can it replace a virtual machine dedicated...

Show Detail

What is applescript and what is it used for?

What is applescript and what is it used for?

Show Detail

Widget -what to do and what not

I would like to make a html/js widget that people can add to their blogs, sites etc. I have never made widgets before so I would like to hear what should be done and what not. For example, how do ...

Show Detail

What is JPQL, what does it do, and what is it used for?

What are the uses of JPQL? I'm fairly new to JPA. Doesn't the EntityManager perform the CRUD operations for you? What is JPQL used for?

Show Detail