Can an imported module use modules already imported?

43 views Asked by At

edit: since no one understands the question I will rewrite it.

file1

import turtle
import file2

turtle.uselesscode()

turtle.mainloop()

file2

turtle.goto(-50,-50)
turtle.fd(50)
#more code with the turtle module

questions:

  1. why am I getting an error when importing file2 (code runs):

NameError: name 'Turtle' is not defined

(module "file2" isn't able to use turtle imported in "file1")

  1. can i use exec() instead of importing module "file2", and will I be able to without putting from turtle import * inside file2
1

There are 1 answers

3
LoGo124 On

Title question: If I understand your question correctly, the answer is Yes, but it is not necessary to import the module if you are not going to use it since if module X uses module Y, module Y is already being imported internally by module X.

If you want, you can see it yourself by checking the code for module X and seeing how in the code it uses "Import" to import module Y

Description question: Regarding the exec() function, it is a function that executes python code passed in string format, there is also eval() which has a similar behavior but it does not execute, you will not be able to create variables but you will be able to see the content.