1

I have a Python project having the following hierarchy:

- product_recommender_sys
 - data
 - dataset.csv
 - public
 - __init__.py
 - startup.py
 - src
 - __init__.py
 - recommender.py

I am trying to import the recommender.py module in startup.py. Following is the code:

import sys
sys.path.insert(0, '/home/user1/product_recommender_sys/src')
print sys.path
from product_recommender_sys.src import recommender
recommender.recommend()

I have included __init__.py file and added the respective folders to sys.path. Also, the same import statement works perfectly fine on the Python interpreter, but fails inside the script. How can I get the import to work inside the script?

Andrew T.
4,7078 gold badges48 silver badges69 bronze badges
asked Apr 3, 2015 at 18:06
6
  • Could you please format the tree better, it would make things a lot easier. Commented Apr 3, 2015 at 18:09
  • What if you use sys.path.insert(0, '/home/user1/product_recommender_sys') Since this is the root folder of the module. Commented Apr 3, 2015 at 18:12
  • @PaulRooney , I tried the root folder too. Doesnt make any difference.Throws the same error "ImportError: No module named product_recommender_sys.src" Commented Apr 3, 2015 at 18:24
  • Strange it fixed it for me. Commented Apr 3, 2015 at 18:28
  • Should I be using __init__.py file in both sub-directories? Commented Apr 3, 2015 at 18:29

1 Answer 1

0

I found the issue. The startup.py module was not getting compiled since it was not being imported.

First compiling the script solves the problem.

python -m compileall product_recommender_sys/public/startup.py
answered Apr 4, 2015 at 2:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.