Skip to content Skip to sidebar Skip to footer

How To Import A Single Function To My Main.py In Python From Another Module?

In my script I have a function inside a module which I wish to be able to use in my main module to prevent redundancy. This other module (not my main, let's call it two.py) contain

Solution 1:

You need to make sure that the directory you wish to import code from is in your system path e.g.:

sys.path.insert(0, path_to_your_module_dir)

Then you can go ahead and do

frommoduleimportfunction

UPDATE

The following thread has details of how to permanently add the directory to your pythonpath in either Windows or Unix-like systems:

Permanently add a directory to PYTHONPATH

Post a Comment for "How To Import A Single Function To My Main.py In Python From Another Module?"