Skip to content

Instantly share code, notes, and snippets.

@nicolas-g
Last active November 30, 2018 04:33
Show Gist options
  • Save nicolas-g/e9df4b9938a44fd11d4702e710a5eca4 to your computer and use it in GitHub Desktop.
Save nicolas-g/e9df4b9938a44fd11d4702e710a5eca4 to your computer and use it in GitHub Desktop.

I have two python scripts, the first one will create a file defining some variables and the second one will import the first and read the created file. The problem is the second script cannot load the variables fromt the file created.

$ ls
create_file.py  read_file.py
$ ./create_file.py
file created

$ ls
create_file.py  my_file.py  read_file.py

$ cat my_file.py
my_var="my_value"
$ ./read_file.py
file created
Traceback (most recent call last):
  File "./read_file.py", line 5, in <module>
    print my_var
NameError: name 'my_var' is not defined

if I comment out the import create_file line from read_file.py and run it again I get the same result

$ cat read_file.py
#!/usr/bin/python

#import create_file
from my_file import *
print my_var


$ ./read_file.py
Traceback (most recent call last):
  File "./read_file.py", line 5, in <module>
    print my_var
NameError: name 'my_var' is not defined

if I manually "touch" the file then it works

$ echo "my_var_2=\"my_value_2\"" >> my_file.py

$ cat my_file.py
my_var="my_value"
my_var_2="my_value_2"

./read_file.py
my_value

WTF ?

#!/usr/bin/python
f= open("my_file.py","w+")
f.write("my_var=\"my_value\"\n")
f.closed
print "file created"
#!/usr/bin/python
import create_file
from my_file import *
print my_var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment