Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What are the “best practices” for using import in a module?

best practices import module
0
10 Posted

What are the “best practices” for using import in a module?

0

In general, don’t use from modulename import *. Doing so clutters the importer’s namespace. Some people avoid this idiom even with the few modules that were designed to be imported in this manner. Modules designed in this manner include Tkinter, and threading. Import modules at the top of a file. Doing so makes it clear what other modules your code requires and avoids questions of whether the module name is in scope. Using one import per line makes it easy to add and delete module imports, but using multiple imports per line uses less screen space. It’s good practice if you import modules in the following order: • standard library modules — e.g. sys, os, getopt, re) • third-party library modules (anything installed in Python’s site-packages directory) — e.g. mx.DateTime, ZODB, PIL.Image, etc. • locally-developed modules Never use relative package imports. If you’re writing code that’s in the package.sub.m1 module and want to import package.sub.m2, do not just write import m2, even th

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.