Shouldn't the try: __import__ in __init__ re-raise original exception?












0














It is confusing when import pandas raises a message like:



>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/raf/anaconda3/lib/python3.7/site-packages/pandas/__init__.py", line 19, in <module>
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['pytz']


Where actually the true error is not in pytz, but in a missing library required by pytz:



>>> import pytz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/__init__.py", line 20, in <module>
from pytz.tzinfo import unpickler, BaseTzInfo
File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/tzinfo.py", line 4, in <module>
from bisect import bisect_right
ImportError: cannot import name 'bisect_right' from 'bisect' (/home/raf/tmp/bisect.py)


As you can see, I happen to have a file called bisect.py in the same folder where I'm trying to import pandas, but the error message raised by pandas doesn't reflect that. It was a silly error after all, but It gave me a headache to sort that out.



Tracking back the source of it, in pandas/__init__.py, note that the original error e is not used after except ImportError as e. I think it should.



for dependency in hard_dependencies:
try:
__import__(dependency)
except ImportError as e:
missing_dependencies.append(dependency)

if missing_dependencies:
raise ImportError(
"Missing required dependencies {0}".format(missing_dependencies))




Question also raised here:
https://github.com/pandas-dev/pandas/issues/23868










share|improve this question





























    0














    It is confusing when import pandas raises a message like:



    >>> import pandas
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/raf/anaconda3/lib/python3.7/site-packages/pandas/__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
    ImportError: Missing required dependencies ['pytz']


    Where actually the true error is not in pytz, but in a missing library required by pytz:



    >>> import pytz
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/__init__.py", line 20, in <module>
    from pytz.tzinfo import unpickler, BaseTzInfo
    File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/tzinfo.py", line 4, in <module>
    from bisect import bisect_right
    ImportError: cannot import name 'bisect_right' from 'bisect' (/home/raf/tmp/bisect.py)


    As you can see, I happen to have a file called bisect.py in the same folder where I'm trying to import pandas, but the error message raised by pandas doesn't reflect that. It was a silly error after all, but It gave me a headache to sort that out.



    Tracking back the source of it, in pandas/__init__.py, note that the original error e is not used after except ImportError as e. I think it should.



    for dependency in hard_dependencies:
    try:
    __import__(dependency)
    except ImportError as e:
    missing_dependencies.append(dependency)

    if missing_dependencies:
    raise ImportError(
    "Missing required dependencies {0}".format(missing_dependencies))




    Question also raised here:
    https://github.com/pandas-dev/pandas/issues/23868










    share|improve this question



























      0












      0








      0







      It is confusing when import pandas raises a message like:



      >>> import pandas
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/raf/anaconda3/lib/python3.7/site-packages/pandas/__init__.py", line 19, in <module>
      "Missing required dependencies {0}".format(missing_dependencies))
      ImportError: Missing required dependencies ['pytz']


      Where actually the true error is not in pytz, but in a missing library required by pytz:



      >>> import pytz
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/__init__.py", line 20, in <module>
      from pytz.tzinfo import unpickler, BaseTzInfo
      File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/tzinfo.py", line 4, in <module>
      from bisect import bisect_right
      ImportError: cannot import name 'bisect_right' from 'bisect' (/home/raf/tmp/bisect.py)


      As you can see, I happen to have a file called bisect.py in the same folder where I'm trying to import pandas, but the error message raised by pandas doesn't reflect that. It was a silly error after all, but It gave me a headache to sort that out.



      Tracking back the source of it, in pandas/__init__.py, note that the original error e is not used after except ImportError as e. I think it should.



      for dependency in hard_dependencies:
      try:
      __import__(dependency)
      except ImportError as e:
      missing_dependencies.append(dependency)

      if missing_dependencies:
      raise ImportError(
      "Missing required dependencies {0}".format(missing_dependencies))




      Question also raised here:
      https://github.com/pandas-dev/pandas/issues/23868










      share|improve this question















      It is confusing when import pandas raises a message like:



      >>> import pandas
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/raf/anaconda3/lib/python3.7/site-packages/pandas/__init__.py", line 19, in <module>
      "Missing required dependencies {0}".format(missing_dependencies))
      ImportError: Missing required dependencies ['pytz']


      Where actually the true error is not in pytz, but in a missing library required by pytz:



      >>> import pytz
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/__init__.py", line 20, in <module>
      from pytz.tzinfo import unpickler, BaseTzInfo
      File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/tzinfo.py", line 4, in <module>
      from bisect import bisect_right
      ImportError: cannot import name 'bisect_right' from 'bisect' (/home/raf/tmp/bisect.py)


      As you can see, I happen to have a file called bisect.py in the same folder where I'm trying to import pandas, but the error message raised by pandas doesn't reflect that. It was a silly error after all, but It gave me a headache to sort that out.



      Tracking back the source of it, in pandas/__init__.py, note that the original error e is not used after except ImportError as e. I think it should.



      for dependency in hard_dependencies:
      try:
      __import__(dependency)
      except ImportError as e:
      missing_dependencies.append(dependency)

      if missing_dependencies:
      raise ImportError(
      "Missing required dependencies {0}".format(missing_dependencies))




      Question also raised here:
      https://github.com/pandas-dev/pandas/issues/23868







      pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 11:49

























      asked Nov 23 '18 at 11:07









      Raf

      373313




      373313





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445555%2fshouldnt-the-try-import-in-init-re-raise-original-exception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445555%2fshouldnt-the-try-import-in-init-re-raise-original-exception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)