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?
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.
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 .
To see all modules installed on the system, use the Get-Module -ListAvailable command.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With