Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if module is installed in "editable mode"?

Tags:

python

pip

I'm pip-installing my module like so:

cd my_working_dir
pip install -e .

When I later import the module from within Python, can I somehow detect if the module is installed in this editable mode?

Right now, I'm just checking if there's a .git folder in os.path.dirname(mymodule.__file__)) which, well, only works if there's actually a .git folder there. Is there a more reliable way?

like image 456
fredrik Avatar asked Apr 11 '17 14:04

fredrik


People also ask

How do you check if a module is installed using pip?

How to Check if Python module is installed? You can use pip commands with grep command to search for any specific module installed on your system. For instance, you can also list out all installed modules with the suffix “re” in the module name.

What is pip E?

pip install -e is how setuptools dependencies are handled via pip . What you typically do is to install the dependencies: git clone URL. cd project. run pip install -e . or pip install -e .

How do you check if I have a module installed?

To see all modules installed on the system, use the Get-Module -ListAvailable command.

What is editable mode pip install?

pip install -e . Although somewhat cryptic, -e is short for --editable , and . refers to the current working directory, so together, it means to install the current directory (i.e. your project) in editable mode.


1 Answers

Another workaround:

Place an "not to install" file into your package. This can be a README.md, or a not_to_install.txt file. Use any non-pythonic extension, to prevent that file installation. Then check if that file exists in your package.

The suggested source structure:

my_repo
|-- setup.py
`-- awesome_package
    |-- __init__.py
    |-- not_to_install.txt
    `-- awesome_module.py

setup.py:

# setup.py
from setuptools import setup, find_packages

setup(
    name='awesome_package',
    version='1.0.0',

    # find_packages() will ignore non-python files.
    packages=find_packages(),
)

The __init__.py or the awesome_module.py:

import os

# The current directory
__here__ = os.path.dirname(os.path.realpath(__file__))

# Place the following function into __init__.py or into awesome_module.py

def check_editable_installation():
    '''
        Returns true if the package was installed with the editable flag.
    '''
    not_to_install_exists = os.path.isfile(os.path.join(__here__, 'not_to_install.txt'))
    return not_to_install_exists
like image 114
betontalpfa Avatar answered Oct 08 '22 20:10

betontalpfa