Alex
Alex

Reputation: 21

Issue with importing and printing variable

Recently, I've tried to get comfortable with the open and write functions. I've encountered a problem which goes as follows: I use one script to write email = [email protected] in a file named "config.py". This is done using the following code: (All sensitive info has been replaced)

email = raw_input("What is your email? ")

f = open("config.py","w") #opens file
f.write("email = '{}'".format(email))
f.close() #This needs to be here else the file won't save

Then I try to import and open config.py in a separate script which looks like this:

import config

print email

However, I receive this error message:

Traceback (most recent call last):
  File "/Users/MyUser/Desktop/untitled folder/Savetests/savetest_loading.py", line 1, in <module>
    import config
  File "/Users/MyUser/Desktop/untitled folder/Savetests/config.py", line 1
    email = [email protected]
                           ^
SyntaxError: invalid syntax

I have tried writing the file with quotations however this creates conflicts with the .format(). I am using python 2.7

My question is what should I do?

Thank you,

User

Upvotes: 0

Views: 35

Answers (1)

falsetru
falsetru

Reputation: 369124

You need to quote the email:

email = '[email protected]'

And in the module where import it, qualify the email with module name:

import config

print config.email

Or import email using from .. import .. statement:

from config import email

print email

Upvotes: 1

Related Questions