0

I want to install and use a library called Synthetic Interventions. I install it like:

pip3 install -e git+https://github.com/deshen24/Synthetic-Interventions#egg=SI --user

I then go to my source folder (in my case located at C:/Users/jgrea/src/), where you should now see a folder called si. I rename this directory to SI.

SI
│   SI.py    
│
└───src
│   │   cvxRegression.py

But then, when I run my code

import sys, os
sys.path.append("C:\\Users\\jgrea\\src")
sys.path.append(os.getcwd())
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from SI import SI

I get


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jgrea\src\SI\SI.py", line 4, in <module>
    from src.cvxRegression import ConvexRegression
ModuleNotFoundError: No module named 'src.cvxRegression'
(0 lines skipped)

I don't understand why I see this. When I look in the src folder in SI, I see a file named cvxRegression, the exact library SI asks for. So, why would this happen if we see this file in the folder in question? Perhaps, it's because there's no setup.py or init file?

  • The path separators look like they could be a problem. See: https://stackoverflow.com/questions/2953834/windows-path-in-python Also of course you can try not renaming this folder because that may be more trouble than its worth. – topsail Oct 02 '22 at 01:32
  • Okay I'll try this and then post back if it works, or if it doesn't. – Jared Greathouse Oct 02 '22 at 02:09
  • The path separators are fine, when I keep the file name the same, and I use all backslashes, I get the same error as described above. So if it isn't the separators or the file name, what might the next most likely issue be? – Jared Greathouse Oct 02 '22 at 04:18
  • I'm not sure what you mean by "using all backslashes". I was thinking more along the lines of using double backslashes, or forward slashes. In any case, another problem might be that you are missing a package called cvxRegression (a missing dependency problem) – topsail Oct 02 '22 at 13:55
  • The library `cvxRegression` is [found](https://github.com/deshen24/Synthetic-Interventions/tree/master/src) in the github repository, so the missing dependency shouldn't be the issue. @topsail – Jared Greathouse Oct 02 '22 at 14:05

2 Answers2

0

The issue is that you are importing this file from a different directory other than SI, and since the imports in the module are absolute rather than relative, and also the SI directory and src directory do not have an __init__.py, so they are not a module.

You can either change the scripts in order to perform relative imports, add the SI directory to your path or attempt running your script in the same directory as src and SI.py.

Divyessh
  • 2,540
  • 1
  • 7
  • 24
  • @JaredGreathouse You might be importing `from SI import SI` and using the it as a function/class `SI()` – Divyessh Oct 03 '22 at 07:31
  • The SI you're improting is the file SI.py, if you want to use a function named SI inside of it, you can do `SI.SI()` – Divyessh Oct 03 '22 at 07:31
0

After talking with the author, the issue here is poor packaging-- the code they wrote was mainly for them and not meant necessarily to be a fully loaded package for everyone to use. The solution, for whatever reason, in this case is to change your directory to the SI folder, in my case

os.chdir('C:\\Users\\jgrea\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\SI')