importing a module in Idle shell

31,177

Solution 1

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).

Solution 2

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.

Share:
31,177
android_student
Author by

android_student

Updated on July 09, 2022

Comments

  • android_student
    android_student almost 2 years

    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