Domanda

I'm trying to learn python and I'm having trouble importing a module. I have a .pyc file that I'm trying to import into idle shell called dfa.pyc

I have the file in a folder called xyz. I navigate to this folder using:

    os.chdir('/Users/xxx/Desktop/xyz')

So now, if I try to run the command:

    from dfa import *

i get the error:

    ImportError: No module named dfa

If i run the command:

    os.path.isfile('dfa.pyc') 

it returns true.

Can someone explain how i can get the dfa.pyc file imported?

Thanks

È stato utile?

Soluzione

I don't think python modules are loaded I based on what you do with chdir. Modules are loaded from the folder you started the python shell and folders in PYTHONPATH.

If you want dynamically load modules maybe you can check imp.loadmodule (sample in the bottom of the page).

Altri suggerimenti

you can add to the PYTHONPATH in code by doing

sys.path.append('<newpath'>)
from dfa import *

I don't believe changing your current directory has any impact on the import process and even if it did, I'm not sure that's how you would want to do it.

from Brian Fitzgerald in Loading (and unloading) Python modules

" ... and this to “un-import”

del sys.modules["package"]
del package

"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top