No module openai found, even though it is installed

Question:
This error suddenly started occurring today with no obvious change in my code to cause it. When I deploy, I get an error with the openai package. However, the package is installed - I even uninstalled it and reinstalled. Can confirm it is present in the Repl. Repl runs with no issue in the browser, just the deployment has the below error:

import functools, os, cv2, openai, urllib.request, random, time
ModuleNotFoundError: No module named 'openai'
starting metadata sidecar...
starting up user application
Traceback (most recent call last):
ModuleNotFoundError: No module named 'openai'
forwarding local port 8080 to external port 1104
    import functools, os, cv2, openai, urllib.request, random, time
exec [python3 main.py] error=exec: already started
  File "/home/runner/55b75cf8-bc12-4c45-b813-440a72d69555/main.py", line 1, in <module>
exec [python3 main.py] error=exit status 1
ModuleNotFoundError: No module named 'openai'
starting metadata sidecar...
exec [python3 main.py] error=exit status 1
  File "/home/runner/55b75cf8-bc12-4c45-b813-440a72d69555/main.py", line 1, in <module>
starting up user application
    import functools, os, cv2, openai, urllib.request, random, time
forwarding local port 8080 to external port 1104
exec [python3 main.py] error=exec: already started
Traceback (most recent call last):
ModuleNotFoundError: No module named 'openai'
starting up user application
    import functools, os, cv2, openai, urllib.request, random, time
  File "/home/runner/55b75cf8-bc12-4c45-b813-440a72d69555/main.py", line 1, in <module>
exec [python3 main.py] error=exit status 1
Traceback (most recent call last):
exec [python3 main.py] error=exit status 1
forwarding local port 8080 to external port 1104
starting metadata sidecar...
exec [python3 main.py] error=exec: already started
    import functools, os, cv2, openai, urllib.request, random, time
starting up user application
Traceback (most recent call last):
  File "/home/runner/55b75cf8-bc12-4c45-b813-440a72d69555/main.py", line 1, in <module>
ModuleNotFoundError: No module named 'openai'

As per suggestion from @NuclearPasta0 with similar bugs on deployment, I tried changing my .replit file nix channel from “stable-23_11” to “stable-23_05”, but that did not work this time. Copying my .replit file and replit.nix below in case there are any issues:

entrypoint = "main.py"
modules = ["python-3.10:v18-20230807-322e88b"]

hidden = [".pythonlibs"]

disableGuessImports = true
disableInstallBeforeRun = true

[nix]
channel = "stable-23_11"

[deployment]
run = ["python3", "main.py"]
deploymentTarget = "gce"

[[ports]]
localPort = 8080
externalPort = 80
{ pkgs }: {
  deps = [
    pkgs.imagemagickBig
    pkgs.ffmpeg-full
    pkgs.python310Packages.opencv4
    pkgs.imagemagick
    pkgs.ffmpeg_6-full
  ];
  env = {
    IMAGEIO_FFMPEG_EXE = "${pkgs.ffmpeg_6-full}/bin/ffmpeg";
  };
}```

Hi @flixr !
Try running poetry update openai then poetry add openai.

@NateDhaliwal unfortunately still get the same error. Nothing was changed on my end prior to this so wondering if it could be something Replit have changed at the system-level…

Across other Repls I have the same issue and see warnings on many import statements, eg

Import "supabase" could not be resolved

I cannot exactly figure out the problem without an example repl, but here is how you can debug the issue:

First, you have installed some packages. The problem is that python cannot find them. The type checker saying “could not be resolved” also indicates that python cannot find them.

Check your folders to make sure that the packages are there. For python repls, usually inside .pythonlibs/lib/python3.10/site-packages .

Now, whenever you do import package, python does a very simple process to find package. There is a list of paths to directories that will be searched in. This list of paths is PYTHONPATH env variable in the shell, and available in python via the sys.path variable.
Check that the directory that contains your package is indeed listed in sys.path right before the ModuleNotFoundError occurs.

It likely isn’t.
To add the .pythonlibs path to PYTHONPATH, put this in .replit file:

[env]
PYTHONPATH = "$PYTHONPATH:$REPL_HOME/.pythonlibs/lib/python3.10/site-packages"

Or, append to sys.path .
The PYTHONPATH is supposed to include that path by default, but certain nix packages may mess it up. Replit has to mess with this often to configure repls so replit might have done something, especially with complicated deployments.