0

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?

  • 1
    Do you have two version of Python ? – Rahul Agarwal Oct 09 '18 at 10:06
  • yes 2.7 and 3.6 why? –  Oct 09 '18 at 10:08
  • `pandas` requires `numpy`, not the other way around (you're handling the pandas exception before the numpy exception). I haven't seen `pip` being used like this before though so I'm not sure if there's something else wrong with your approach – roganjosh Oct 09 '18 at 10:08
  • So sometimes, it is installing at the base path lets say 2.7 whereas u r using 3.6 or vice versa. Do check what version you are using and where are these packages getting installed – Rahul Agarwal Oct 09 '18 at 10:09
  • 2
    To add on @RahulAgarwal's comment, you might have run your first script with Python 2, but your second with Python 3 (or any other couple of different versions). A safer way to install a module through a script would be `subprocess.run(["python3", "-m", "pip", "install", "--user", "pandas"])`. – Right leg Oct 09 '18 at 10:09
  • @roganjosh This post advises to do so: https://stackoverflow.com/questions/12332975/installing-python-module-within-code; although the official doc says that `pip` is definitely not meant to be used that way. – Right leg Oct 09 '18 at 10:10
  • @Rightleg thanks for that, I was wondering why I hadn't encountered it before :) – roganjosh Oct 09 '18 at 10:12
  • pandas and numpy were properly installed in my python27, I don't know why it gives the error after running for the first time, the second time I run the code gives no ImportError –  Oct 09 '18 at 10:21

1 Answers1

0

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

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361