2

When I try to run a script in conda environment it gives me a ModuleNotFound error

(ldm) C:\Users\Иван\Documents\git\stable-diffusion>python scripts/txt2img.py --prompt "a photograph of an astronaut in space" --plms 
Traceback (most recent call last):
  File "scripts/txt2img.py", line 17, in <module>
    from ldm.util import instantiate_from_config 
ModuleNotFoundError: No module named 'ldm'

Here are the txt2img.py's lines causing the trouble

from ldm.util import instantiate_from_config
from ldm.models.diffusion.ddim import DDIMSampler
from ldm.models.diffusion.plms import PLMSSampler

But the main points are:

  1. There is a folder called "ldm" containing everything needed
  2. There are no complains from the compiler in the actual code editor

What I'm sure of:

  • The txt2img.py's directory is C:\Users\Иван\Documents\git\stable-diffusion\scripts\txt2img.py and the ldm's directory is C:\Users\Иван\Documents\git\stable-diffusion\ldm
  • Anaconda is added to my PATH environment variable
  • I'm trying to make it work in VSCode terminal (I set it up to run conda) but it's absolutely the same in the separate Anaconda Prompt
  • The problem does not occur with other projects

Is there a way to fix this?

Ivanes1
  • 31
  • 5

2 Answers2

1

The problem got resolved with typing in the following:

pip install -e .

I think it just installs all the packages from the project folder

Ivanes1
  • 31
  • 5
0

As a quick solution try the following:

cd C:\Users\Иван\Documents\git\stable-diffusion

C:\Users\Иван\Documents\git\stable-diffusion> python scripts\txt2img.py --prompt "a photograph of an astronaut in space" --plms

In VSCode, do the following:

  1. Add 'C:\Users\Иван\Documents\git\stable-diffusion' to your workspace
  2. Open VSCode Terminal (ctrl + ~)
  3. Follow the steps from here
  4. Run your script
python scripts\txt2img.py --prompt "a photograph of an astronaut in space" --plms

Update based on comment:

You need to add an empty __init__.py inside all your folders. Refer here

Jeril
  • 7,858
  • 3
  • 52
  • 69
  • I already selected my virtual environment, which is also called "ldm", as the workspace's interpreter. That's why there are no compains in the code editor. And actually it doesn't complain about any packages when I run the script, the only thing it doesn't see is the folder. And of course I've already cd'ed into the base folder – Ivanes1 Sep 01 '22 at 09:08
  • Updated my solution, kindly check – Jeril Sep 01 '22 at 09:15
  • Tried it out and unfortunately it's all the same. My conda's python version is 3.8.5 so based on the info you provided, I don't need the __init__.py, as it's no longer requiered in 3.3+ versions of python – Ivanes1 Sep 01 '22 at 09:36
  • can you add these 2 lines at the top of txt2img.py script. `import os print(os.getcwd())` and show the output – Jeril Sep 02 '22 at 08:54
  • Just solved this problem – Ivanes1 Sep 02 '22 at 09:35