3

I want to import my custom-written functions in any script on any directory, just like I import the requests modules in any script. I am running Ubuntu and Python 3.9

Edit: I fulfilled my requirements by following this tutorial - https://packaging.python.org/tutorials/packaging-projects/

Tarek Rahman
  • 68
  • 1
  • 9
  • put your custom module at the same directory of your main script and just do: `from your_module import *` or **(function name)** – Rutangaba Nov 18 '21 at 15:12
  • I suppose you are referring to putting the scripts in same directory. Isn't there a way to put a script in system directory, so that I don't need to have the file in same directory – Tarek Rahman Nov 18 '21 at 15:17
  • 1
    Yeah, you can do: `import sys sys.path.append('/path/to/application/app/folder') from module import function_names` - like @Ulises Bussi said. – Rutangaba Nov 18 '21 at 15:22

3 Answers3

4

You can add the folder to python path, or import sys, add path to this sesion and then import

import sys
sys.path.append(Path_to_module)
import module

or

import sys
sys.path.append(Path_to_module)
from module import function_names
Ulises Bussi
  • 1,635
  • 1
  • 2
  • 14
  • thanks for the information. may I know how does the pip install work. how the process installs a module in my default python path – Tarek Rahman Nov 18 '21 at 15:31
  • I'm not really sure i'll be answering this correct, because i have no formation in that, but I think it install the module inside an specific folder included in your python path. In my case I'm using conda environment in linux so the path is something like `/home/user/anaconda3/envs/my_environment/lib/python3.8/site-packages` So tecnically if you add your library there you could import it, but I think it's not a good idea to do that. you might add you library path permanently to pythonpath. What's your OS ? – Ulises Bussi Nov 18 '21 at 15:39
4

You could make a simple package of your custom functions and then just install the package in your system locally using pip. After this you will be able to import the functions from any script.

# for example
pip install .

# or if you need to edit your functions install in editable mode
pip install -e .

Note: the dot '.' above indicates that your setup.py is located in the current working directory. You can also provide the path to the setup.py for your package instead of the dot. Reference on creating package: How to write a Python module/package?


aprath
  • 76
  • 4
0

Given your_function inside your_script.py in the same directory of your current code, you can write on your code:

 from your_script import your_function
Ruggero
  • 427
  • 2
  • 10
  • I suppose you are referring to putting the scripts in same directory. Isn't there a way to put a script in system directory, so that I don't need to have the file in same directory – Tarek Rahman Nov 18 '21 at 15:17
  • 1
    @TarekRahman: You can put `your_script.py` in any directory. Just add `sys.path.append(r'directory_path')` in the program which imports the function or the class. Import using `import 'your_script'` without the extension `py`. You can use any form of import (`import ... as ...`, `from ... import ...`, etc). Path to directory can be absolute or relative. – mins May 04 '23 at 15:04