-1

I'm trying to use a function called start to set up my enviroment in python. The function imports os. After I run the function and do the following os.listdir(simdir+"main") I get a error that says os not defined code

>>> def setup ():
    import os.path
    import shutil
    simdir="e:\\"
    maindir="c:\\backup\\bitcois\\test exit\\"

>>> setup()
>>> os.listdir(simdir+"main")
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    os.listdir(simdir+"main")
NameError: name 'os' is not defined
try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

5 Answers5

1

The import statement is scoped. When importing modules they are defined for the local namespace.

From the documentation:

Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs). [...]

So in your case the os package is only defined within function setup.

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
  • What about `simdir` that is scoped too? – Malik Brahimi May 25 '15 at 17:25
  • Yes, @MalikBrahimi, according variables are local scoped unless a global valiable has been imported using the `global` statement: `"If the name does not occur in a global statement in the current code block: the name is bound to the object in the current local namespace."` ([Assignment statements](https://docs.python.org/2/reference/simple_stmts.html#index-7)) – try-catch-finally May 25 '15 at 17:36
0

You are getting this error because you are NOT importing the whole os library but just the os.path module. In this way, the other resources at the os library are not made available for your use.

In order to be able to use the os.listdir method, you need to either import it alongside the os.path like this:

>>> def setup ():
    import os.path, os.listdir
    import shutil
    simdir="e:\\"
    maindir="c:\\backup\\bitcois\\test exit\\"

or import the full library:

>>> def setup ():
    import os
    import shutil
    simdir="e:\\"
    maindir="c:\\backup\\bitcois\\test exit\\"

You can read more here: https://docs.python.org/2/tutorial/modules.html

FBidu
  • 972
  • 9
  • 21
0

try:

import os.path
import shutil
import glob

def setup ():
    global simdir
    simdir="e:\\"
    maindir="c:\\backup\\bitcois\\test exit\\"

setup()
os.listdir(simdir+"main")
  • Usage of global ??? Global is bad: http://stackoverflow.com/questions/19158339/why-are-global-variables-evil – Zulu May 25 '15 at 17:41
0

You need to return the paths and assign the returned values in the global scope. Also, import os too:

import os

def setup():
    # retain existing code
    return simdir, maindir

simdir, maindir = setup()
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

When you import os or do any sort of command within a function, the command's effect only last while that function itself is running. What you need to do is

import os
...Do your function and other code

This way, your import lasts for the whole program :).

rassa45
  • 3,482
  • 1
  • 29
  • 43