错误:404 Not Found 抱歉,请求的 URL 'http://localhost:8000/main.html' 导致错误:文件不存在

[enter image description here ](https://i.stack.imgur.com/iompu.png)everythink一切都很好,但是当我完成代码并尝试运行时,我遇到了这个错误,您认为会发生什么情况。我使用Linux薄荷。问题是我认为,当我运行HTML代码并转到控制台时,我可以看到 GET file:///eel.js net::ERR_FILE_NOT_FOUND 我已经解决了问题,但我找不到答案。希望您能帮助我。Python代码:

import eel
import pyowm

own = pyowm.OWM('d845c8640738fe82515cb1277bb3dd38')

eel.init('web')

eel.start('main.html', size=(700, 700))

@eel.expose
def get_weather(city):

    mgr = own.weather_manager()

    observation = mgr.weather_at_place(city)
    w = observation.weather 

    temp = w.temperature('celsius')['temp']

    return city + ' ' +  str(temp)

和HTML/JS代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Weather</title>

    <script type="text/javascript" src="/eel.js"></script>
    <link rel="stylesheet" type="image/png" href="weather-icon.png">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

    <input id="location" type="text" placeholder="Write the name of your Country ot City" required="" value="Bukhara, Uzbekistan">

    <button id ="show">Know the weather</button>

    <div id="info">

    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript">

        async function display_weather(){
            let place = document.getElementById('location').value;

            let res = await eel.get_weather(place)();
            document.getElementById('info').innerHTML = res;
        }

        jQuery('#show').on('click', function(){
            display_weather();
        });

    </script>

</body>
</html>

在我询问并指出仅在浏览器的控制台中看到的第二个错误之前,我找不到ASNWER。谢谢

stack overflow Error: 404 Not Found Sorry, the requested URL 'http://localhost:8000/main.html' caused an error: File does not exist
原文答案

答案:

作者头像

刚刚面对这个问题,我用它来以一种更通用的方式解决:

import os

eel.init(f'{os.path.dirname(os.path.realpath(__file__))}/web')