Python Module not Found from within Github Download

Question:

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:UsersjgreasrcSISI.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?

Asked By: Jared Greathouse

||

Answers:

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.

Answered By: Divyessh

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')
Answered By: Jared Greathouse
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.