2

Maybe this is a typical "you're doing it wrong" situation - in this case please let me know.

Currently I'm re-using a pre-built Python among different installations located in different directories, and to make this work I need to modify the generated _sysconfigdata.py to contain a certain installation path, which is also available as an environment variable (here SOME_ENV).

This can be done by search/replace a __PLACEHOLDER__ (configured with --prefix) inside _sysconfigdata.py, like this:

'INCLUDEPY': '__PLACEHOLDER__/include/python3.10',

.. with something dynamic like this:

'INCLUDEPY': f"{os.environ['SOME_ENV']}/include/python3.10",

Direct references of environment variables like

'INCLUDEPY': "${SOME_ENV}/include/python3.10",

seem to not work.

This of course feels like an ugly workaround for the built-in way to say "please take this pre-built Python and re-configure the prefix I've provided with configure.

Is there a way to "reconfigure" a readily built Python to be located in a different path?

(Background: I need those values in order to be able to build Python packages from source inside a delivered Python installation)

frans
  • 8,868
  • 11
  • 58
  • 132
  • 1
    Seems to me that you're trying to replicate what virtual environments do. – CristiFati Jan 16 '23 at 23:19
  • that's not true - AFAIK a virtual environment is making use of an already available and correctly configured installation of Python. In my question I'm basically asking how to provide that working installation of Python in an arbitrary directory without having to configure+compile it just to set a couple of variables pointing to that directory. – frans Jan 18 '23 at 07:51
  • Hmm with the current information, sounds like an *XY Problem* to me. Using a virtual environment seems to be a simpler option than what you're trying. – CristiFati Jan 18 '23 at 07:59
  • If I want to use a virtual environment I need Python first - this is what I want to achieve. But since I'm running in a manually maintained system (no packet manager) and I need a custom build of Python I need to ship and deploy it first. – frans Jan 18 '23 at 08:28
  • Maybe I'm missing the whole picture. The interpreter should not be set from the code. You should have some script that sets the correct paths (*Python*'s executable and *PYTHONHOME*) and Python* should pick it up from there. – CristiFati Jan 18 '23 at 08:45
  • That's not true when it comes to e.g. running `pip install` for packages built from source. `pip` will take the variable `CONFINCLUDEPY` from `_sysconfigdata[..].py` and fail if that variable is not set correctly (i.e. `Python.h` cannot be found there). You can try that with modifying `CONFINCLUDEPY` and running e.g. `pip install ibm_db`. – frans Jan 18 '23 at 10:43
  • These are two different methodologies: №1 is LBYL (look before you leap) and №2 is EAFP (easier to ask forgiveness than permission). https://stackoverflow.com/questions/204308/checking-for-member-existence-in-python – Nadeem Taj Jan 19 '23 at 06:55
  • How does that relate to my question? – frans Jan 19 '23 at 07:46
  • I'm not quite sure what the situation here is - it's not clear what you mean by "re-using a pre-built Python among different installations located in different directories". Are you saying you installed Python in one directory, and now you're trying to copy-paste it into a different directory and edit some files to get the new copy to work? – user2357112 Jan 19 '23 at 09:29
  • It seems you have Python distribution prebuilt outside of the system you want to use this distribution in and you don't have a way to build equivalent Python distribution using standard way (`configure, make, make install`) in the same system, right? If so this should be clearly stated in the question as this is important information which basically changes the original question to *How do I relocate (move) Python installation (distribution)?*. – Piotr Dobrogost Jan 19 '23 at 09:43
  • You're right, and this is why I've posted another question: https://stackoverflow.com/questions/75108809/how-can-a-pre-built-python-installation-be-modified-to-work-in-another-directory This one is about a specific implementation, maybe I should delete it (before the ChatGPT answer gets the reward..) – frans Jan 19 '23 at 10:35
  • _This of course feels like an ugly workaround for the built-in way to say "please take this pre-built Python and re-configure the prefix I've provided with configure._ – if `_sysconfigdata.py` is the only source for these paths and modifying it actually makes pip happy then I would say it's not so ugly and probably the best solution as I don't think there is any official way to relocate already built Python installation. – Piotr Dobrogost Jan 19 '23 at 11:15

0 Answers0