Ask coding questions

← Back to all posts
How I supose to use other files? It doesn´t allows me to do it.
Hugoprogramer (0)

When I try to use a comand that I created in another file in the main file it doesn´t work.
Do I need to activate something??
Or use a caracteristic comand?
I use phyton

Comments
hotnewtop
InvisibleOne (2683)

just import the file

import filename

then you can use functions inside of that file by calling the file name, and then the function name. So if I made a file called hello_world.py and put

def hello():
  print("Hello World!")

inside of it, then in my main.py I could do

import hello_world
hello_world.hello() # "Hello World!" is printed to the console

or I could do this

from hello_world import hello
hello() # "Hello World!" Is printed to the console
[deleted]

Or you can do import os than for example you have a file named Test.py do so.system(‘python Test.py’)

Ascendancy (1)

For example, you have main.py,main1.py,main2.py,main3.py if you want to run another python script, type in main.py (the original) import main1.py and it will run that script.