installation - Python 3.x - How to get the directory where user installed package -
suppose building python package following folder tree:
main |-- setup.py |-- the_package |--globar_vars.py |--the_main_script.py
when user performs anyway install it, like:
sudo python setup.py install python setup.py install --user python setup.py install --prefix=/usr/local
or well, using pip:
pip install 'someproject'
i want folder package installed saved on global_vars.py
, in variable, eg:
globarl_vars.py #!/usr/bin/env python3 user_installed_pkg = '/some/path/where/the/package/was/installed'
there someway it? in advance.
assuming someproject
package well-formed python package (which can done using setuptools), user can derive location use pkg_resources
module provided setuptools
package information. example:
>>> pkg_resources import working_set >>> pkg_resources import requirement >>> working_set.find(requirement.parse('requests')) requests 2.2.1 (/usr/lib/python3/dist-packages) >>> working_set.find(requirement.parse('requests')).location '/usr/lib/python3/dist-packages'
however, path returned inside egg means not path directly usable through standard filesystem tools. want use resource manager api access resources in cases.
>>> import pkg_resources >>> api_src = pkg_resources.resource_string('requests', 'api.py') >>> api_src[:25] b'# -*- coding: utf-8 -*-\n\n'
Comments
Post a Comment