python - Print() 被忽略

标签 python syntax-error runtime-error

当我运行这个时:

from firebase import firebase
from sanction import Client

client_pin = ''
client_id = 'valid_id'
client_secret = 'valid_secret'
request_token = 'state'
access_token = ''

query_url_wss = 'wss://developer-api.nest.com'
query_url_https = 'https://developer-api.nest.com'
auth_url = 'https://home.nest.com/login/oauth2?client_id=%s&state=%s' %(client_id, request_token)
access_token_url = 'https://api.home.nest.com/oauth2/access_token'

print('Visit the below link in a web browser to get an access PIN:\n')
print(auth_url)
client_pin = input('Enter PIN: ')

c = Client(
    token_endpoint=access_token_url,
    client_id=client_id,
    client_secret=client_secret)

c.request_token(code = client_pin)

data = c.request('/devices')
print(data)
<小时/>

我得到这个输出(忽略这里的错误 - 这只是客户端被创建和使用以及获得有效 token 的证明。这是目前打印 access_token 的唯一方法):

<小时/>
Visit the below link in a web browser to get an access PIN:

https://home.nest.com/login/oauth2?client_id=VALID_ID&state=state
Enter PIN: [ENTERED_A_VALID_PIN]
Traceback (most recent call last):
  File "C:\py\nest_testing_sanction.py", line 29, in <module>
    data = c.request('/devices')
  File "C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py", line 169, in request
  File "C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py", line 211, in transport_query
  File "C:\Python34\lib\urllib\request.py", line 258, in __init__
    self.full_url = url
  File "C:\Python34\lib\urllib\request.py", line 284, in full_url
    self._parse()
  File "C:\Python34\lib\urllib\request.py", line 313, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'None/devices?access_token=[VALID_TOKEN]'
<小时/>

创建客户端类时,我没有打印出“客户端启动”。我对 __init__.py 所做的任何操作都不会生效。它是否与库存版本一起缓存在某处?

<小时/>

__init__.py 来源:http://pastebin.com/TksTyZT4

<小时/>

修改后的函数(如上面错误输出引用的__init__.py中所示):

class Client(object):

    def __init__(self, auth_endpoint=None, token_endpoint=None,
        resource_endpoint=None, client_id=None, client_secret=None,
        token_transport=None):

        assert token_transport is None or hasattr(token_transport, '__call__')

        self.auth_endpoint = auth_endpoint
        self.token_endpoint = token_endpoint
        self.resource_endpoint = resource_endpoint
        self.client_id = client_id
        self.client_secret = client_secret
        self.access_token = None
        self.token_transport = token_transport or transport_query
        self.token_expires = -1
        self.refresh_token = None

        print('Client initiated') # Added this line - not getting executed

我还修改了这个函数(大概是与上面相同或相似的问题):

def request_token(self, parser=None, redirect_uri=None, **kwargs):

    kwargs = kwargs and kwargs or {}

    parser = parser or _default_parser
    kwargs.update({
        'client_id': self.client_id,
        'client_secret': self.client_secret,
        'grant_type': 'grant_type' in kwargs and kwargs['grant_type'] or \
            'authorization_code'
    })
    if redirect_uri is not None:
        kwargs.update({'redirect_uri': redirect_uri})

    msg = urlopen(self.token_endpoint, urlencode(kwargs).encode(
        'utf-8'))
    data = parser(msg.read().decode(msg.info().get_content_charset() or
        'utf-8'))

    for key in data:
        setattr(self, key, data[key])

    if hasattr(self, 'expires_in'):
        try:
            # python3 dosn't support long
            seconds = long(self.expires_in)
        except:
            seconds = int(self.expires_in)
        self.token_expires = mktime((datetime.utcnow() + timedelta(
            seconds=seconds)).timetuple())

    #
    # I added all these prints and the return - no dice
    #
    print('***************************************************')
    print('Access Token: %s' %self.access_token)
    print('Token Life: %s' %self.token_expires)
    print('***************************************************')

    return self.access_token
  1. 我添加的行怎么可能被忽略?
  2. 我需要做什么才能让他们被处决?

我唯一能想到的是该文件的另一个版本正在被使用,但我不知道它可能在哪里或者是否是这种情况。

最佳答案

可能有一个过时的 .pyc 文件需要删除。根据您运行代码的方式,它可能会被编译为存储在 .pyc 文件中的字节码。但是,当您对 .py 中的源进行更改时,重新运行时它不会覆盖现有的 .pyc。如果您的代码位于您正在导入的模块中,就会发生这种情况。查找 __init__.pyc 并删除那个坏男孩。

编辑,聊天后我们发现:

导入的

__init__.py 位于 C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py,而正在编辑的 __init__.py 位于 C:\Python34\lib\site-packages\sanction\__init__.py。

sanction-0.4.1-py3.4.egg 实际上不是一个文件夹,而是一个文件,并且由于某种原因在导入时具有优先权(未知,但可能是由于安装错误) .

关于python - Print() 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26493592/

相关文章:

python - 如何在 python 中将数字转换为 12 位精度?

php - 代码点火器错误?

javax.naming.NameNotFoundException : Name [comp/env] is not bound in this Context. Java 调度程序无法找到 [comp] 错误

下载 zip 文件时的 Python Flask 已损坏

Java:在数组列表中删除期间出现运行时错误

python - 使用 Python 将电子邮件发送到邮件列表

python - 使用 django mptt 时出现问题

python - Django/重复键值违反唯一约束

mysql - MYSQl 触发器创建中的语法错误

iphone - Xcode、 Objective-C 、IOS。线程 1 : Program received signal: "SIGABRT". 我做错了什么