There is a problem on my Reddit bot, can you fix it please? [New User + Poetry]

import praw
import time
import os
from dotenv import load_dotenv

load_dotenv()
reddit = praw.Reddit(
    client_id=os.getenv("CLIENT_ID"),
    client_secret=os.getenv("SECRET"),
    user_agent="anti_h_bot (by crispybeatle)",
    username=os.getenv("USERNAME"),
    password=os.getenv("PASSWORD")
)
def run():
    try:
        for comment in reddit.subreddit("TheLetterH").stream.submissions(skip_existing=True):
            print("New comment", end="")
            comment.comment("h is bad")
            print(", replied")
    except Exception as e:
      print(e)
      run()
run()

My main issue is a Traceback which says

reddit = praw.Reddit(
AttributeError: module ‘praw’ has no attribute ‘Reddit’

Hi,

I referenced the The Reddit Instance documentation from here:

The Reddit Instance - PRAW 7.7.1 documentation

Is there a reason that you are deviating from their recommended set-up and using the os module?

reddit = praw.Reddit(
    client_id="CLIENT_ID",
    client_secret="CLIENT_SECRET",
    password="PASSWORD",
    user_agent="USERAGENT",
    username="USERNAME",
)

Can you try and adhere to their recommendation?

Did you save this code into a file called praw.py?

1 Like

Can you try and adhere to their recommendation?

Yes, I will.

Is there a reason that you are deviating from their recommended set-up and using the os module?

This is my first time using Python, so I followed a guide which didn’t use the recommended set-up.

Thank you for your time Paul.

It didn’t work :frowning:

Yes but it still didn’t work

What went wrong here?

Traceback (most recent call last):
File “C:\Users\Facu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\Roaming\pypoetry\venv\Scripts\anti_h_bot\praw.py”, line 1, in
import praw
File “C:\Users\name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\Roaming\pypoetry\venv\Scripts\anti_h_bot\praw.py”, line 3, in
reddit = praw.Reddit(
AttributeError: partially initialized module ‘praw’ has no attribute ‘Reddit’ (most likely due to a circular import)

Are you using a virtual environment?

I would suggest something a little easier. If you are just starting out, I wouldn’t recommend jumping in and working on a project like this. First learn the language. There is so much to learn and understand before attempting to work on a project of this scale if you’re just starting out.

I think so?

Have you tried running it from a non-virtual environment? There have been a few posts from other users on this platform reporting issues when running from third party virtual environments. I would suggest downloading either IDLE or Spyder IDE. Then try running your code from there. But I still recommend learning the Python language first so that you understand a bit more of what you are doing (I am assuming you don’t know any other language but who knows, maybe you’re an expert in another language and already comfortable with programming :grinning:)

IDLE:

Spyder:

Either is relatively easy to use.

I was running it from IDLE though maybe I should try Spyder??

What is your IDLE Python version (IDLE is fine)? Spyder is easier and more natural to use.
Note that Spyder comes with Python v3.8. If you do decide to download it, make sure you point your interpreter to v3.12. So it can use the latest version of Python.

IDLE Python 3.12

Ok, that is good.

Try following this tutorial and working from there:

@onePythonUser I am sorry, but everything you have said in this thread until now has been a completely distraction from the problem, which @Rosuav correctly identified in their post. You are still a new user of python, and you do not have enough experience to be helpful for most people tbh.


@crispybeatle What you are running into is the same as this issue, just with praw instead of requests. Don’t name the file you directly want to execute the same as the library you want to use.

2 Likes

Hmm. Ok. My apologies. From the OPs response, when asked and responding, the response was (when responding to @Rosuav):

Yes, but I think that was a miscommunication. The file should not be named praw.py, but the traceback they posted showed that the file is clearly called praw.py

I renamed it to praw.py after @Rosuav replied to my post

Can you show the full traceback of the error message from a file that isn’t named praw?

Traceback (most recent call last):
File “C:\Users\name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\Roaming\pypoetry\venv\Scripts\anti_h_bot\index.py”, line 3, in
reddit = praw.Reddit(
AttributeError: module ‘praw’ has no attribute ‘Reddit’