0

I have 2 python files. In file1, a list named MyList is created and in file2, the goal is to only print MyList from file1. In file2 I have the code below but it executes all of file1 along with the print function from file2.

#CODE FOR FILE 2
#/usr/bin/python
from file1 import MyList
print MyList

How do I prevent it from executing the commands in file1?

The code for file 1 is as follows:

MyList={}
UserListName = str(raw_input("Provide a name for your list: "))
MyList[UserListName]=[]
print "The userlist name is ", MyList

#feed values into MyList[UserListName] using raw_input
q = int(raw_input("Specify quantities for your list: "))
ArbitValue = 0
while ArbitValue < q:
MyList[UserListName].append(raw_input("Enter value: "))
ArbitValue += 1

print "The values entered in ", MyList
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • Post the code. Most likely can be done by using methods and checking if name is main: http://stackoverflow.com/questions/419163/what-does-if-name-main-do – Peter Sep 28 '15 at 17:48

1 Answers1

1
  1. You don't. When a module is imported, the statements in it are executed.
  2. Organize the code in functions. Then when imported the module will define some functions, but not do other stuff like producing output or reading input.
  3. Distinguish between being imported and being run directly via the standard if __name__ == '__main__': construct.
dsh
  • 12,037
  • 3
  • 33
  • 51