Python Forum
How to assign a module to a variable even if it's not defined?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to assign a module to a variable even if it's not defined?
#1
Hello, I want to import modules from files, I need to store them into variables and I need their names as strings because I must check if the modules python files exist or not for example.
My question is, how to assign a module to a variable even if it's not defined? See the line 1 below, thanks.

module = math # NameError: name 'math' is not defined
module_str = module.__name__ # It should be 'math', but not 'module' because it's not a variable name.
import module
To make everything clear, here is another example, I want to store the string 'math' into a variable, then I import it:
import 'math' # SyntaxError: invalid syntax
Reply


Messages In This Thread
How to assign a module to a variable even if it's not defined? - by mandaxyz - Aug-12-2020, 02:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 128 Yesterday, 02:13 PM
Last Post: Larz60+
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 517 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,045 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,335 Apr-13-2021, 07:22 PM
Last Post: HeRo
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,308 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  Function will not return variable that I think is defined Oldman45 6 3,436 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  Variable not defined Heyjoe 4 2,491 Jul-10-2020, 11:27 PM
Last Post: Heyjoe
  python library not defined in user defined function johnEmScott 2 3,775 May-30-2020, 04:14 AM
Last Post: DT2000
  Error: variable can not be defined julio2000 2 3,142 Feb-09-2020, 08:51 PM
Last Post: julio2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020