Xavier Villafaina
Xavier Villafaina

Reputation: 635

Using import our script with Python

I don't know how explain what I want do so I think with code example you can understand.

y.py

y = 0

import printy

printy.py

import y

print y

NameError: name 'y' is not defined

When I call scripts with import works perfect but I want to know how I can share variables.

y.py

import printy

printy.py

y = 0

print y

result = 0

Upvotes: 1

Views: 56

Answers (1)

Slass33
Slass33

Reputation: 66

I don't know which is which file, (assuming each code block is a different file) it should work, and share variables if importing them works. Also, I would write it like this:

file y.py:

from printy import *

y = 0

file printy.py:

from y import *

print y

Also, both files have to already exist at time of running, and they have to saved into the same folder.

EDIT: If result = 0 is the output, then everything is working fine. Also, if this doesn't work, I'd do printy.print(y)

Upvotes: 1

Related Questions