1

i am having file structure like

/home/ec2-user/wep-rs/WEPR/weprs/api/voucher.py
/home/ec2-user/wep-rs/WEPR/weprs/api/scrappers/quotes/quotes.py
i want to access voucher.py from quotes.py 

i have tried these

import sys
sys.path.append("..")# ValueError: attempted relative import beyond top-level package
from .. .. import api # ValueError: attempted relative import beyond top-level package
sys.path.append("/home/ec2-user/wep-rs/WEPR/weprs/api/")
from api.voucher import Voucher
error i am getting is 
ModuleNotFoundError: No module named 'api'
2
  • sys.path.append("/home/ec2-user/wep-rs/WEPR/weprs") maybe? Commented Jul 12, 2018 at 8:07
  • not working same error no module named api, sys.path.append("/home/ec2-user/wep-rs/WEPR/weprs") from api import voucher Commented Jul 12, 2018 at 8:10

1 Answer 1

2

You are at the right path, however..

It should be:

sys.path.append("/home/ec2-user/wep-rs/WEPR/weprs/api/")
from voucher import Voucher # or just import voucher

In the example I am showing you, from voucher import Voucher tries to import the Voucher class from /home/ec2-user/wep-rs/WEPR/weprs/api/voucher.py.

Otherwise, in your way, you're trying to access /home/ec2-user/wep-rs/WEPR/weprs/api/api/voucher.py.
Also, keep in mind that there must be an __init.py__ file in the directories.

Sign up to request clarification or add additional context in comments.

5 Comments

sys.path.append(/home/ec2-user/wep-rs/WEPR/weprs/api/api/voucher.py) is it again needed to import like from voucher import Voucher and creating the object for the class or directly we can access the functions under the class
If Voucher is a class defined in voucher.py, then you with import voucher you can access it with voucher.Voucher.method(), while with from voucher import Voucher you can access the functions via Voucher.method().
sys.path.append("/home/ec2-user/wep-rs/WEPR/weprs/api/voucher.py") from voucher import Voucher still i am getting ModuleNotFoundError: No module named 'voucher'
Why are you using sys.path.append("/home/ec2-user/wep-rs/WEPR/weprs/api/voucher.py")? I've explicitly told you in my answer, that it should be sys.path.append("/home/ec2-user/wep-rs/WEPR/weprs/api/") and from voucher import Voucher. Please copy/paste if you have to..
No problem. You can accept my answer if it has worked.

Your Answer

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.