5

I embedded Python in an application. When the user installs a package or module via

{...}\myapp\python\python.exe setup.py install

the packages will be installed in

{...}\myapp\python\lib\site-packages

Is there any chance to use another directory instead by default?

HelloWorld
  • 2,392
  • 3
  • 31
  • 68
  • Btw, I try to solve it without virtualenv – HelloWorld Oct 06 '17 at 19:27
  • have you tried `{...}\myapp\python\python.exe setup.py install --prefix={...}\myapp\` ? You can also use pip: `pip3 install --install-option="--prefix=/some/location/outside/myapp/" /path/to/folder/with_setup_py/` – Payman Oct 14 '17 at 16:18
  • 1
    Might help if you added a little more context, but I think the short answer is that these directories are built in to [site.py](https://github.com/python/cpython/blob/master/Lib/site.py#L342), so you'll have to patch your python install to do this. – Peter Brittain Oct 14 '17 at 16:42

2 Answers2

4

There are few things you need to take care for in order to do this. I did some research, and this is what I've found out:

This part is taken from Python's docs:

After the build command runs (whether you run it explicitly, or the install command does it for you), the work of the install command is relatively simple: all it has to do is copy everything under build/lib (or build/lib.plat) to your chosen installation directory.

If you don’t choose an installation directory—i.e., if you just run setup.py install—then the install command installs to the standard location for third-party Python modules. This location varies by platform and by how you built/installed Python itself.

Most Linux distributions include Python as a standard part of the system, so prefix and exec-prefix are usually both /usr on Linux. If you build Python yourself on Linux (or any Unix-like system), the default prefix and exec-prefix are /usr/local.

prefix and exec-prefix stand for the directories that Python is installed to, and where it finds its libraries at run-time.

You have a way for alternating the install location, up to a certain point: you can alternate the base dir, but not the installation scheme

The Distutils install command is designed to make installing module distributions to an alternate location simple and painless. The basic idea is that you supply a base directory for the installation, and the install command picks a set of directories (called an installation scheme) under this base directory in which to install files. The details differ across platforms, so read whichever of the following sections applies to you.

The idea behind the “home scheme” is that you build and maintain a personal stash of Python modules. This scheme’s name is derived from the idea of a “home” directory on Unix, since it’s not unusual for a Unix user to make their home directory have a layout similar to /usr/ or /usr/local/. This scheme can be used by anyone, regardless of the operating system they are installing for.

python setup.py install --home=<dir>

The --home option defines the installation base directory. Files are installed to the following directories under the installation base as follows:

modules home/lib/python
scripts home/bin
data    home
C headers   home/include/python/distname

Then you'll need to modify Python's search path in order to locate the new location.

You can also use --prefix option which defines the installation base python setup.py install --prefix=. Read more about it here


To sum it up, you can change the home directory, but the site-packages hierarchy will be built in it.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • Thanks a lot for your detailed answer, that helps a lot! I will still try to find another solution but in the worst case I have to simply accept that – HelloWorld Oct 16 '17 at 22:49
  • Hard to imagine you'll get a more thorough answer than this. Can you specify what you think is missing from it? – charlesreid1 Oct 17 '17 at 11:37
  • @HelloWorld is using `virtualenv` an option? – Chen A. Oct 17 '17 at 11:55
  • 1
    @charlesreid: Seems my comment was not clear. With "to find another solution" I mean to find a complete different approach how to circumvent the problem at the root in general, but the answer is perfect and answers my question – HelloWorld Oct 17 '17 at 17:09
  • I try to avoid the virtualenv dependency, but I will try it out – HelloWorld Oct 17 '17 at 17:11
1

To add new site-packages directory add that new directory path to the path configuration file.

The path configuration file will let you add an additional site-packages directory. If you don't want existing site-packages directory you can remove it from the PYTHON_PATH.

echo "new_site-package_directory" > your_site_packages_path/usrlocal.pth

Poorna Prudhvi
  • 711
  • 7
  • 22