Scientific Computing Arithmetic Formatter Project error: no module named pytest

Hello everyone, I would really appreciate any and all help with this as I am completely new to Python and don’t understand everything quite as well. I am working on my arithmetic formatter project but when I click run to check whether I have done it correctly, I keep getting the following error:

          Traceback (most recent call last):
            File "main.py", line 2, in <module>
              from pytest import main
          ModuleNotFoundError: No module named 'pytest'

Here is my code too if you would like to have a look: arithmetic - Replit

Thank you so much!

First of all, what you’re seeing is not caused by any of the code you’ve written.

You’ll notice that the error message mentions “line 2”. This is where you’ll find the problematic code:

from pytest import main

When you ‘run’ your code, line 2 asks the Python interpreter to get some code from a module called ‘pytest’, which is stored in a file called pytest.py. In short, the error message is saying the interpreter cannot find ‘pytest.py’.

Usually, the most basic way to solve this kind of error is to install the module you want using pip from the command line:

$pip install some_module

In this case, this won’t work. I don’t understand the reasons well enough to give you a good answer with confidence yet, but I will get back to you. I had the same issue as you a few minutes ago. I was looking for a solution when I stumbled across your post.

I am surprised that it happen because one would expect the coding environment to be setup to work ‘out of the box’, especially for a course aimed at beginners :confused:

This thread covers your problem (but with modules like numpy and pandas, instead of pytest). In your case, the key difference is that the line you need to add is pkgs.python38Packages.pytest. Once done, your replit.nix file should look like this:

{ pkgs }: {
  deps = [
    pkgs.python38Full
    pkgs.python38Packages.pytest
  ];
  env = {
    PYTHON_LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
      # Needed for pandas / numpy
      pkgs.stdenv.cc.cc.lib
      pkgs.zlib
      # Needed for pygame
      pkgs.glib
      # Needed for matplotlib
      pkgs.xorg.libX11
    ];
    PYTHONBIN = "${pkgs.python38Full}/bin/python3.8";
    LANG = "en_US.UTF-8";
  };
}