My Coding > Programming language > Python > Python libraries and packages > How to import my own module to Python script

How to import my own module to Python script

If the Python code is pretty big, it is a common sense to split it into a few files. But after spiting into few files it is necessary to refer to them. The standard procedure import only works with the files in the Python pathway.

From my point of view, the easier way to refer to files in the same directory, it is update Pyhon module path with adding your current directory, or directory with you modules.

First of all you need to import module for working with operational system information os and module with system information sys, then read current path for working directory with command os.getcwd() and then add this directory to to the python system path sys.path


import sys
import os
sys.path.append(os.getcwd())

after this you can import your modules with the same command import or from if they are in the same directory.

If they are in the different directory – add more information to the path.


Published: 2022-07-23 14:10:46

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.