1

I have attempted to follow the advise found here: https://stackoverflow.com/a/58040520/3403085

but I am still getting an error. See my code below:

import subprocess
import sys

try:
        import pandas
except ImportError:
        subprocess.call([sys.executable, "-m", "pip", "install", 'pandas'])
finally:
        import pandas
print(pandas.__version__)

Here is the output when I run the command:

Collecting pandas
  Downloading https://files.pythonhosted.org/packages/63/e0/a1b39cdcb2c391f087a1538bc8a6d62a82d0439693192aef541d7b123769/pandas-0.25.3-cp37-cp37m-manylinux1_x86_64.whl (10.4MB)
    100% |████████████████████████████████| 10.4MB 13.8MB/s
Collecting python-dateutil>=2.6.1 (from pandas)
  Downloading https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227kB)
    100% |████████████████████████████████| 235kB 13.9MB/s
Collecting pytz>=2017.2 (from pandas)
  Downloading https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl (509kB)
    100% |████████████████████████████████| 512kB 11.7MB/s
Collecting numpy>=1.13.3 (from pandas)
  Downloading https://files.pythonhosted.org/packages/9b/af/4fc72f9d38e43b092e91e5b8cb9956d25b2e3ff8c75aed95df5569e4734e/numpy-1.17.4-cp37-cp37m-manylinux1_x86_64.whl (20.0MB)
    100% |████████████████████████████████| 20.0MB 14.1MB/s
Collecting six>=1.5 (from python-dateutil>=2.6.1->pandas)
  Downloading https://files.pythonhosted.org/packages/65/26/32b8464df2a97e6dd1b656ed26b2c194606c16fe163c695a992b36c11cdf/six-1.13.0-py2.py3-none-any.whl
Installing collected packages: six, python-dateutil, pytz, numpy, pandas
Successfully installed numpy-1.17.4 pandas-0.25.3 python-dateutil-2.8.1 pytz-2019.3 six-1.13.0
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    import pandas
ModuleNotFoundError: No module named 'pandas'

If I run the script a second time, the pandas module gets imported successfully, without re-installing, and I get the following output:

0.25.3

I can also uninstall by running python3 -m pip uninstall pandas so I can confirm that it is indeed installed. Any ideas why it fails if pandas isn't already installed?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jrod091
  • 51
  • 7
  • aftr installing module you need to close and reopen that window – ravishankar chavare Dec 14 '19 at 04:30
  • 1
    Thanks for that response. Is there any way to check if modules are installed, install them if not, and then load it and continue with the script without having to re-run the script? I was under the impression that's what the looked answer was doing... :-( – jrod091 Dec 14 '19 at 12:56

1 Answers1

0

The solution is in another answer in that thread:https://stackoverflow.com/a/24773951/6417038

finally:
 globals()[package] = importlib.import_module(package)
Gazala Muhamed
  • 2,131
  • 1
  • 11
  • 8