#๐Ÿ”’ Error

151 messages ยท Page 1 of 1 (latest)

azure valley
#

Im coding a custom bot made with Python and I get a Error the part of the code that Is giving an error Is client.run(os.getenv('TOKEN'))

terse mothBOT
#

@azure valley

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

kind ingot
#

What's the full traceback?

modern siren
#

show us the full traceback

#

and the code

azure valley
#

Code and traceback.

#

@modern siren

#

@kind ingot

modern siren
#

Hm

#

are you sure that you downloaded the discord pip library

azure valley
#

Insatll pip discord?

#

Install*

modern siren
#
pip install discord
azure valley
#

Should I do In It shell?

modern siren
#

yes

#

if it does not work

#

try this

azure valley
#

Ok done now lemme try.

modern siren
#
py -m pip install discord
azure valley
#

It downloaded but I still get the error

modern siren
#

hm

#

could you hover over this part of your code

#

and show us the error message?

azure valley
modern siren
#

???????????

#

anyways that seems to not be a problem.

#

btw i am so sorry

#

that i cannot help ou

#

because i have no experience of discord bots in python

#

i am so sorry

azure valley
#

Ok.

#

@kind ingot

#

You have any idea of how to fix?

modern siren
#

please help thi scase

kind ingot
#

It looks like you don't have an API key in you environment

azure valley
kind ingot
#

You'll need to get an API key

azure valley
#

Im new to Python

#

Im using a 3 year old vid.

#

U have a vid that I can use that will work?

edgy cobalt
#

@azure valley create a .env file with
TOKEN = yourbottoken

azure valley
edgy cobalt
#

show your env file

#

Wait... Is that replit?

azure valley
azure valley
edgy cobalt
#

Pretty sure there's another way to add env vars then

#

Try googling

heavy fable
#

did you install dotenv-python ?

azure valley
#

And also discord py.

edgy cobalt
edgy cobalt
azure valley
#

With the other way.

modern siren
edgy cobalt
edgy cobalt
edgy cobalt
#

Try it

#

in the client.run() u can replace what's inside with my_secret

#

Since you're already defining it before

edgy cobalt
#

no, client.run(my_secret)

azure valley
#

Now i get this error.

edgy cobalt
#

did you set MY_SECRET = yourtoken
in the Secrets tab?

azure valley
heavy fable
#

try import load_dotenv ig py from dotenv import load_dotenv load_dotenv()

edgy cobalt
#

I'm not sure then... someone more experienced with replit can help perhaps

azure valley
#

()

heavy fable
#

nothing

azure valley
#

I put It at the top

heavy fable
#

try to run it

azure valley
#

It didnt work

heavy fable
#

what is the error

azure valley
azure valley
#

Ima reset my token after

#

But Is this correct In my ENV file?

modern siren
#

at least blur it

azure valley
#

I already reset It.

modern siren
#

oh ok

heavy fable
#

your token not in TOKEN = '..........'

#

ig you forgot the ' at last

azure valley
zinc pendant
#

if its not string then print out what it holds

#

its None i see

heavy fable
zinc pendant
# azure valley

well you are trying to access MY_SECRET variable but you named it TOKEN

zinc pendant
#

you have 2 ) at the end?

#

also why you create MY_SECRET variable when you dont use it

#

when you directly pass token to .run

azure valley
zinc pendant
#

just remove that variable

zinc pendant
#

where did you even get that code from

azure valley
zinc pendant
#

i recommend first learning at least basics of python

azure valley
#

I used to make python stuff before.

#

Like micro bits.

#

And just In the console.

zinc pendant
#

doesnt look like if you dont see a problem with 2 closing )

azure valley
zinc pendant
#

you showed error on line 4

#

not on line 25

azure valley
#

Okay.

#

client.run(os.getenv('TOKEN'))

#

I tried to do this.

heavy fable
#

it work ?

azure valley
#

Didnt work.

#

Just adding to the error

zinc pendant
#

if you still want to try your best at discord bots

#

this examples will work 4sure and wont be so outdated

azure valley
#

User*

#

Use*

zinc pendant
#

this guide was written pretty recently

#

and for past 3 years few things changed in the lib

terse mothBOT
#
Using .env files in Python

.env (dotenv) files are a type of file commonly used for storing application secrets and variables, for example API tokens and URLs, although they may also be used for storing other configurable values. While they are commonly used for storing secrets, at a high level their purpose is to load environment variables into a program.

Dotenv files are especially suited for storing secrets as they are a key-value store in a file, which can be easily loaded in most programming languages and ignored by version control systems like Git with a single entry in a .gitignore file.

In Python you can use dotenv files with the python-dotenv module from PyPI, which can be installed with pip install python-dotenv. To use dotenv files you'll first need a file called .env, with content such as the following:

TOKEN=a00418c85bff087b49f23923efe40aa5

Next, in your main Python file, you need to load the environment variables from the dotenv file you just created:

from dotenv import load_dotenv

load_dotenv()

The variables from the file have now been loaded into your program's environment, and you can access them using os.getenv() anywhere in your program, like this:

from os import getenv

my_token = getenv("TOKEN")

For further reading about tokens and secrets, please read this explanation.

zinc pendant
#

and this should explain you how environment variables work and how to use them in code

azure valley
zinc pendant
terse mothBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.