Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing a module in Idle shell

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

like image 554
android_student Avatar asked Nov 01 '11 19:11

android_student


People also ask

What is the command to import a module?

The Import-Module cmdlet adds one or more modules to the current session. Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module. However, you can still use the Import-Module command to import a module.


2 Answers

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

like image 85
Lycha Avatar answered Oct 01 '22 03:10

Lycha


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.

like image 20
Jeffery Smith Avatar answered Oct 01 '22 03:10

Jeffery Smith