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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 if this answer solved your problem, please consider upvoting as well as accepting it as correct. stackoverflow.com/help/someone-answers
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 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 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.