How can I import a python package or __init__.py file using the full path? -


i have package: pyfoo in directory /home/user/somedir

the package normal 1 '/home/user/somedir/pyfoo/__init__.py'

i able import module using full-path '/home/user/somedir/pyfoo/'

how non-package modules shown in: how import module given full path?

but seem unable work when module package.


the odd use case found embed deep script execution before writing file h5py.

i had uninstall h5py , reinstall parallel version openmpi, though uninstalled h5py(serial) still in memory. did not want restart, because script took long time. attempted reload module, did not work. tried import filename using directory , __init__.py, got relative import errors. tried adding new install location sys.path , executing normal import, failed.

i have import_from_filepath function in personal utility library, , i'd add import_from_dirpath well, i'm having bit of trouble figuring out how done.


here script illustrating issue:

    # define 2 temporary modules not in sys.path     # , have same name different values.     import sys, os, os.path     def ensuredir(path):         if not os.path.exists(path):             os.mkdir(path)     ensuredir('tmp')     ensuredir('tmp/tmp1')     ensuredir('tmp/tmp2')     ensuredir('tmp/tmp1/testmod')     ensuredir('tmp/tmp2/testmod')     open('tmp/tmp1/testmod/__init__.py', 'w') file_:         file_.write('foo = \"spam\"\nfrom . import sibling')     open('tmp/tmp1/testmod/sibling.py', 'w') file_:         file_.write('bar = \"ham\"')     open('tmp/tmp2/testmod/__init__.py', 'w') file_:         file_.write('foo = \"eggs\"\nfrom . import sibling')     open('tmp/tmp1/testmod/sibling.py', 'w') file_:         file_.write('bar = \"jam\"')      # neither module should importable through normal mechanism     try:         import testmod         assert false, 'should fail'     except importerror ex:         pass      # try temporarilly adding directory of module path     sys.path.insert(0, 'tmp/tmp1')     testmod1 = __import__('testmod', globals(), locals(), 0)     sys.path.remove('tmp/tmp1')     print(testmod1.foo)     print(testmod1.sibling.bar)      sys.path.insert(0, 'tmp/tmp2')     testmod2 = __import__('testmod', globals(), locals(), 0)     sys.path.remove('tmp/tmp2')     print(testmod2.foo)     print(testmod2.sibling.bar)      assert testmod1.foo == "spam"     assert testmod1.sibling.bar == "ham"      # fails, returns spam     assert testmod2.foo == "eggs"     assert testmod2.sibling.bar == "jam" 

the sys.path method not import via file path. defaults importing loaded module.

in general don't imports absolute paths in python. possible break things imports in module.

what adapt pythonpath. tells python stuff want import. can set environmental variable pythonpath before calling script or can see , manipulate @ runtime. so, change sys.path. it's list, can append() other paths.

an other option use virtual environments offer special python environments different projects , project setups. read http://docs.python-guide.org/en/latest/dev/virtualenvs/ idea of it.

if still want import path, read how import module given full path? .


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -