-1

I am trying to create one distributable package in Python.So in my package I need to access a file from the root of the project for which my package is installed inside. I am trying to do that like this,

Path(__file__).parents[1] / Path(self.file_name)

But is is just returning '.' which means it indicating my package's root folder. For example my package directory structure looks like this,

mypackage
 - lib
   - myfile.py
 - __init__.py

So when I convert that package to installable wheel format I can install that using pip command.If now I create project like this,

myproject
 - __init__.py
 - config.json
 - main.py
 - venv

Now see I want to use/install mypackage in myproject. All the packages are installed inside the venv folder. So now my question is how I can access the config.json file inside my mypackage. So to do that I require to access the root folder of myproject.I hope this is clear to understand. Any help would be appreciated.

Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
  • . is the "current working directory". It is a proper path. – MisterMiyagi May 12 '21 at 06:09
  • @MisterMiyagi I know but that is my package directory. When I install my package for any project I want that project's root directory. – Ropali Munshi May 12 '21 at 06:11
  • This statement '*So now my question is how I can access the config.json file inside my mypackag*' doesn't match your graph. The 'config.json' seems under your project directory instead of your package in your graph. – felixc May 12 '21 at 06:47
  • 1
    That is my problem statement. I want to access `config.json` file which is inside the `myproject` dir not in `mypackage` dir. – Ropali Munshi May 12 '21 at 06:51
  • Do you need the actual path or just the content of the JSON? Is `myproject` a package itself? The `__init__.py` suggests yes, the venv suggests no. Put another way: what do you consider "a project"? Python has no such concept. – MisterMiyagi May 12 '21 at 06:54
  • If I have access to the `myproject` dir then I can read the any file I want but yes my main goal is to read the `config.json` file. `mypackage` is a python package I am trying to create. – Ropali Munshi May 12 '21 at 06:58
  • Does this answer your question? [Python - Get path of root project structure](https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure) – Давид Шико Nov 12 '22 at 09:26

3 Answers3

2

Use importlib.resources to access file-like resources in packages. This uses the import machinery to locate the resource, ensuring that you get the resource in the package used by your program. For example, if you want the resource as a file:

import importlib.resources
import json

# create a pathlib.Path pointing to the resource
with importlib.resources.path("myproject", "config.json") as json_path:
    with json_path.open() as json_file:
        data = json.load(json_file)

Using importlib will work for any importable package, including those not backed by the filesystem (e.g. a zipped package). Note that if you are just interested in the contents, then importlib.resources.read_text is more convenient instead of creating a file just to discard it.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • Thanks for the help but one question `myproject` can have any name so in that case how I would I know that. I just can't hard code the project name in my package. – Ropali Munshi May 12 '21 at 07:28
  • @RopAliMunshi In that case, treating it as "the JSON inside ``myproject``" is the entirely wrong approach. As mentioned in the question comments, there plainly is no such thing as "a project", only packages – what you consider "a project" is not well-defined enough for code to find it. It seems logically the JSON is just *some* JSON and ``mypackage`` should make no attempt at all to locate it – it should be *given* the location of either "the project" or the JSON. – MisterMiyagi May 12 '21 at 07:34
  • So the conclusion is that it is not possible to achieve that in python. – Ropali Munshi May 12 '21 at 07:47
  • The conclusion is that you should clearly define what your setup is – including which parts are happenstance and which are deliberate. – MisterMiyagi May 12 '21 at 08:47
  • Which part you don't understand?Let me put it this way,Pandas is a well know library/package which can be installed in your project. In the same manner I am trying to create one standalone package which I can use in any of my "project". I create one venv and install my that package, So my package needs to have access to a file which resides inside that project.Hope this make sense to you. – Ropali Munshi May 12 '21 at 09:13
  • @RopAliMunshi Mostly the word "project" makes no sense for Python. Consider that "a well known library/package" like Pandas has *no* concept of being installed into a project – you *don't* install package into projects, you install them into the module search path. What is a project? Some IDE folder? A working directory? The directory in which a venv root is? – MisterMiyagi May 12 '21 at 10:05
0

If my understanding is correct, a python script in the root directory will import your package. If this is correct, the following code may help:

import os
import __main__ as main
if hasattr(main, '__file__'):
    root_dir = os.path.dirname(main.__file__)
felixc
  • 168
  • 2
  • 11
0

you can use

import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) # This is your Project Root

and see this answer as reference , it will help you

Mohamed Ibrahim
  • 291
  • 3
  • 12