-1

I am developing a python library and the end-user who uses my library in their respective application should be able to provide a configuration file in the root path of their application which my library will read in run-time. The end-user's application can be any Project like a Simple Python Project, Django, Flask etc

In my library, I'm planning to have the following code. The following is a piece of code that will be exported as a library. for example, my-library

def read_config():
   BASE_DIR = # Some code
   with open(os.path.join(BASE_DIR, 'test.conf')) as f:
      f.read()

I'm not sure how to get the value of BASE_DIR of the end-user's application through my library

I have seen some posts related to getting root path using settings.py which resides in the root path of the application. But in my case since I have no idea about end user's project structure, I guess I cannot use this.

Note-The enduser would install my library as a package using pip install my-library

user3451476
  • 297
  • 1
  • 4
  • 17
  • Why has this been given a down vote ? – user3451476 Jun 17 '16 at 04:34
  • How does the user intend on using the application? Is this not something that could be specified on startup? – Sayse Jun 17 '16 at 06:29
  • @Sayse The enduser would install my library as a package using pip install package – user3451476 Jun 17 '16 at 06:32
  • Thats how they install it.. but how do they use it? I can't think of a way you could achieve this unless you either have it hosted on a cloud somewhere or allow the user to specify the conf file themselves. – Sayse Jun 17 '16 at 06:34
  • @Sayse I'm new to python. This is something similar to classLoader.getResource('filename") in Java. I have already gone through posts related to it. In those posts, answers are related to reading files in **my-library** itself. Whereas I want the end user to provide **test.conf** somewhere in his application and when he uses read_config of my library, it should be able to read the **test.conf** – user3451476 Jun 17 '16 at 06:46
  • @Sayse updated question as well – user3451476 Jun 17 '16 at 06:48

1 Answers1

2

I think you may want to rethink your approach.

I don't think its going to be possible to dynamically retrieve a base directory easily, but then you may not have to. Instead of relying on a settings file, make your app have its own settings static class that you can retrieve any configurations you need. If an end user wishes to customize any settings then they can either introduce or expand an initialization step to their application.

This is the same way that django does it, django uses its own settings class which end users can override and set their own values for.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • I do not require the base directory of my library. I want my library to get the path of **test.conf** in the application used by end-user in which the end-user uses my library – user3451476 Jun 17 '16 at 06:53
  • @user3451476 - And thats fine, but you need the user of your application to supply that path first before it can be really used. – Sayse Jun 17 '16 at 06:54
  • I'm actually able to do this with java using javaClassLoader.getResource("test.conf"). Not sure why I cant do it in python – user3451476 Jun 17 '16 at 06:57
  • Would it work if the end-user provides the path to **test.conf** in the PYTHONPATH of the enduser's application. And how can my-library know the parameters set in the PYTHONPATH in the enduser's application – user3451476 Jun 17 '16 at 07:19
  • So you want the user to supply the path.... so why not let them do that in their own settings for your app – Sayse Jun 17 '16 at 07:22
  • I am not familiar with the approach you have mentioned. It would be great if you could add any links with examples related to that – user3451476 Jun 17 '16 at 07:25
  • @user3451476 - Best example would be to just install django somewhere and take a look at the settings.py file of an app – Sayse Jun 17 '16 at 07:27