Search code examples
pythonnext.jsvercel

ModuleNotFoundError no module named 'data', Vercel python


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

Solution

  • 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.