ModuleNotFoundError no module named 'data', Vercel python

Question:

I have data (data.py) which is file that contins constants. When I try to import the file which is in the same folder ‘/api’ it says that no module named ‘data’

I have it the same casing, and i dont understand what could be the reason why it would happen.

Any help would be appericated as I am not great with python

   from data import *
   import numpy as np
   from http.server import BaseHTTPRequestHandler
   from urllib.parse import urlparse, parse_qs
   import json


class handler(BaseHTTPRequestHandler):

def do_GET(self):
    self.send_response(200)
    self.send_header('Content-type', 'text/plain')
    self.end_headers()

    result = urlparse(self.path)

    query_params = json.dumps(parse_qs(result.query))

    print('result', query_params)

    self.wfile.write('Hello, world!'.encode('utf-8'))

    return
Asked By: beginnersLuck.css

||

Answers:

If you’re trying to import a module in the same directory, you can use a relative import instead of an absolute import.

For example, if your directory structure looks like this:

- api/
  - data.py
  - main.py

You can import data.py into main.py using the relative import syntax:

from .data import *

The leading ‘.’ tells Python to look for data.py in the same directory as main.py.

Answered By: ismaael17

you have to create an empty file in the same directory with the name __init__.py to tell python that it’s okay to import from there.

After that import your file:

from .data import *
Answered By: tfk
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.