Pytorch on replit

I love the ease of replit, however in one of my projects I ran into the problem that I cannot run a python library.

I am using the sentence_transformers library which is using PyTorch and replit requires the CPU only version. I do not know how to implement this in replit since dependencies are installed automatically and there is no requirements.txt file.

Looking forward to hearing back from you and getting my script up running! :slight_smile:

1 Like

Hello, could you provide some test code that would verify the package is working correctly?

Or, try this installation method:
Create a new python repl. Click three dots near files and click Show hidden files.
Then, create a file named replit.nix
Paste this code into it:

{ pkgs }: {
  deps = [

Then, open .replit file and insert this line anywhere near the top:

disableGuessImports = true

Then, reload and see if it works.

1 Like

What if I want to use torch and a package that needs to be installed with pip?

I have the replit.nix file as this and it can’t load any of the files normally installed with pip

{ pkgs }: {
  deps = [

try removing the pkgs.python39Packages.pip from your replit.nix file.
To install a python package, you can use poetry add packageName. (It is not recommended to use pip directly on replit.) If the package depends on torch, it should still work fine with this setup.

If you are getting a package installation error, then this means that you trying to install pip yourself may have corrupted your repl, and the easiest thing to do would be to move everything to a new repl and try again (but without pip).

By “it can’t load any of the files normally installed with pip”, can you be more specific? What packages are you using? If you are having problems, you should also provide a link to your repl (and any error messages).

I don’t need the sentence_transformers library specifically, just PyTorch, but I figured that doesn’t make a huge difference. If I do the changes you recommended torch can be imported in, but then I can import plotly which is the one of the “files normally installed with pip”

Here’s the link if this helps:

Ah, I see.
You can replace sentence-transformers in replit.nix with torchvision.
Install your python with poetry add plotly. If the package does not work, there are two possibilities:

  1. I have not verified that the pytorch package works on replit, only that it can be installed.
  2. If plotly says it is missing dependencies, then you must install it with a different method (ask if this is the case).
1 Like

I get a ModuleNotFoundError when using import as px. When I run poetry add plotly, i get a Syntax Error on add

Thank you for the info. I did some debugging and managed to fix the import problems.
Open the .replit file, then replace it with this, then reload of course:

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

hidden = [".pythonlibs"]

disableGuessImports = true


channel = "stable-23_05"

language = "python3"

run = ["python3", ""]
deploymentTarget = "cloudrun"

(an [env] section is added)

Now, all of your packages should import fine. (Also, could you verify that all of the packages work correctly?)

Basically, the problem was that python was not able to find your python packages because the directory containing the packages was not in PYTHONPATH (sys.path). (I am not entirely sure why.) The changes to the .replit file append the package directory to the list of paths. Some packages such as numpy and torch are found in a separate place, the nix store, so they always imported just fine. (Therefore, there are redundant packages in the python package directory.) Nix store packages must take priority over the python package directory because of non-python dependency problems. Nix packages are not recognized by the python package manager, so import guessing must be disabled, and torch is not in your pyproject.toml dependencies. Even if poetry must install some of these packages (for dependency resolving), the nix package version will be used instead so nothing breaks. Note that nix packages take no account storage while poetry python packages do.