Search code examples
python-3.xpandasnumpypipsys.path

Why does command prompt import differ from sublime text import?


I have installed with pip several packages (numpy/pandas/blpapi/pyarrow). I work with a Windows 64-bit machine, python3.6 in a sublime environment. While all packages are shown as correctly imported in the command prompt, some packages are not found by my sublime scripts.

To try and remedy this problem, I used sys.path.insert and changed the names of my scripts, to no avail. The traceback below describes what I'm seeing:

Code in Command Prompt:

>>> import pyarrow
>>> import pandas
>>>

Code in Sublime (better_name.py):

print('Hi')
import numpy
import pandas

Output of better_name.py:

Hi
Traceback (most recent call last):
  File "C:\Users\Documents\better_name.py", line 4, in <module>
    import pandas
ModuleNotFoundError: No module named 'pandas'

Obtaining the paths in Command Prompt:

>>> import os
>>> import numpy
>>> path = os.path.dirname(numpy.__file__)
>>> print(path)
C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy
>>> import pandas
>>> path = os.path.dirname(pandas.__file__)
>>> print(path)
C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas

Trying to use sys.path.insert :

print('Hi')
import sys
import numpy
import os 

sys.path.insert(1, r"C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas")

Output:

C:\Users\Documents>better_name.py
Hi
Traceback (most recent call last):
  File "C:\Users\Documents\better_name.py", line 7, in <module>
    import pandas
ModuleNotFoundError: No module named 'pandas'

I get the same results whether I change the argument in sys.path.insert to 0.


Solution

  • The issue seems to be that your default version of python points to the 32-bit version - i.e. when you say python your windows system executes the 32 bit version.

    One workaround is to specify the full path of your 64 bit version - i.e. launch your script as

    C:\PATH\TO\64-BIT-VERSION\PYTHON.EXE your_script.py 
    

    from the command line.

    The other option is to set your windows environment variables to point to the 64 bit version by default. This link should help