Search code examples
pythonpython-modulepython-3.6

How to use a module stored in a different directory in Python


I am making a python 3.6 program which involves the use of a few 3rd party modules. Is it possible to configure my program to use modules stored in the local folder (C:\Users\username\directory\filelocationfolder\modules) rather than the default location for modules installed to python (C:\Python36\Lib\site-packages)?

This way, I could send someone the folder containing my program and they would not need to have the required modules pre-installed.

I am somewhat new to Python, so I would appreciate if you could explain your answers.


Solution

  • use the sys lib for example you have directory C:\a and C:\b having scripts as a.py and b.py

    then to import b.py into a.py do something like this

    import os
    
    import sys
    
    sys.path.append(os.getcwd()+"/../b")
    
    from b import *