Problem Description:
I am working on a Python project with the following structure:
Scriptus
├── frontend
│ ├── hf_call.py
│ ├── slide_deck.py
│ ├── slide_gen.py
│ └── ui.py
├── hf_service
│ ├── consts.py
│ └── hf_model.py
├── generated/
├── .env
├── .gitignore
├── .gitattributes
├── README.md
└── requirements.txt
Issue 1: When running frontend/hf_call.py directly, I encounter:
ModuleNotFoundError: No module named 'hf_service'
Issue 2: When running the script as a module with python -m frontend.hf_call, I get:
SyntaxError: source code string cannot contain null bytes
Code Details:
hf_service/consts.py:
HF_API_URL = "https://api-inference.huggingface.co/models/"
MODEL_NAME = "meta-llama/Meta-Llama-3.1-8B-Instruct"
hf_service/hf_model.py:
import os
import requests
from dotenv import load_dotenv
from consts import HF_API_URL, MODEL_NAME
load_dotenv()
HF_API_KEY = os.getenv('HF_API_KEY')
def generate_text(input_text: str) -> str:
api_url = f"{HF_API_URL}/{MODEL_NAME}"
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
payload = {"inputs": input_text}
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status()
return response.json().get('generated_text', '')
if __name__ == "__main__":
input_text = "Hello, world!"
print(generate_text(input_text))
frontend/hf_call.py:
import requests
import os
from dotenv import load_dotenv
from hf_service.consts import HF_API_URL, MODEL_NAME
load_dotenv()
HF_API_KEY = os.getenv('HF_API_KEY')
def generate_text(input_text: str) -> str:
api_url = f"{HF_API_URL}/{MODEL_NAME}"
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
payload = {"inputs": input_text}
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status()
return response.json().get('generated_text', '')
if __name__ == "__main__":
input_text = "Hello, world!"
print(generate_text(input_text))
.env:
HF_API_KEY=your_huggingface_api_key_here
What I Tried:
Running frontend/hf_call.py Directly:
- Command: python frontend/hf_call.py
- Issue Encountered: ModuleNotFoundError: No module named 'hf_service'
Running the Script as a Module:
- Command: python -m frontend.hf_call
- Issue Encountered: SyntaxError: source code string cannot contain null bytes
Checked and Updated Code Files:
Verified and updated code in hf_service/consts.py, hf_service/hf_model.py, and frontend/hf_call.py.
Confirmed Presence of .env File:
Ensured .env file exists with the correct API key.
What I Expected:
For Issue 1: I expected to be able to run frontend/hf_call.py without encountering import errors by correctly configuring the module imports.
For Issue 2: I expected the script to run without syntax errors when using python -m frontend.hf_call, following the correct module execution process.
Summary: Help resolving the ModuleNotFoundError and SyntaxError issues to ensure proper execution of the scripts and functionality of the project.
Scriptusto be on the module search path, and it's not when you executepython frontend/hf_call.py(becauseScriptus/frontend, notScriptusitself, is automatically added to the path). As a general rule, do not put scripts in directories intended to define packages.consts.py, for example, I can get the syntax error with the second call. The first call fails to import the module in the first place, so never sees the syntax error.