1

I'm unable to import pandas with import pandas as pd on replit. I've already installed the package with pip install pandas and it can be seen in packages. I've successfully imported it to other projects on replit. Every time I try importing it into my code on this project, it gives me the following error:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import pandas as pd
  File "/home/runner/thing/venv/lib/python3.8/site-packages/pandas/__init__.py", line 16, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/home/runner/thing/venv/bin/python"
  * The NumPy version is: "1.22.2"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: libz.so.1: cannot open shared object file: No such file or directory
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
realhuman
  • 137
  • 1
  • 8
  • Because repl.it is based on Nix, you can ask it to give you a Python interpreter with anything you want -- including pandas -- preinstalled by writing the appropriate expression telling it how to find that in nixpkgs. – Charles Duffy Feb 10 '22 at 03:00
  • This doesn't just work for Python, by the way. You can tell Nix to install compiled C software; NPM modules for JavaScript, CPAN libraries for Perl, and much else; it effectively gives you a single, unified, cross-language tool with the power of Python's `virtualenv`. – Charles Duffy Feb 10 '22 at 03:21
  • ...if you want to see a REPL where the steps described in my answer have already been completed, take a look at https://replit.com/@CharlesDuffy2/LastingOldlaceWatchdog – Charles Duffy Feb 10 '22 at 03:23
  • (I wonder if the "other projects on replit" you refer to where the `pip` approach worked are from before they switched to Nix). – Charles Duffy Feb 10 '22 at 03:24

1 Answers1

1

You don't need to use pip to install packages on repl.it -- and in fact, you shouldn't! Using Nix derivations not only works better (as you're using their OS distro the way it's designed), but also keeps their storage costs low, by allowing packages to be used from a read-only, hash-addressed, shared store.

Binaries built for other distributions might assume that there will be libraries in /lib, /usr/lib, or the like, but that's not how NixOS works: Libraries will be in a path like /nix/store/<hash>-<packagename>-<version>/lib, and those paths get embedded into the executables that use those libraries.

The easiest thing to do here is to create a new bash repl, but to add a Python interpreter to it. (I suggest this instead of using a Python repl because the way they have their Python REPLs set up adds a bunch of extra tools that need to be reconfigured; a bash repl keeps it simple).

  • Create a new bash repl.
  • Click on the three-dots menu.
  • Select "Show Hidden Files".
  • Open the file named replit.nix
  • Edit the file by adding a Python interpreter with pandas, as follows:
    { pkgs }: {
        deps = [
            pkgs.bashInteractive
            (pkgs.python38.withPackages (p: [p.pandas]))
        ];
    }
    
    ...changing that to taste (as long as they're getting software from a channel that has binaries for Python 3.9 or 3.10, for example, you can change python38 to python39 or python310).
  • Click the "run" button
  • In the new shell that opens, run python, and see that you can import pandas without trouble.

If, after you add a Python file to your repl, you can also change the .replit hidden file to make it run that file automatically on startup. Note that on NixOS, you should use #!/usr/bin/env python as your shebang, as PATH lookups are essential.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441