#๐ Problem while importing a function.
71 messages ยท Page 1 of 1 (latest)
@tacit spindle
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.
give us more context. Is it an external package? How are you importing it? Where are you using the imported function, or class? Give us the error message, if you see any. Share the code.
a matrix i dont want to be printed
i have to run this function a thousand times and return a mean
share the code here
please share the code related to the error, we can't help you blindly
without looking at the code, we can't tell you what's wrong .. there are multiple guesses we can take but rather look at the code and give a definitive answer ..
def impresion(matriz2):
"""
Por cada casilla de la matriz que le pasemos, va a imprimir un cuadrado, sin cambiar la matriz original
"""
for fila in range(dimension):
for columna in range(dimension):
if matriz2[columna][fila] == 3:
print(colored("โโ", "red"),end='')
elif matriz2[columna][fila] == 2:
print(colored("โโ", "light_red"),end='')
elif matriz2[columna][fila] == 1:
print(colored("โโ", "yellow"),end='')
elif matriz2[columna][fila] == -1:
print(colored("โโ", "green"),end='')
elif matriz2[columna][fila]== 0:
print(colored("โโ", "white"),end='')
else:
print(" ",end='')
print("")
print("\n")
lines 59 to 79 contain print()
and you are calling the function in line 150 inside the incendio() function which is being called in the file at line 157, which causes it to be printed on importing
to solve the issue you need move the function call on 157 to a function, and since it is in the main part of the script. I recommend you put it in a main function and add this to the end of your file
if __name__ == "__main__":
main()
which tells python to run this function (which is basically the main script) only when you run the file itself and not import it from somewhere ele
you got it? ask me if you want to understand more
i tried doing thos
like this
but in the second file i removed the part when it calls that function
the function will run even if you remove the call from the second file because it is being called from the file itself
and to prevent that you need to wrap it in a main() function
i do not think we are allowed to do that
this is for an assignment
we cant use some things
so they don't allow you to wrap the code in a main function? that can't be true
acutally it is a good practice to do so
they told us to use only what weยดve seen
a little bit of good practice won't harm, and you can explain the reason as being a way to prevent the code from running when you import it
as I said, wrap any code that is not iside a function to be in a main function
im sorry, i dont get what you mean
in that case keep everything inside their own function. So, when you import, you import the function directly without calling it. Right now, there are part of the script (in the first file) that are not wrapped in any function. So, as soon as you import it to another file, it will trigger them. If you can't do if __name__ == "__main__: then keep everything as a part of some function.
For example, in the first file keep you while loop at line 161 in a function. So that it will trigger when you import that function in the 2nd file and call the function explicitly.
OOOO
ok
i think iget it
but how do i call it without it running on the second file
Instead of having the following
while burningtrees>0:
bosque=incendio(bosque)[0]
burningtrees=incendio(bosque)[1]
minutos +=1
print(f"Tiempo: {minutos}")
Do,
def trigger_main():
while burningtrees>0:
bosque=incendio(bosque)[0]
burningtrees=incendio(bosque)[1]
minutos +=1
print(f"Tiempo: {minutos}")
This way, you have to import it in the 2nd file and explicitly trigger the function.
bc i need it to run on the firt file
.
this doesnt work ๐ญ
add this to the very last bit of your code
if __name__ == "__main__":
trigger_main()
and it will only run when you run the file itself, and not import it
OK
i think it works
give me a sec
there is a way to create modules and turn your project into a package. Which means, you can't directly run it. Only 1 file should run. All other should only contain functions and classes. They are not supported to run.
happy to hear that TL_AnimeThumbsUpWellDone
:\
los muertos se cuentan frios
I'm sorry but I don't understand this
its a famous phrase in spanish
can u translate?
its literal translation would be "dont count them as dead until thheir bodies are cold"
thats nice
โค๏ธ
yeah
@tacit spindle Check out the code I have modified.
a lot .. please check it properly .. I wrapped bosque_vacio inside a function. In the 2nd file I remove all extra stuff and simply import the functions from the first file and triggered it. Just compare these with your original code and see the difference.
Yes it won't. That's the whole point. Break down the code and keep them in separate files to make sure it is organised. And keep only 1 file as a point of entry. This way to trigger anything we have to trigger that 1 specific file.
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.