简体   繁体   中英

ImportError: No module named <> after installing using python code

Assuming I don't have pandas and numpy installed in my Python, How can I refresh my terminal after successfully install a module.

I tried installing these 2 modules using a code in my install.py :

try:
    import pandas as pd
except ImportError:
    from pip._internal import main as pip
    pip(['install', '--user', 'pandas'])

try:
    import numpy as np,pandas as pd
except ImportError:
    from pip._internal import main as pip
    pip(['install', '--user', 'numpy'])

print "Dependencies installed successfully"

Then I import it to my combined.py then imported pandas and numpy

from install import *
import pandas as pd
import numpy as np

The installation was successful but after that this error occurs:

import pandas as pd
ImportError: No module named pandas

When I tried to run it, of course pandas and numpy are installed, It doesn't show the error, I think the terminal didn't recognize the module installed. Any Solutions for this?

My guess is that the first time you run it the interpreter does not rescan the modules so the package is installed but the interpreter is not aware of it. You must restart the interpreter. Maybe reloading the sys.path could do the job - see How to refresh sys.path? :

import importlib, site
importlib.reload(site)

although reload is frowned upon for good reason and maybe simpler is to restart the interpreter - or alternatively directly add the new pandas and numpy dirs to sys.path although starts getting dirty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM