(Solved) How do I use other files in a program
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.
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! :)
stadenbrant
If more information is required, please just leave me a comment and I will try and get you more.
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
you could do
import <file>
orfrom <file> import <method>
to just import a function.You might want to create a method and call it like:
P.S. that should work but it might not :D
@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
@stadenbrant the other file should be called
town.py
. Import with:import town
Please upvote if this helps :)
@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.
@ArchieMaclean Am I doing it right?
This file cannot be displayed: https://storage.googleapis.com/replit/images/1551978051113_279bab28cc6fed84f2bcac6364478af5.pn
@ArchieMaclean https://repl.it/live/cUYLCs81zEBQMg
@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 :)
@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.
@ArchieMaclean Do I just delete my top question when the problem is solved?
@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 :)
@stadenbrant Yes, as @ArchieMaclean said, please just edit the title and leave the post in case someone else has the same question!