0

I am brand new at all of this and I am completely lost even after Googling, watching hours of youtube videos, and reading posts on this site for the past week.

I am using Jupyter notebook

I have a config file with my api keys it is called config.ipynb

I have a different file where I am trying to call?? (I am not sure if this is the correct terminology) my config file so that I can connect to the twitter API but I getting an attribute error

Here is my code

    import numpy as np
    import pandas as pd
    import tweepy as tw
    import configparser



    #Read info from the config file named config.ipynb

    config = configparser.ConfigParser()
    config.read(config.ipynb)
    api_key = config[twitter][API_key]

                      
    print(api_key) #to test if I did this correctly`
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In [17], line 4
  1 #Read info from the config file named config.ipynb
  3 config = configparser.ConfigParser()

----> 4 config.read(config.ipynb) 5 api_key = config[twitter][API_key]

AttributeError: 'ConfigParser' object has no attribute 'ipynb'

After fixing my read() mistake I received a MissingSectionHeaderError.

MissingSectionHeaderError: File contains no section headers. 
file: 'config.ipynb', line: 1 '{\n'. 

My header in my config file is [twitter] but that gives me a NameError and say [twitter] is not defined... I have updated this many times per readings but I always get the same error.

My config.ipynb file code is below:

['twitter']

API_key = "" #key between the ""

API_secret =  "" #key between the ""
        
Bearer_token = "" #key between the ""

Client_ID = "" #key between the ""

Client_Secret = "" #key between the ""

I have tried [twitter], ['twitter'], and ["twitter"] but all render a MissingSectionHeaderError:

sitWolf
  • 930
  • 10
  • 16
IDK
  • 15
  • 3
  • With Python's configparser you do not need to add the values as strings in the config file. [twitter], API_key=your_twitter_api_key_value, and not ['twitter'] and not "your_twitter_api_key_value" – sitWolf Nov 19 '22 at 14:07
  • Hi IDK, welcome to StackOverflow. When your problem is solved you should not extend it with new issues that arise down your project. Your issue as described in the original post of not being able to read the config file has been resolved. You can now accept the answer, and raise a new question for your new issue that has to do with authentication. – sitWolf Nov 20 '22 at 09:21

2 Answers2

0

You are using the read() method incorrectly, the input should be a string of the filename, so if your filename is config.ipynb then you need to set the method to

config.read('config.ipynb')
brance
  • 1,157
  • 9
  • 22
  • Thank you for your help. I corrected the mistake only to get a MissingSectionHeaderError. MissingSectionHeaderError: File contains no section headers. file: 'config.ipynb', line: 1 '{\n'. My header in my config file is [twitter] but that files gives me a NameError and say [twitter] is not defined... I have updated this many times per readings but I always get the same error. – IDK Nov 19 '22 at 10:47
  • Can you please post your config file in your question. – brance Nov 19 '22 at 12:18
  • I added the file in the question, thank you – IDK Nov 19 '22 at 13:52
  • Remove the `'` from `['twitter']` so that it is `[twitter]` and then try again. – brance Nov 19 '22 at 15:11
0

Per your last comment in Brance's answer, this is probably related to your file path. If your file path is not correct, configparser will raise a KeyError or NameError.

Tested and working in Jupyter:

Note that no quotation marks such as "twitter" are used

# stackoverflow.txt
[twitter]
API_key = 6556456fghhgf
API_secret =  afsdfsdf45435
import configparser
import os

# Define file path and make sure path is correct
file_name = "stackoverflow.txt"

# Config file stored in the same directory as the script.
# Get currect working directory with os.getcwd()
file_path = os.path.join(os.getcwd(), file_name)

# Confirm that the file exists.
assert os.path.isfile(file_path) is True 
# Read info from the config file named stackoverflow.txt
config = configparser.ConfigParser()
config.read(file_path)

# Will raise KeyError if the file path is not correct
api_key = config["twitter"]["API_key"]
print(api_key)
sitWolf
  • 930
  • 10
  • 16
  • Thank you! I have managed to get all my keys called correctly and then I hit an authentication error and I have posted everything I have – IDK Nov 19 '22 at 18:46
  • Hi IDK, glad I could help. Please accept the answer (closing your original post) and raise a new question for the new problem you are facing. – sitWolf Nov 20 '22 at 09:13
  • Before posting a new question, please have a look at this [answer](https://stackoverflow.com/questions/66156958/how-to-acess-tweets-with-bearer-token-using-tweepy-in-python/66597109#66597109) and check if it answers your question. – sitWolf Nov 20 '22 at 09:16