User Tools

Site Tools


programming:python:packaging

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Last revisionBoth sides next revision
programming:python:packaging [2018-07-25 02:13] – remove __all__ section due to being poor practice zlgprogramming:python:packaging [2018-09-22 10:45] – [Single Member Importing] better explain namespacing zlg
Line 52: Line 52:
 </code> </code>
  
-In the above example, ''do_route'' will become available in the current environment, imported from the ''router'' module inside the ''my_package'' package. If you end up with name collisionsyou can rename the import with the ''as'' keyword:+In the above example, ''do_route'' will become available in the current environment, imported from the ''router'' module inside the ''my_package'' package. 
 + 
 +If you find yourself with strange ''NameError''s''AttributeError''s, or ''ValueError''s, it may point to a name collision. You can rename your imports with the ''as'' keyword:
  
 <code python> <code python>
Line 59: Line 61:
 </code> </code>
  
 +This is often caused by importing two modules with the same member name. If you import two things with the same member name, the latter name will take precedence:
 +
 +<code python>
 +# Same name, different structures
 +from os import path
 +print(type(path))
 +# <class 'module'>
 +from sys import path
 +print(type(path))
 +# <class 'list'>
 +</code>
 +
 +For this reason, it's generally better to leave imports alone and go one level deeper only as needed. So if you wanted to use ''path'' from both ''os'' and ''sys'', just import them on their own, so you're using ''os.path'' and ''sys.path''. It's more verbose, but also more precise and less likely to break.
 ====== References ====== ====== References ======
  
programming/python/packaging.txt · Last modified: 2023-11-06 20:36 by zlg