importing a module in Idle shell

巧了我就是萌 提交于 2020-01-12 15:52:14

问题


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


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




回答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.




回答3:


from Brian Fitzgerald in Loading (and unloading) Python modules

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

del sys.modules["package"]
del package

"



来源:https://stackoverflow.com/questions/7971744/importing-a-module-in-idle-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!