3

This is my code:

import os

if os.path.exists(r'C:\Genisis_AI'):
    print("Main File path exists! Continuing with startup")
else:
    createDirs()

def createDirs():
    os.makedirs(r'C:\Genisis_AI\memories')

When I execute this, it throws an error:

File "foo.py", line 6, in <module>
    createDirs()
NameError: name 'createDirs' is not defined

I made sure it's not a typo and I didn't misspell the function's name, so why am I getting a NameError?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
NaruS
  • 168
  • 1
  • 15
  • Does this answer your question? [Python NameError: name is not defined](https://stackoverflow.com/questions/14804084/python-nameerror-name-is-not-defined) – Georgy Oct 07 '20 at 08:28

1 Answers1

10

You can't call a function unless you've already defined it. Move the def createDirs(): block up to the top of your file, below the imports.

Some languages allow you to use functions before defining them. For example, javascript calls this "hoisting". But Python is not one of those languages.


Note that it's allowable to refer to a function in a line higher than the line that creates the function, as long as chronologically the definition occurs before the usage. For example this would be acceptable:

import os

def doStuff():
    if os.path.exists(r'C:\Genisis_AI'):
        print("Main File path exists! Continuing with startup")
    else:
        createDirs()

def createDirs():
    os.makedirs(r'C:\Genisis_AI\memories')

doStuff()

Even though createDirs() is called on line 7 and it's defined on line 9, this isn't a problem because def createDirs executes before doStuff() does on line 12.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Ah thank you, a stupid mistake on my part, I never actually had a issue doing this before(At least I think I tend to do this alot) and have never faced this problem, thank you for the help Kevin. Though seeing your comment on javascript(A language I program in as well as python) I make have just mixed this up. – NaruS May 08 '18 at 15:27
  • @NaruS if this answer solved your problem, please consider upvoting as well as accepting it as correct. https://stackoverflow.com/help/someone-answers – Aaron May 08 '18 at 15:36
  • I had to wait till the time limit, I did upvote it however. In fact I did upvote as soon as I saw it, (7 minutes must pass after posting the question before a answer can be accepted as correct so that was the reason for the acceptance delay) – NaruS May 08 '18 at 15:48
  • 1
    @NaruS Note that there is a huge difference between calling a function globally, as in your original code, and calling a function from another function. You have probably done the later many times before without a problem. – Code-Apprentice May 08 '18 at 16:14
  • @Code-Apprentice Thanks for the note, Its something I have done before and am familiar with and tend not to have an issue with, it also wasn't my initial intention(As Aaron stated, I intentionally attempted to define the function after calling it as you would be able to in something like javascript[A language I script in as well] and didn't realize python didn't work in the same way) Either way, Thanks for that note, I will make sure to keep that in mind. – NaruS May 08 '18 at 16:30