venkatesh
venkatesh

Reputation: 141

directory file import error in python

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'

Upvotes: 1

Views: 82

Answers (1)

Fanatique
Fanatique

Reputation: 428

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.

Upvotes: 2

Related Questions