How to import local files in replit?

Question: how to import local files in replit mobile app

Repl link/Link to where the bug appears:

Screenshots, links, or other helpful context:

code snippet

:wave:
HI! Welcome to the forums. In python, you can import functions from other files. For example. This is how our files are placed:

main.py
functions.py

In functions, we have this code:

def foo():
     print('Hello, World!')

to get our function, foo() into our main.py, we put this code into main.py

import functions # no ".py" necessary
functions.foo()

and the output:

Hello, World!

Stuff I am not totally sure about:

If you have functions.py in a different location then main.py, you would also need to include the file. For example, this is our file tree:

main.py
 |foldername
 |functions.py

Then you would import it with a period between the folder and the functions:

import foldername.functions as functions # without the "as," you would need to type foldername.functions.foo()

(Thanks @QwertyQwerty88. I edited this post)

If you want more ways to import from a folder, see @QwertyQwerty88’s post below.

I hope this helps!

3 Likes

No, you have to import it like this:

import foldername.functions
# or
import foldername.functions as functions
# or
from foldername import functions
4 Likes

I edited my original post.

2 Likes