python giving a E501: line too long error

Viewed 19697

While trying to input my API key python is giving me a line too long code

E501: line too long

What I have is

notifications_client = NotificationsAPIClient(aaaaaaa_aaaaaaaa-11aa1a1a-aa11-111a-aaaa-11111aaa1a1a-aa11a1a1-0aa1-11a1-1111-1aa111a0a111)

For obvious reasons I have changed the API key to have only a's 1's and 0's but how can I break up this line of code so I no longer get this error?

4 Answers

E501 is a linter error, not a Python interpreter error. Your code, in theory, should work just fine. If you want to prevent this error, simply break the value up (assuming it's a string ... you don't make that clear):

my_key = ('aaaaaaa_aaaaaaaa-11aa1a1a-aa11-111a-aaaa-'
          '11111aaa1a1a-aa11a1a1-0aa1-11a1-1111-'
          '1aa111a0a111')
notifications_client = NotificationsAPIClient(my_key)

E501 is not a python error, rather than a PEP8 error. Meaning your line is longer than 80 chars (in your case it's 137 chars long).

Your editor or runtime are verifying that your code is correct by PEP8 rules and that's why you are getting this "error". Your Python code has actually no errors at all.

If you want your code to be PEP8 compliant I suggest:

  1. Extract the API key to a local variable.
  2. If it's still too long you can break up the string into multiple lines

Here is an example:

API_KEY = 'aaaaaaa_aaaaaaaa-11aa1a1a-aa11-111a' \ 
          '-aaaa-11111aaa1a1a-aa11a1a1-0aa1-' \
          '11a1-1111-1aa111a0a111'
notifications_client = NotificationsAPIClient(API_KEY)

Use \ to break your line. Like; notifications_client = NotificationsAPIClient(aaaaaaa_aaaaaaaa-11aa1a1a-\ aa11-111a-aaaa-11111aaa1a1a-\ aa11a1a1-0aa1-11a1-1111-1aa111a0a111)

Option which doesn't involved breaking the string literal:

notifications_client = NotificationsAPIClient(
    "kkkkkkkkkkkkkeeeeeeeeeeeeeeeeeeeeeeeeeeyyyyyyyyyyyyyyyyyyyyy"
)

So long as your key is <73 (minus scope indentation) characters long. If not, you'll have to split it.

Related