Python 名称错误:名称';自我';尝试创建模块时未定义

Python 名称错误:名称';自我';尝试创建模块时未定义,python,Python,我有两个文件 检查.py import UrlParsing as owm API_key = 'private_API_Key' obj1 = owm.OWM(API_key) cd = obj1.three_hours_forecast("London, uk") print(cd) import urllib class OWM(object): def __init__(self, API_key): self.API_key = API_key

我有两个文件

检查.py

import UrlParsing as owm

API_key = 'private_API_Key'

obj1 = owm.OWM(API_key)
cd = obj1.three_hours_forecast("London, uk")
print(cd)
import urllib

class OWM(object):
    def __init__(self, API_key):
        self.API_key = API_key

    def three_hours_forecast(city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary

    def get_dict(self, api_url):
        url = urllib.urlopen(api_url)
        output_data = url.read().decode('utf-8')
        data_dict = json.loads(output_data)
        url.close()
        print(data_dict)
        return data_dict
cd = obj1.three_hours_forecast("London", "uk")
def three_hours_forecast(self, city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary
UrlParsing.py

import UrlParsing as owm

API_key = 'private_API_Key'

obj1 = owm.OWM(API_key)
cd = obj1.three_hours_forecast("London, uk")
print(cd)
import urllib

class OWM(object):
    def __init__(self, API_key):
        self.API_key = API_key

    def three_hours_forecast(city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary

    def get_dict(self, api_url):
        url = urllib.urlopen(api_url)
        output_data = url.read().decode('utf-8')
        data_dict = json.loads(output_data)
        url.close()
        print(data_dict)
        return data_dict
cd = obj1.three_hours_forecast("London", "uk")
def three_hours_forecast(self, city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary
我正在尝试创建一个UrlParsing模块,以便在主程序中使用它。谁能告诉我这个程序有什么问题吗。我收到名称错误:未定义名称“self”

我做错了什么?你能告诉我怎么改正吗。我试着对同样的问题做一些跟进,但没有成功。谢谢你的帮助

错误跟踪到这一行

        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
你可以试试

检查.py

import UrlParsing as owm

API_key = 'private_API_Key'

obj1 = owm.OWM(API_key)
cd = obj1.three_hours_forecast("London, uk")
print(cd)
import urllib

class OWM(object):
    def __init__(self, API_key):
        self.API_key = API_key

    def three_hours_forecast(city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary

    def get_dict(self, api_url):
        url = urllib.urlopen(api_url)
        output_data = url.read().decode('utf-8')
        data_dict = json.loads(output_data)
        url.close()
        print(data_dict)
        return data_dict
cd = obj1.three_hours_forecast("London", "uk")
def three_hours_forecast(self, city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary
UrlParsing.py

import UrlParsing as owm

API_key = 'private_API_Key'

obj1 = owm.OWM(API_key)
cd = obj1.three_hours_forecast("London, uk")
print(cd)
import urllib

class OWM(object):
    def __init__(self, API_key):
        self.API_key = API_key

    def three_hours_forecast(city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary

    def get_dict(self, api_url):
        url = urllib.urlopen(api_url)
        output_data = url.read().decode('utf-8')
        data_dict = json.loads(output_data)
        url.close()
        print(data_dict)
        return data_dict
cd = obj1.three_hours_forecast("London", "uk")
def three_hours_forecast(self, city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary
我希望这对你有帮助

你可以试试

检查.py

import UrlParsing as owm

API_key = 'private_API_Key'

obj1 = owm.OWM(API_key)
cd = obj1.three_hours_forecast("London, uk")
print(cd)
import urllib

class OWM(object):
    def __init__(self, API_key):
        self.API_key = API_key

    def three_hours_forecast(city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary

    def get_dict(self, api_url):
        url = urllib.urlopen(api_url)
        output_data = url.read().decode('utf-8')
        data_dict = json.loads(output_data)
        url.close()
        print(data_dict)
        return data_dict
cd = obj1.three_hours_forecast("London", "uk")
def three_hours_forecast(self, city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary
UrlParsing.py

import UrlParsing as owm

API_key = 'private_API_Key'

obj1 = owm.OWM(API_key)
cd = obj1.three_hours_forecast("London, uk")
print(cd)
import urllib

class OWM(object):
    def __init__(self, API_key):
        self.API_key = API_key

    def three_hours_forecast(city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary

    def get_dict(self, api_url):
        url = urllib.urlopen(api_url)
        output_data = url.read().decode('utf-8')
        data_dict = json.loads(output_data)
        url.close()
        print(data_dict)
        return data_dict
cd = obj1.three_hours_forecast("London", "uk")
def three_hours_forecast(self, city, country):
        cty = city
        api_url = "http://api.openweathermap.org/data/2.5/forecast?q={},{}&mode=json&units=metric&APPID={}".format(cty, country, self.API_key)
        data_dictionary = get_dict(api_url)
        return data_dictionary

我希望这对你有帮助

您没有将
self
作为
three\u hours\u forecast
的第一个参数传入
three\u hours\u forecast
中,您忘记了
self
参数,你忘了把self.放在函数中的get_dict.调用中。当我尝试这样做时,它是说很少有参数被称为抛出错误。你是否尝试过定义三小时预测(self,city,country)?在这行:
cd=obj1.三小时预测(“伦敦,英国”)
您在同一参数中传递的是城市和国家。。尝试:
cd=obj1.三小时预测(“伦敦”、“英国”)
您没有将
self
作为
三小时预测的第一个参数传递给
self
您忘记了
三小时预测中的
self
参数,你忘了把self.
放在函数中的get_dict.
调用中。当我尝试这样做时,它是说很少有参数被称为抛出错误。你是否尝试过定义三小时预测(self,city,country)?在这行:
cd=obj1.三小时预测(“伦敦,英国”)
您在同一参数中传递的是城市和国家。。试试看:
cd=obj1.三小时预测(“伦敦”、“英国”)