11

How can I save the following function in one python file, and then use it in another?

Function in File A

def basic(x):
    print(x)

Statement in File B:

basic("some string")
2
  • 3
    import it? Have you tried reading a tutorial? Commented May 6, 2016 at 12:45
  • i have but i got confused. thank you for explaining. Commented May 6, 2016 at 13:12

1 Answer 1

23

A. Create a folder which will contain all of your modules. As an example, lets use 'MyModules'

Create_Directory

B. Within the folder, save your classes in a py file. You can have more nesting than I'll demonstrate, but for simplicity we will not nest folders in this example.

Classes

enter image description here

C. Create another python file in the same directory named __init__.py. Note there are 2 underscores before and after 'init'. Within that file, import the file you need.

The syntax should be 'from FOLDERNAME import FILENAME'

Init Import

Init Location

D. Now, from a new python file, you can import your custom classes. Note, if your package isn't recognized, you may need to change the path you're running the file from.

import

RESULT

RESULT

Sign up to request clarification or add additional context in comments.

1 Comment

Could you please fix the pictures and edit this using the help text to the right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.