Home > python > Using naming conventions to make `import *` safer in Python

Using naming conventions to make `import *` safer in Python

July 24Hits:1
Advertisement

Consider the following code:

a.py

import os  def print_rand():     print os.urandom() 

And another module:

b.py

from a import * 

In this code, I end up with an os variable getting imported from a. I could prevent this with a small change:

a.py

import os as _os  def print_rand():     print _os.urandom() 

Since from a import * does not pick up on members beginning with an underscore, b wouldn't import the os module from a.

I've thought about doing this in some of my code. I never use import *, but logically, it makes some sense anyway. (Not to mention I don't work alone on my code.) os is a member that a uses internally; it's not there to be exposed to the outside world. (Yes, I'm aware that it can be accessed anyway, but I just mean in the sense that the underscore conventionally means "private".)

Is this something I should do? Would it be considered "Pythonic", or is it something I should particularly avoid doing? Does it even matter? (Should I have asked on StackOverflow? It seems too conceptual for there to me.)

Answers

I've seen this done, although not often. I wouldn't do this myself, for several reasons:

  • In case of a module, it is not the module itself that is internal, it's the fact of its usage that is internal. The module can be accessed quite legitimately by importing it explicitly, especially the one from the standard library. So the leading underscore will only confuse someone who reads your code.
  • If a user of your package relies on something that is not explicitly mentioned in the documentation (and you probably won't advertise os module as a part of your module's public API), it is not really your problem. Usual assumption in this case is that if you want to use something from the standard library, you import it yourself.
  • PEP8 recommends using __all__ for modules that are imported with import *, which eliminates the problem.
  • The usage of import * is often frowned upon anyway. For example, pylint complains about it.

That said, these are still not 100% compelling reasons, and there may be cases where obscuring the imported module's name in such a way is justified.

Related Articles

  • Using naming conventions to make `import *` safer in PythonJuly 24

    Consider the following code: a.py import os def print_rand(): print os.urandom() And another module: b.py from a import * In this code, I end up with an os variable getting imported from a. I could prevent this with a small change: a.py import os as

  • issues importing arcpy in Python IDLEJune 8

    I would want to use arcpy in my python code. I am able to import it through the ARCGIS desktop python console. But I am not able to import it into Python IDLE. I get the following error Traceback (most recent call last): File "C:\Python26\lib\site-pa

  • Import functions in python Add-In logic script

    Import functions in python Add-In logic scriptFebruary 1

    I am running into an error. I have a set of functions which I want to import in the python Add-In script at the time of execution, but when I built Add-In with "import stats" line then tools are unresponsive. Any suggestions whether it is possib

  • Importing otb into PythonMarch 21

    I have Python 2.7 (installed together with ArcGIS 10.1). Via OSGeo4W a have installed qGIS and otb application.I was able to connect qGIS and otb and it's working fine. Through OSGeo installer I installed also otb-python and otb-wrapping. But I have

  • Importing modules in python gives syntax errorSeptember 16

    Whenever I try to import modules from python source code files using the python shell(installed in brew folder), I get the following error : >>> from not import *; File "<stdin>", line 1 from not import *; ^ SyntaxError: invalid s

  • unable to import pcl to python?January 29

    I followed the instructions given here http://www.pointclouds.org/downloads/linux.html . After installing the libpcl , what should be done to import it in python? --------------Solutions------------- You should import libpcl now in your Python script

  • Cannot import module in python shell or py filesSeptember 29

    I use xubuntu 14.04.3,and its default python version is python 2.7.6 and python 3.4.3.Then I download python 2.7.10 source code and compile install with command ./configure make sudo make -i install make shows some modules not found: INFO: Can't loca

  • Python - pip pip2 pip3 pip3.5 packages are installed but cannot be imported in any Python sessionJanuary 8

    As I am using Xubuntu I thought perhaps this was a better forum than Stackexchange for some questions relating to pip and Python Today I have encountered troubles with pip and Python. For some reason I can install packages with pip install but the pa

  • Importing Wand in Python Ubuntu 14.04January 15

    I'm fairly new to Ubuntu, Python and have never used imported anything in Python before. My Ubuntu system has Python2.7, 3 and 3.4 installed. I'm trying to start working with Wand for an assignment. Using this documentation from Wand website I instal

  • Is WP Importer Safe on Multisite?December 17

    Is it safe to allow users of a multisite to use WP importer? I'm assuming importer can only create pages, posts and users (if I allow them to add users to their site), but my concern is whether a hacker can create an account and import malicious code

  • installing OpenCV 2.0 on Ubuntu Karmic: still get ImportError from "import cv" in PythonFebruary 25

    I have tried compiling and installing from the 2.0.0 release source, the SVN head, and from a PPA. After each, I still get the following from a Python console: >>> import cv Traceback (most recent call last): File "<stdin>", line

  • Why would "import arcpy" hang Python script run from DOS/cmd prompt?January 17

    I have a very small test script (test.py) that I use to see if Python is installed and ready to use ArcPy. All it contains is: print "Starting" import os print "OS imported" import arcpy print "ArcPy imported" I'm running it

  • Why can't I import "pygtk" with Python 3.2 from PyDev?January 20

    I have installed Eclipse + PyDev + Python 3.2. Now I have two Python interpreters in PyDev: Python 2.7 and Python 3.2. If I try to execute command import pygtk with 2.7 interpreter it works fine, but with Python 3.2 it raises ImportError: No module n

  • Importing GDAL from Python window of ArcGIS for Desktop?August 14

    I am trying to run some Python code using the Python window in ArcGIS 10.1 that uses both the arcpy and gdal modules. However, when I try and import the gdal module I get an error: ImportError: No module named osgeo Obviously it can't find the module

  • Django installed, but can't import django in pythonFebruary 2

    I've installed Django multiple ways, via apt-get and pip install. They all say I have the most recent version of Django. Now whenever I run python in Terminal and type in import django, I receive ImportError: No module named django However, when I ru

  • Underscore in filenames in \textit{} imported from a python scriptJune 27

    I'm trying to import the name of a result file from a python script and displaying it in the report. In the LaTeX template I have a bunch of variables defined as ${variablename}. The python script changes this into the actual variable values. I only

  • How to import screenlets on PythonFebruary 20

    (I'm using Ubuntu 10.04 with Linux 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010 i686) For some reason I had updated some Python files (I'm not able to tell which ones, but it was during the installation of Hugin). After that I notice

  • error importing numpy into pythonMay 20

    Hello I know there are a lot of posts already about this but after reading through previous posts I still have not found a solution to my issue. To install python I followed the steps of this post here. All seemed to work as expected. In terminal, ty

  • Error importing lxml package python in UbuntuOctober 10

    When I try importing lxml package in python I am receiving the following error. Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/lxml/html/__init__.py", line 42,

  • Why no "import speechd" in Python 3?October 26

    Despite a page (link below) implying that speechd works with Python 3, "import speechd" from within Python 3.4.2 gets "no module named 'speechd'". (While I have reasons to prefer Python 2.7, the linked page suggests that's not possible

Copyright (C) 2018 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.670 s.