Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 python library not defined in user defined function
#1
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:
Error:
NameError: name 'os' is not defined
Quote
#2
Because the import is local to your main function. Imports should go at the top of the file, as per PEP 8.
buran likes this post
Quote
#3
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()
"Often stumped... But never defeated."
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Variable not defined Heyjoe 4 119 Yesterday, 11:27 PM
Last Post: Heyjoe
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 98 Yesterday, 04:51 AM
Last Post: pjfarley3
  iundefined is really defined Skaperen 5 472 Jul-01-2020, 06:32 AM
Last Post: Gribouillis
  Trouble with Name not being defined - don't know why? simms7400 1 200 Jul-01-2020, 01:06 AM
Last Post: bowlofred
  Name 'function' not defined, although it is... dougDimmadome_ 2 208 Jun-23-2020, 03:25 PM
Last Post: dougDimmadome_
  NameError: name 'pins' is not defined glennford49 2 432 Jun-15-2020, 12:20 PM
Last Post: glennford49
  Name Error: name 'Stockton' is not defined Pinokchu 3 182 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  [split] NameError: name 'formats' is not defined shobhu123 1 337 May-24-2020, 06:29 PM
Last Post: Skaperen
  error ,,name append is not defined'' Killdoz 1 297 May-24-2020, 06:23 PM
Last Post: bowlofred
  NameError: name 'cross_validation' is not defined tmhsa 5 922 Apr-21-2020, 08:15 PM
Last Post: DT2000

Forum Jump:


Users browsing this thread: 1 Guest(s)