Issue
I recently migrated my solution using pytz
to dateutil.tz
. I've tested everything with iPython, and it worked nicely. However, when in production with python... ERROR!
Can anyone explain why Python and iPython deal differently with the imports, and if there is a way to make python behave the same way as iPython does?
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'dateutil' has no attribute 'tz'
>>>
$ ipython
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import dateutil
In [2]: dateutil.__file__
Out[2]: '/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
In [3]: dateutil.tz
Out[3]: <module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>
Solution
Because IPython wants to be too clever. For any reason it has already imported dateutil.tz
and when you manually import dateutil
, then dateutil.tz
is available in you main namespace.
But as is is a submodule, you should consistently import it if you want to use it. So you should replace import dateutil
with import dateutil.tz
and your code will work fine on both environment.
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil.tz
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
<module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>
Answered By - Serge Ballesta
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.