1

is it possible to detect if current python code is running from package?

if yes - is it possible to get package metadata (name, version, description)?


package is created with this kind of setup.py

import os
from setuptools import setup, find_packages
setup(
    name='my-pack-name',
    description='my description ' + os.getenv('GIT_COMMIT', '*')[:7],
    version=os.getenv('BUILD_VERSION', '0.0.0dev'),
    packages=find_packages(),
)

build: python3 setup.py bdist_wheel -d ./artifact

install on target: pip3 install "my-pack-name-x.x.x.whl" --upgrade

now from my_pack_name/app.py that was inside my-pack-name-x.x.x.whl i want to detect that i'm running from installed package

and if so then get package metadata defined during setup.py execution

daggett
  • 26,404
  • 3
  • 40
  • 56
  • 1
    I think this is a possible duplicate of https://stackoverflow.com/questions/55900956/check-if-package-is-imported-from-within-the-source-tree – MEE Sep 08 '21 at 18:23
  • thank you for the link. seems i have a subset of the goal described there. – daggett Sep 08 '21 at 19:47

2 Answers2

3

For Python >=3.8

https://docs.python.org/es/3.10/library/importlib.metadata.html

You can get the metadata for a package by:

from importlib.metadata import metadata

md = metadata("your package name")
author = md["Author"]
# etc ...

For Python <3.8

This is just an idea (not tested).

What about having the package metadata in a different module, and try relative import it in the app.py module?

# metadata.py
name='my-pack-name',
description='my description ' + os.getenv('GIT_COMMIT', '*')[:7],
version=os.getenv('BUILD_VERSION', '0.0.0dev')

In setup.py you could reuse that:

# setup.py
import os
from setuptools import setup, find_packages
import .metadata as md
setup(
    name=md.name,
    description=md.description + os.getenv('GIT_COMMIT', '*')[:7],
    version=md.version,
    packages=find_packages(),
)

And the in app.py

def get_metadata():
    try:
        import .metadata as md
    except ImportError:
        return None
    else:
        # return metadata here

That way if get_metadata returns None, you were not able to import the module, so you're not executing the app.py in your package, otherwise, you are in your package and as a bonus you got your metadata.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • good idea, but the issue here that env vars like BUILD_VERSION are available at build step, and not at deploy or runtime. i could generate the kind of MD file inside setup.py... was looking for idea to get MD like `pip show` does. – daggett Sep 08 '21 at 19:07
  • I did a little more investigation on this, and I found what I think it is exactly what you're looking for, but it is just available for Python >= 3.8 I've edited the answer. – Raydel Miranda Sep 09 '21 at 19:02
  • I see if 'site-packages' is in `__file__` as the only downside in this method is that it cannot tell the difference between a `pip install -e` and a normal install. Using importlib is good for what OP asks though. – Benjamin Goodacre Mar 15 '23 at 12:02
2

For the test if python code is running from an installed package (vs. from a development location), I use

if 'site-packages' in __file__:
  ...

I don't know if that's a good approach; seems to work thus far.

ssc
  • 9,528
  • 10
  • 64
  • 94