So I have two different files somewhat like this:
file1.py
from file2 import *
foo = "bar";
test = SomeClass();
file2.py
class SomeClass :
def __init__ (self):
global foo;
print foo;
However I cannot seem to get file2 to recognize variables from file1 even though its imported into file1 already. It would be extremely helpful if this is possible in some way.
This question is related to
python
Just put your globals in the file you are importing.
After searching, I got this clue: https://instructobit.com/tutorial/108/How-to-share-global-variables-between-files-in-Python
the key is: turn on the function to call the variabel that set to global if a function activated.
then import the variabel again from that file.
i give you the hard example so you can understood:
file chromy.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def opennormal():
global driver
options = Options()
driver = webdriver.Chrome(chrome_options=options)
def gotourl(str):
url = str
driver.get(url)
file tester.py
from chromy import * #this command call all function in chromy.py, but the 'driver' variable in opennormal function is not exists yet. run: dir() to check what you call.
opennormal() #this command activate the driver variable to global, but remember, at the first import you not import it
#then do this, this is the key to solve:
from chromy import driver #run dir() to check what you call and compare with the first dir() result.
#because you already re-import the global that you need, you can use it now
url = 'https://www.google.com'
gotourl(url)
That's the way you call the global variable that you set in a function. cheers don't forget to give credit
All given answers are wrong. It is impossible to globalise a variable inside a function in a separate file.
When you write
from file2 import *
it actually copies the names defined in file2
into the namespace of file1
. So if you reassign those names in file1
, by writing
foo = "bar"
for example, it will only make that change in file1
, not file2
. Note that if you were to change an attribute of foo
, say by doing
foo.blah = "bar"
then that change would be reflected in file2
, because you are modifying the existing object referred to by the name foo
, not replacing it with a new object.
You can get the effect you want by doing this in file1.py
:
import file2
file2.foo = "bar"
test = SomeClass()
(note that you should delete from foo import *
) although I would suggest thinking carefully about whether you really need to do this. It's not very common that changing one module's variables from within another module is really justified.
global
is a bit of a misnomer in Python, module_namespace
would be more descriptive.
The fully qualified name of foo
is file1.foo
and the global statement is best shunned as there are usually better ways to accomplish what you want to do. (I can't tell what you want to do from your toy example.)
from file2 import *
is making copies. You want to do this:
import file2
print file2.foo
print file2.SomeClass()
Source: Stackoverflow.com