Skip to content
← Back to Community
(Solved) How do I use other files in a program
Profile icon
stadenbrant

I am using Python 2.7.10 and I would like to know how to program it so that a different file will run when a specific input has been detected as the result of a question.

Voters
Profile icon
yanovsky
Profile icon
Bananablue
Profile icon
ArchieMaclean
Profile icon
stadenbrant
Comments
hotnewtop
Profile icon
Cerberus_75

you could do import <file> or from <file> import <method> to just import a function.

You might want to create a method and call it like:

from test import test test = input("Hi") if test == "hi" print(test) #method from import file else print("Good Bye")``` # and for your test.py: def test(): test = "Hello" return

P.S. that should work but it might not :D

Profile icon
stadenbrant

@Cerberus_75 Is it importing if its already in the website?
This file cannot be displayed: https://storage.googleapis.com/replit/images/1551926850631_c0f444bdd4d9c8d78369ba111c419f23.pn

Profile icon
ArchieMaclean

@stadenbrant the other file should be called town.py. Import with:
import town
Please upvote if this helps :)

Profile icon
ArchieMaclean

@Cerberus_75 That is very muddly because there is a method called test and a variable called test and a file called test.
So it may not work.

Profile icon
stadenbrant

@ArchieMaclean Am I doing it right?
This file cannot be displayed: https://storage.googleapis.com/replit/images/1551978051113_279bab28cc6fed84f2bcac6364478af5.pn

Profile icon
stadenbrant
Profile icon
ArchieMaclean

@stadenbrant Here is an example repl I set up to show importing in python:
https://repl.it/@ArchieMaclean/Importing-Stuffs?language=python&folderId=
Please upvote if this helps :)

Profile icon
stadenbrant

@ArchieMaclean Thanks, this workes nicely and now I can make my short choose your own story game without having 256 lines of code in the same folder.

Profile icon
stadenbrant

@ArchieMaclean Do I just delete my top question when the problem is solved?

Profile icon
ArchieMaclean

@stadenbrant Just change the title to something like
(Solved) How do I use other files in a program
Then other people can always see this if they have the same problem.
Glad I could help :)

Profile icon
themaka

@stadenbrant Yes, as @ArchieMaclean said, please just edit the title and leave the post in case someone else has the same question!

Profile icon
yanovsky

What cerberus_75 forgot to add is that the from (file) import (method) also works with lists, and variables. Having a list called testlist, with file test you could say from test import testlist, and then you could print the list, or operate with it if needed, all in main.py. This import function is very useful! :)

Profile icon
stadenbrant

If more information is required, please just leave me a comment and I will try and get you more.

Profile icon
Geocube101

@stadenbrant I don't use Python 2.7, but in Python 3, importing the file runs it automatically
When importing a file, don't include the .py extension