Import module works in terminal, but not in IDLE
This usually happens when multiple versions of python are installed with different paths. You can check if you have multiple installations by opening IDLE terminal and using
import sys
sys.version
sys.path
These commands will print the system PATH and version of the current python instance. Use this in both IDLE and the command line terminal to see where they differ. Once you know which version you want, just uninstall the other. You can also uninstall all python instances and then reinstall the clean python environment, but then you will have to reinstall all your modules using pip or easy_install
source to share
- Open python in cmd (type
python
and hit enter) - Import the module into cmd (enter
import modulename
) - Enter
modulename.__file__
- You will get the path where the module is stored
- Copy the appropriate folder
- In IDLE
import sys
andsys.executable
to get the paths it looks for modules to import - Paste your module folder into the path where IDLE looks for modules.
This method worked for me.
source to share
You can pip show
after installing the package and find out where the package is.
After that, check in IDLE sys.path
and if the package directory is not in sys.path
try adding it.
import sys sys.path.append ("/ home / dm / .local / lib / python3.6 / site-packages") # or another folder that 'pip show' about package.
source to share