python library not defined in user defined function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: python library not defined in user defined function (/thread-27221.html) |
python library not defined in user defined function - johnEmScott - May-30-2020 Why doesn't this work? def main(): import os fileName = "text.txt" userfunction(fileName) def userfunction(fileName): os.path.exists(fileName) main()I get the following error:
RE: python library not defined in user defined function - ndc85430 - May-30-2020 Because the import is local to your main function. Imports should go at the top of the file, as per PEP 8.
RE: python library not defined in user defined function - DT2000 - May-30-2020 As mentioned above by ndc85430 you need to import your modules before the actual code functions are executed. import os def main(): fileName = "text.txt" userfunction(fileName) def userfunction(fileName): os.path.exists(fileName) main() |