4

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/

3
  • put your custom module at the same directory of your main script and just do: from your_module import * or (function name) Commented Nov 18, 2021 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 Commented Nov 18, 2021 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. Commented Nov 18, 2021 at 15:22

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the information. may I know how does the pip install work. how the process installs a module in my default python path
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 ?
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?


1 Comment

this is what I have been asking for. thanks for the solution
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

2 Comments

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
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.