Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 964
HelloWorld Avatar asked Oct 06 '17 16:10

HelloWorld


People also ask

How do I change where Python packages are installed?

CAUTION: Make sure you check option Add Python 3.5 to PATH . To change install location, click on Customize installation , then Next and enter C:\python35 (or another appropriate location) as the install location. If you didn�t check the Add Python 3.5 PATH option earlier, check Add Python to environment variables .

How do I install Python packages from setup py?

Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

Where should setup py be located?

The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.


2 Answers

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.

like image 178
Chen A. Avatar answered Oct 21 '22 18:10

Chen A.


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

like image 1
Poorna Prudhvi Avatar answered Oct 21 '22 16:10

Poorna Prudhvi