How to Create and Import a Custom Module in Python

后端 未结 1 577
悲哀的现实
悲哀的现实 2020-12-28 23:18

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)
         


        
相关标签:
1条回答
  • 2020-12-29 00:05

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

    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.

    C. Create another python file in the same directory named 'init'. 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'

    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.

    RESULT

    0 讨论(0)
提交回复
热议问题