-1

I wrote a function and saved it in a different directory. If I want to call the function in the script I'm currently at. How do I tell Python the location of this directory? I've read some article like this one http://tinyurl.com/q2mmlh3 but I really don't understand how to solve my problem. Please explain to me how to import morbo_function_lib.py into euler_04.py

the location of the script I currently working at /home/morbo/document/python/euler/euler_04.py below is the location of the function I wish to import from /home/morbo/document/python/python_script/morbo_function_lib.py

I am running Python 2.X on Ubuntu 14.04. And I am using Geany to write Python code.

I must apologize because this seem to be a repeated question on the forum but due to my limited knowledge. I can't seem to locate the proper answer to my question.

Thank you very much for the time spent

msw
  • 40,500
  • 8
  • 77
  • 106
MorboRe'
  • 121
  • 8
  • 1
    possible duplicate of [Permanently add a directory to PYTHONPATH](http://stackoverflow.com/questions/3402168/permanently-add-a-directory-to-pythonpath) – msw Aug 25 '15 at 21:41

2 Answers2

1

Common used approach for this with good explanation are provided here How to import a module given the full path?

Community
  • 1
  • 1
Samrat Das
  • 1,572
  • 1
  • 19
  • 32
  • 'module.name' is just the file name containing the function I would like to call, right? – MorboRe' Aug 25 '15 at 21:52
  • 1
    right, one more example for that you can refer http://stackoverflow.com/questions/4970235/importing-a-module-dynamically-using-imp – Samrat Das Aug 25 '15 at 22:04
  • I successfully loaded the whole module, but it seems to be an incorrect solution to my problem. I need to call the function from that module, not the whole module. nonetheless, thank you so much for your help. I will go read some info provided about manipulating PYTHONPATH. thanks a lot!! – MorboRe' Aug 25 '15 at 22:20
  • 1
    If you imported module successfully, then you can just call function as shown foo = imp.load_source('module.name', 'path') foo.funtion() – Samrat Das Aug 25 '15 at 22:26
0

By default only the current directory and the Python library path are used (https://docs.python.org/2/tutorial/modules.html#the-module-search-path); anyway, you can use the "imp" (https://docs.python.org/2/library/imp.html):

import imp

foo = imp.load_source('module.name', '/home/morbo/document/python/python_script/morbo_function_lib.py')
matteo
  • 428
  • 4
  • 10
  • correct me if I am wrong. import imp is just a module built-in with Python to import user specified module. It's like a one time use function? – MorboRe' Aug 25 '15 at 21:41
  • If I want to permanently define a directory for Python to look for the script from now on when I call import.Do I have to make changes to the PYTHONPATH file? to specify the directory for the Python to look for? – MorboRe' Aug 25 '15 at 21:48
  • This is a horrible way to do an import, kinda like starting your car everyday with a crowbar. See the link I left as a comment on your question http://stackoverflow.com/questions/3402168/permanently-add-a-directory-to-pythonpath – msw Aug 25 '15 at 21:51
  • the information in the link is a bit confusing for a beginner like myself. Could you explain to me a bit about how to make changes to ~/.bashrc export PYTHONPATH="${PYTHONPATH}:/my/other/path" – MorboRe' Aug 25 '15 at 21:57
  • It's better to just add the directory to the sys path. – Alexander Huszagh Aug 25 '15 at 21:59
  • how do I add directory to the sys path? – MorboRe' Aug 25 '15 at 22:00
  • 1
    Put "export PYTHONPATH="${PYTHONPATH}:/home/morbo/document/python/python_script/" " in your ~/.bashrc (if you use bash - check file /etc/passwd for that) – matteo Aug 25 '15 at 22:06
  • 1
    btw there are 2 links on my answer, first one refers to path, second one to imp. Which one is preferred is your choice depending by your context. – matteo Aug 25 '15 at 22:09
  • just add the line "export PYTHONPATH="${PYTHONPATH}:/home/morbo/document/python/python_script/" into my .bashrc file with text editor?? – MorboRe' Aug 25 '15 at 22:26
  • I also have a .profile in my /home directory. Do I make change to .bashrc only and ignore .profile? – MorboRe' Aug 25 '15 at 22:27
  • 1
    Yes use bash if 'bash' is the shell specified in the line of your user login in /etc/passwd file, then log out and log in to easily reload the parameters (you can type "env" command then, in order to check if PYTHONPAH contains your custom directory). After that, mark this question as answered if works ;) – matteo Aug 25 '15 at 22:31
  • I can't find PYTHONPATH when I type env into my terminal. This is really getting confusing. However the method using import sys then using sys.path.insert works fine. I think I will just stop here and hoping some day when my skill catches up. I will be able to understand how to config PYTHONPATH... thanks for your help!! – MorboRe' Aug 26 '15 at 01:27
  • thank you so much matteo for all the time you spent answering my question. especially the part of answering question from a beginner. I understand it could be quite tedious. thank you so much :) – MorboRe' Aug 26 '15 at 19:53