python - Check if my application runs in development/editable mode -


my python application can install normal way, or in development/editable mode pip, this:

virtualenv my_app source my_app/bin/activate pip install -e my_app 

how can make function introspect virtualenv , check if application runs in development/editable mode?

is there "flag" in sys module that?

motivation: have different configuration in development mode, , in production.

edit: compare virtualenv , package directories.

import os import sys  import pkg_resources  main_pkg = 'my_app' package_dir = pkg_resources.resource_filename(main_pkg, '__init__.py') virtualenv_dir = os.path.dirname(os.path.dirname(sys.executable)) common_path = os.path.commonprefix([package_dir, virtualenv_dir]) is_dev_mode = not common_path.startswith(virtualenv_dir) 

i test if package_dir subdirectory of virtualenv_dir: if not subdirectory, on development mode.

edit2:

is there more reliable solution?

i want know if there isn’t data/a flag in environment indicate me application running in development mode.

what happen if dependency in development mode too?

using code pip can determine this:

import pip import pkg_resources  # i've done `pip install -e .` git repo i'm working in inside # virtualenv distributions = {v.key: v v in pkg_resources.working_set} # >>> distribution # pre-commit 0.9.3 (/home/asottile/workspace/pre-commit) distribution = distributions['pre-commit']  # below approximately how `pip freeze` works, see end of # answer simpler approach, still using pip  # turn pip frozenrequirement (i'm using pip 9.0.1, may # different version) # i've passed empty list second argument (dependency_links) # don't think it's necessary? frozen_requirement = pip.frozenrequirement.from_dist(distribution, [])  # query whether requirement installed editably: print(frozen_requirement.editable) 

the magic of comes little function inside pip (pip.utils):

def dist_is_editable(dist):     """is distribution editable install?"""     path_item in sys.path:         egg_link = os.path.join(path_item, dist.project_name + '.egg-link')         if os.path.isfile(egg_link):             return true     return false 

the dist in pkg_resources distribution (as acquired above). of course can use dist_is_editable function directly instead of going through frozenrequirement:

# `distribution` above: pip.utils import dist_is_editable print(dist_is_editable(distribution))  # true in case ;) 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -