# Python HF Not Working

**URL:** <https://discuss.huggingface.co/t/python-hf-not-working/161237>\
**Category:** Beginners\
**Created:** [June 30, 2025, 9:31pm UTC](https://discuss.huggingface.co/t/python-hf-not-working/161237 "2025-06-30T21:31:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathancodes](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nathancodes/32/50358_2.png) [@nathancodes](https://discuss.huggingface.co/u/nathancodes)\
**Post date:** [June 30, 2025, 9:31pm UTC](https://discuss.huggingface.co/t/python-hf-not-working/161237/1 "2025-06-30T21:31:22Z")

</div>

I created an account on Hugging Face and made a token. I have saved that as a variable in my environment. I still can’t seem to get a basic connection to interact with a script. Any help will be very much appreciated

import requests  
import os

api\_key = os.getenv(‘HF\_TOKEN’) or os.getenv(‘HUGGINGFACE\_API\_KEY’)  
print(f"Using API key: {api\_key[:10]}…" if api\_key else “No API key found”)

url = “[https://api-inference.huggingface.co/models/distilgpt2](https://api-inference.huggingface.co/models/distilgpt2)”  
headers = {“Authorization”: f"Bearer {api\_key}"}

# Simple message

payload = {  
“inputs”: “Hello, how are you today?”,  
“options”: {“wait\_for\_model”: True}  
}

print(“Sending request…”)  
response = requests.post(url, headers=headers, json=payload)

print(f"Status: {response.status\_code}“)  
print(f"Response: {response.text}”)

if response.status\_code == 200:  
data = response.json()  
print(f"Generated text: {data}“)  
else:  
print(f"Error: {response.status\_code} - {response.text}”)

OUTPUT

Sending request…  
Status: 404  
Response: Not Found  
Error: 404 - Not Found

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [July 1, 2025, 12:12am UTC](https://discuss.huggingface.co/t/python-hf-not-working/161237/2 "2025-07-01T00:12:52Z")

</div>

It cannot be used via API unless it is a deployed model, and deployed models are occasionally changed. [That model does not appear to be deployed](https://huggingface.co/distilbert/distilgpt2).

> [@Persistent 404 Not Found Errors with Public Inference API](https://discuss.huggingface.co/t/persistent-404-not-found-errors-with-public-inference-api/154119):
>
> Problem: For the past day or so, all My attempts to make POST requests to the public Inference API endpoint result in a 404 Not Found error. This happens regardless of the model I try to query, including standard, known-available models like gpt2. The response body simply contains “Not Found"“ My Hugging Face Username: Mehdimemar Troubleshooting Steps Taken: Model Validity Confirmed: I’ve tested numerous valid model IDs (like gpt2, distilbert-base-uncased-fine tuned-sst-2-english, various …

These are [the models currently deployed with Hugging Face](https://huggingface.co/models?inference_provider=hf-inference&sort=trending).
