0

I'm getting the error in the title when running a .py script. I have pandas installed (version 0.24.2) as you can see in the screenshot. The Python version is 3.7.3. When I run the command import pandas as pd in a Jupyter notebook it works. However, when I have the same command in a .py file ("MyFile.py") and try to run that file using Anaconda command prompt I get this error. Same happens when using the Windows command prompt. Can someone please advise on how to solve this?enter image description here

enter image description here

The 2nd screenshot above shows that I can't find a folder for pandas in site-packages but anaconda seems to think it's installed.

Chipmunk_da
  • 467
  • 2
  • 9
  • 27
  • 1
    You say Python 3.7.3 but your directory says `Python2.7`? – yvesonline Mar 27 '21 at 12:50
  • Yes, the directory name is a bit misleading but the Python version installed is as per the screenshot from Jupyter notebook – Chipmunk_da Mar 27 '21 at 12:52
  • 1
    You could go further into Lib/site-packages and see if pandas is there, but yes, the directory mismatch is a bit confusing. – dm2 Mar 27 '21 at 12:52
  • How did you install `pandas`? – yvesonline Mar 27 '21 at 12:53
  • @yvesonline I don't quite remember as I installed it a few months ago and never used it until today. But to @dm2 's point, when I just checked the lib\site-packages folder I can't find a folder for pandas. Though when now try to install pandas using Anaconda prompt it says `requirement already satified`. I've included another screenshot in the question to show site-packages folder and output from Anaconda prompt. – Chipmunk_da Mar 27 '21 at 13:09
  • Do you have multiple Python versions installed? What happens when you try `pip3 install pandas`? – yvesonline Mar 27 '21 at 13:14
  • I might have multiple python versions - how do I check that? When I just ran `pip3 install pandas` I got an error - `pip3 is not recognised as an internal or external command, operable program or batch file`. – Chipmunk_da Mar 27 '21 at 13:16

3 Answers3

1

have you tried python3.7 MyFile.py?

If not, I would try it. Then if it doesn't work I'd create a new environment with python 3.7, and then install pandas and run your code:

conda create -n test_env python=3.7 conda activate test_env pip install pandas python MyFile.py

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • But there's no `.exe` called `python3.7`. So when I run `"C:\python27\python3.7.exe" MyFile.py` it fails saying `'"C:\python27\python3.7.exe"' is not recognised as an internal or external command, operable program or batch file. – Chipmunk_da Mar 27 '21 at 13:21
  • good. Then you could start by creating another env with python 3.7, then switching to the new env, installing pandas and executing the code. it should work. – Marcelo Ventura Mar 27 '21 at 14:45
1

Probably you have installed Python and afterwards Anaconda.

  • AnaConda has its own python exe and Libs that are automatically installed. They're probably located in (Windows path): C:\ProgramData\AnacondaX\
  • So, if you go to C:\ProgramData\AnacondaX\Lib\site-packages\ you will see a folder "pandas". It means that Pandas lib is installed for Anaconda exclusively.
  • On the other hand, you have installed Python in c:\Python27. I'd advise you firstly add to %PATH% environment variable to get access to Python from any folder you're in and after please check the libs you have installed in your Python (not Anaconda) with (pandas is not listed there): c:>pip freeze
  • Next step is to install pandas for your Python installation. Just do it: c:>pip install pandas
  • Thanks @Ricardo. Since I'm working on a virtual machine it seems like I don't have permission to edit the system environment variables. I can only edit the user environment variables where I have now added `%PATH%` as `C:\Python27`. Doesn't look like it works. When I write `pip install pandas` I get an error saying pip is not recognised. – Chipmunk_da Mar 27 '21 at 13:43
1

Anaconda uses its own python installation, which by default, and as per this documentation, should be under C:\Users\<your-username>\Anaconda3\.

By installing pandas via Anaconda, and then specifically running the script with a different python installation, you're running a script using a library within environment that does not have that library installed.

You can try installing pandas to this separate python installation (I saw your pip install didn't work, you could again specify this python installation and use pip as a module):

'C:\python27\python.exe' -m pip install pandas

Or run the script with the Anaconda python installation by specifying that python installation:

{path_to_anaconda_python} MyFile.py

Where path_to_anaconda_python is path to Anaconda python installation (by default, within C:\Users\<your-username>\Anaconda3\)

dm2
  • 4,053
  • 3
  • 17
  • 28
  • Thanks @dm2. I tried your suggestion and this seems to have installed Pandas correctly. I can now see it in `C\:Python27\Lib\site-packages`. However, when I try to now execute `MyFile.py` as per the original screenshot, I get `ImportError: DLL load failed: %1 is not a valid Win32 application.` – Chipmunk_da Mar 27 '21 at 14:27
  • @Chipmunk_da this is a bit out of my knowledge (virtual machine with no ability to edit environmental variables, with anaconda and standalone python that is in 2.7 folder but is actually 3.7), but I encourage you to explore related question on stackoverflow: https://stackoverflow.com/questions/19019720/importerror-dll-load-failed-1-is-not-a-valid-win32-application-but-the-dlls – dm2 Mar 27 '21 at 14:32