1

Perhaps I'm just miswording my searches, but I'm not finding anything concrete for the following scenario:

Say I have my Python script in main.py and I have a few helper functions in a helpers.py module. In helpers.py, I have a function that relies on urllib, and I'm not using urllib inside of main.py otherwise.

Should I import the module inside of helpers.py, inside the function using urllib, or just inside main.py?

Edit: For some reason, this question was marked as a duplicate to import at module level or at function level?, but this is not correct. PEP8 guidelines advise doing imports at the top of the file. They don't answer the scenario I've advised above, where the function with its own dependencies is imported from another file.

David Metcalfe
  • 2,237
  • 1
  • 31
  • 44
  • Try importing it in `main.py` and see what happens. And anyway, even if that worked, it'd still be better to import it directly in `helpers.py`, otherwise the `helpers` module would be completely useless if it's not used together with `main.py`. Always import dependencies there where you use them. – Aran-Fey Apr 08 '18 at 23:30
  • If you are importing from another file, then put your import at the top of that file, putting it on top of main file will not work. If you need `urllib` on both file, you have to import it in both files, this is how python works. The question is answered there, please read both the answers. – Munim Munna Apr 09 '18 at 07:45

0 Answers0