I'm making a function for an assignment right now, and I used "sys" and 'random' in the function. I was just wondering where the most acceptable place is to put the "import ___" is when you're making a function? I wanted to know for future reference if it's better to put the "import" inside your function, or if it should stay outside.
Asked
Active
Viewed 37 times
2 Answers
1
Put your imports at the top of the file.
The only time it really makes sense to import
things anywhere else, is if you're doing so conditionally, to see if said library exists for example.
From this answer:
PEP 8 authoritatively states:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

Community
- 1
- 1

Jonathon Reinhart
- 132,704
- 33
- 254
- 328
0
From PEP 8:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
More pep8 imports

pythad
- 4,241
- 2
- 19
- 41