0

I would like to change the way on Python 2.7 under Linux would load its modules/libraries from. I have tried to change it from the Configure file. Before that, it was like:

BINLIBDEST=     $(LIBDIR)/python$(VERSION)
LIBDEST=        $(SCRIPTDIR)/python$(VERSION)
INCLUDEPY=      $(INCLUDEDIR)/python$(VERSION)
CONFINCLUDEPY=  $(CONFINCLUDEDIR)/python$(VERSION)
LIBP=           $(LIBDIR)/python$(VERSION)

And I attempted to change it into this:

BINLIBDEST=     $(LIBDIR)
LIBDEST=        $(SCRIPTDIR)
INCLUDEPY=      $(INCLUDEDIR)
CONFINCLUDEPY=  $(CONFINCLUDEDIR)
LIBP=           $(LIBDIR)

Mainly removing python%(VERSION) from the pathname, so that instead of lib/python27, it would simply load its modules from only lib folder. However, even if initiating make and make install works with the changes, the python or python27 Python binary file does not loads the modules from the new path. It falls back with this output:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
'import site' failed; use -v for traceback

Is there a way on forcing the Python binary itself (if must) to load up the modules from a new path set by me, instead of the default one as "$(LIBDIR)/python$(VERSION)"?

Starbright
  • 1
  • 1
  • 1
  • I don't know what you're trying to achieve by getting rid of python$(VERSION) in path but it seems to me as a terrible idea which would get even worse if you try to install python3 along your python2 installation. – Michał Bentkowski Jul 18 '11 at 11:12
  • That may be true, but the main idea was not to work with the system-wide Python, but to have a separate, more of a portable version of Python to have its own libraries and modules without, as stated earlier, affecting anything or touching anything from the system-wide Python. – Starbright Jul 18 '11 at 11:27
  • I see. Anyway, according to docs: "By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion". So probably the best thing you can do is to change the prefix, not the suffix. Consider installing it to `/opt/` directory. You may also have to set `$PYTHONHOME` variable to `/opt/` as it was stated in the error message. – Michał Bentkowski Jul 18 '11 at 11:35

3 Answers3

1

You'll have to do a few changes to python source and recompile, but I'm going to assume that is okay, since this is a pretty non-standard thing to do.

Look at the file Modules/getpath.c. The steps python performs to determine the libdir is detailed in the comments in the beginning of the file. You can have a look at the svn repo here. I think you'll want to look at how this define is used:

#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
          EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"

I don't think it will be as easy as just changing it to [...] PREFIX "/lib/:" [...], but it will be something along those lines.

carlpett
  • 12,203
  • 5
  • 48
  • 82
0

Are you trying to accomplish something that virtualenv doesn't do?

It seems to meet your requirement:

"...the main idea was not to work with the system-wide Python, but to have a separate, more of a portable version of Python to have its own libraries and modules.."

Dave Forgac
  • 3,146
  • 7
  • 39
  • 54
  • I don't think you can do virtualenv on a flash drive, unless it's ext3 formatted... – Wayne Werner May 01 '12 at 21:01
  • @Starbright didn't mention a flash drive but if that's the requirement, there's a specific question for that: http://stackoverflow.com/questions/7798704/ And why would it have to be ext3? – Dave Forgac May 01 '12 at 21:25
  • Not specifically ext3 - but it can't be FAT32, which is usually the default - you can't create symlinks on FAT32, which virtualenv requires. Ask me how I know this ;) – Wayne Werner May 02 '12 at 12:33
0

I'd like to thank carlpett: I was able to set python search path at runtime: changed from lib/ to lib64/ while building Python 2.7.10 in x86_64-my_distro-gnu-linux using gcc 5.1 by modifying Modules/getpath.c.

For the record, I tried --libdir at configure time (works for the shared library but not for python modules install paths), modifying Makefile, modifying pyconfig.h, tweaking $PYTHONPATH, $PYTHONHOME, nothing worked.

Just a detail, but the make install does not place correctly the libraries, so you have to do a little cp -af and mv by yourself.

THANK YOU CARLPETT!!!

Vincent Achard
  • 221
  • 1
  • 10
  • Was this the only file you had to change? Several 'multi-lib' style patches (for older versions of Python-2.x.x) make changes in about 6 or 7 files, including `Modules/getpath.c`. If it can be done with just changes to Modules/getpath.c, I'd love to know - I have 4 python installations in `/opt/Python/Python-{2.7.9,3.4.3}-{32,64}` now, and anything that requires Python is always a huge pita to get to install/work (because I also didn't want 64-bit libraries in a non-existent `/usr/lib`) – chrisb2244 Jul 24 '15 at 02:32
  • Hi Chris! You're right, I checked my notes, there was another file to change: at compile time: Modules/Setup (zlib and SSL sections), and at run time: the sysconfig.py module (to adapt modules search, not sure if it has something to do with the libs..). – Vincent Achard Jul 25 '15 at 06:29