title: Error: "Only unicode objects are escapable. Got None of type <class 'NoneType'>". Could someone please help me spot the mistake(s) in my code?
body: <p>I am a beginner at Twitter Development and Python programming in general, but recently I have been trying to build a bot that, when tagged in reply to a Tweet, finds keywords and uses Google to deliver some info from reliable sources regarding the topic of the original Tweet. However, I have encountered one major problem while programming: <code>API</code> doesn't get created since the code triggers the error &quot;only unicode objects are escapable&quot;. I have used module Config to set my Twitter API credentials as environmental variables and that seems to work fine on its own. Before trying to run the bot, I activate my virtual environment and export the env variables, so I do not think this issue has to do with incorrectly setting those variables, but I would not say I am certain about that either!
  The code goes this way:</p>
  <pre><code>import tweepy
  import logging
  from config import create_api
  import time
  import re
  from googlesearch import search
  import sys
  import io
  
  logging.basicConfig(level=logging.INFO)
  logger = logging.getLogger()
  
  api = tweepy.API
  
  def check_mentions(api, keywords, since_id):
  
      logger.info(&quot;Collecting info&quot;)
  
      new_since_id = since_id
  
      for tweet in tweepy.Cursor(api.mentions_timeline,
          since_id=since_id).items():
  
          new_since_id = max(tweet.id, new_since_id)
  
  
          if tweet.in_reply_to_status_id is not None:
              in_reply_to_status_id = previous_tweet.id
              status_id = tweet.in_reply_to_status_id
              tweet_u = api.get_status(status_id,tweet_mode='extended')
              #to store the output -links- as a variable to use it later
              old_stdout = sys.stdout
              new_stdout = io.StringIO()
              sys.stdout = new_stdout
  
  
              output = new_stdout.getvalue()
              sys.stdout = old_stdout
  
          print(output)
  
  
          text = output
  
          # remove words that are between 1 and 3 characters long
          shortword = re.compile(r'\W*\b\w{1,3}\b')
  
          print(shortword.sub('', text))
  
          
  
          keywords_search = print(shortword.sub('', text))
  
          if keywords_search is not None:
  
                  mystring = search(keywords_search, num_results=500)
          else:
                  mystring = search(&quot;error&quot;, num_results=1)
  
  
  
          for word in mystring:
                  if &quot;harvard&quot; in word or &quot;cornell&quot; in word or &quot;researchgate&quot; in word or &quot;yale&quot; in word or &quot;rutgers&quot; in word or &quot;caltech&quot; in word or &quot;upenn&quot; in word or &quot;princeton&quot; in word or &quot;columbia&quot; in word or &quot;journal&quot; in word or &quot;mit&quot; in word or &quot;stanford&quot; in word or &quot;gov&quot; in word or &quot;pubmed&quot; in word:
                                  print(word)
  
          #to store the output -links- as a variable to use it later
          old_stdout = sys.stdout
          new_stdout = io.StringIO()
          sys.stdout = new_stdout
  
          for word in mystring:
                  if &quot;harvard&quot; in word or &quot;cornell&quot; in word or &quot;researchgate&quot; in word or &quot;yale&quot; in word or &quot;rutgers&quot; in word or &quot;caltech&quot; in word or &quot;upenn&quot; in word or &quot;princeton&quot; in word or &quot;columbia&quot; in word or &quot;journal&quot; in word or &quot;mit&quot; in word or &quot;stanford&quot; in word or &quot;gov&quot; in word or &quot;pubmed&quot; in word:
                                  print(word)
  
          #to print normally again
          output = new_stdout.getvalue()
  
          sys.stdout = old_stdout
          print(output)
  
  
          if output is not None:
                  status = &quot;Hi there! This may be what you're looking for&quot; and print(output),
                  len(status) &lt;= 280
                  api.update_status(status, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=False),
  
          else:
                  status = &quot;Sorry, I cannot help you with that :(. You might want to try again with a distinctly sourced Tweet&quot;,
                  len(status) &lt;= 280
                  api.update_status(status, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=False),
  
          return new_since_id
  
  
  
  
  
  def main():
      api = create_api()
      since_id = 1 #the last mention you have.
      while True:
          print(since_id)
          since_id = check_mentions(api, [&quot;help&quot;, &quot;support&quot;], since_id)
          logger.info(&quot;Waiting...&quot;)
          time.sleep(15)
  
  if __name__ == &quot;__main__&quot;:
      main()
  
  </code></pre>
  <p>My Config module:</p>
  <pre><code>
  import tweepy
  import logging
  import os
  
  logger = logging.getLogger()
  
  def create_api():
      consumer_key = os.getenv(&quot;XXXXXXXXXX&quot;)
      consumer_secret = os.getenv(&quot;XXXXXXXXXX&quot;)
      access_token = os.getenv(&quot;XXXXXXXXXX&quot;)
      access_token_secret = os.getenv(&quot;XXXXXXXXXX&quot;)
  
  
      auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
      auth.set_access_token(access_token, access_token_secret)
      api = tweepy.API(auth, wait_on_rate_limit=True, 
          wait_on_rate_limit_notify=True)
      try:
          api.verify_credentials()
      except Exception as e:
          logger.error(&quot;Error creating API&quot;, exc_info=True)
          raise e
      logger.info(&quot;API created&quot;)
      return api
  
  </code></pre>
  <p>The error goes:</p>
  <pre><code>ERROR:root:Error creating API
  Traceback (most recent call last):
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 184, in execute
      resp = self.session.request(self.method,
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 516, in request
      prep = self.prepare_request(req)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 449, in prepare_request
      p.prepare(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 318, in prepare
      self.prepare_auth(auth, url)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 549, in prepare_auth
      r = auth(self)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py&quot;, line 108, in __call__
      r.url, headers, _ = self.client.sign(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 313, in sign
      ('oauth_signature', self.get_oauth_signature(request)))
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 127, in get_oauth_signature
      uri, headers, body = self._render(request)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 209, in _render
      headers = parameters.prepare_headers(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 32, in wrapper
      return target(params, *args, **kwargs)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py&quot;, line 59, in prepare_headers
      escaped_value = utils.escape(value)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 56, in escape
      raise ValueError('Only unicode objects are escapable. ' +
  ValueError: Only unicode objects are escapable. Got None of type &lt;class 'NoneType'&gt;.
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\config.py&quot;, line 23, in create_api
      api.verify_credentials()
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\api.py&quot;, line 672, in verify_credentials
      return bind_api(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 253, in _call
      return method.execute()
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 192, in execute
      six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\six.py&quot;, line 702, in reraise
      raise value.with_traceback(tb)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 184, in execute
      resp = self.session.request(self.method,
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 516, in request
      prep = self.prepare_request(req)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 449, in prepare_request
      p.prepare(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 318, in prepare
      self.prepare_auth(auth, url)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 549, in prepare_auth
      r = auth(self)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py&quot;, line 108, in __call__
      r.url, headers, _ = self.client.sign(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 313, in sign
      ('oauth_signature', self.get_oauth_signature(request)))
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 127, in get_oauth_signature
      uri, headers, body = self._render(request)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 209, in _render
      headers = parameters.prepare_headers(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 32, in wrapper
      return target(params, *args, **kwargs)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py&quot;, line 59, in prepare_headers
      escaped_value = utils.escape(value)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 56, in escape
      raise ValueError('Only unicode objects are escapable. ' +
  tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type &lt;class 'NoneType'&gt;.
  Traceback (most recent call last):
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 184, in execute
      resp = self.session.request(self.method,
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 516, in request
      prep = self.prepare_request(req)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 449, in prepare_request
      p.prepare(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 318, in prepare
      self.prepare_auth(auth, url)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 549, in prepare_auth
      r = auth(self)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py&quot;, line 108, in __call__
      r.url, headers, _ = self.client.sign(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 313, in sign
      ('oauth_signature', self.get_oauth_signature(request)))
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 127, in get_oauth_signature
      uri, headers, body = self._render(request)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 209, in _render
      headers = parameters.prepare_headers(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 32, in wrapper
      return target(params, *args, **kwargs)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py&quot;, line 59, in prepare_headers
      escaped_value = utils.escape(value)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 56, in escape
      raise ValueError('Only unicode objects are escapable. ' +
  ValueError: Only unicode objects are escapable. Got None of type &lt;class 'NoneType'&gt;.
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\botstring20.py&quot;, line 114, in &lt;module&gt;
      main()
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\botstring20.py&quot;, line 105, in main
      api = create_api()
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\config.py&quot;, line 26, in create_api
      raise e
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\config.py&quot;, line 23, in create_api
      api.verify_credentials()
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\api.py&quot;, line 672, in verify_credentials
      return bind_api(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 253, in _call
      return method.execute()
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 192, in execute
      six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\six.py&quot;, line 702, in reraise
      raise value.with_traceback(tb)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 184, in execute
      resp = self.session.request(self.method,
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 516, in request
      prep = self.prepare_request(req)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py&quot;, line 449, in prepare_request
      p.prepare(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 318, in prepare
      self.prepare_auth(auth, url)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py&quot;, line 549, in prepare_auth
      r = auth(self)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py&quot;, line 108, in __call__
      r.url, headers, _ = self.client.sign(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 313, in sign
      ('oauth_signature', self.get_oauth_signature(request)))
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 127, in get_oauth_signature
      uri, headers, body = self._render(request)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py&quot;, line 209, in _render
      headers = parameters.prepare_headers(
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 32, in wrapper
      return target(params, *args, **kwargs)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py&quot;, line 59, in prepare_headers
      escaped_value = utils.escape(value)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py&quot;, line 56, in escape
      raise ValueError('Only unicode objects are escapable. ' +
  tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type &lt;class 'NoneType'&gt;.
  
  
  </code></pre>
  <p>I know it is pretty long but I just wanted to know if anyone could tell me which direction to go with the bug-solving process, since I am kind of lost and I did not know where else to ask! Thank you in advance and sorry for how long this is!!!!&lt;3</p>