Search code examples
pythoninstallationsetup.pycpythonsite-packages

Use another site-packages directory by default for setup.py install


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?


Solution

  • 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.