Justin
Justin

Reputation: 293

Importing and storing the data from a Python file

How do I import a Python file and use the user input later?

For example:

#mainprogram
from folder import startup
name

#startup
name = input('Choose your name')

What I want is to use the startup program to input the name, then be able to use the name later in the main program.

Upvotes: 0

Views: 82

Answers (3)

ovrwngtvity
ovrwngtvity

Reputation: 4419

I think is better do your code in classes and functions. I suggest you to do:

class Startup(object):
@staticmethod
def getName():
    name = ""
    try:
        name = input("Put your name: ")
        print('Name took.')
        return True
    except:
        "Can't get name."
        return False

>> import startup
>> Startup.getName()

Upvotes: 0

user2555451
user2555451

Reputation:

name will be in startup.name. You can use dir(startup) to see it.

Or, as an alternate solution:

# Assuming from the names that 'folder' is a folder and 'startup' is a Python script
from folder.startup import *

now you can just use name without the startup. in front of it.

Upvotes: 0

gr3co
gr3co

Reputation: 893

You can access that variable via startup.name later in your code.

Upvotes: 2

Related Questions