Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a class variable from another module

Tags:

python

import

I'm trying to import just a variable inside a class from another module:

import module.class.variable 
# ImportError: No module named class.variable

from module.class import variable 
# ImportError: No module named class

from module import class.variable
# SyntaxError: invalid syntax (the . is highlighted)

I can do the following, but I'd prefer to just import the one variable I need.

from module import class as tmp
new_variable_name = tmp.variable
del tmp

Is this possible?

like image 657
Alex L Avatar asked Feb 06 '12 02:02

Alex L


People also ask

How do I import a class from another module?

Python modules can get access to code from another module by importing the file/function using import. The import statement is that the commonest way of invoking the import machinery, but it's not the sole way. The import statement consists of the import keyword alongside the name of the module.

How can I access class variable from another file?

In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement.


1 Answers

variable = __import__('module').class.variable 
like image 87
icktoofay Avatar answered Oct 04 '22 12:10

icktoofay