Install sudo and pg_config

os-linux
There seems to an issue using sudo when installing from source or
pip. setup.py complains that pg_config cannot be found on the
path.
Checking the PATH shows that the directory for pg_config is indeed
there.

My usual install is:

python setup.py build

This runs and the code finds the appropriate directories to build against.

sudo python setup.py install

This is where I get the error.
Doing:
sudo python setup.py build_ext --pg-config /path/to/pg_config install
makes it complete.

If I try

sudo python setup.py build

it also fails.

If I sudo su - to root then I can do the build and install with
no problem.

The PATH for root also contains the pg_config directory. As I
said in the original problem report, when I run build/install
entirely as root there is no problem. It is the usage of sudo that
cause the issue.

Have been doing some digging around.
What I found so far.
In the sudoers file there in a setting env_reset that restricts the
PATH when using sudo. There are other settings that affect PATH
also and I don't think I will touch any of them.

This I can see if I:

print os.environ['PATH']

in

def find_on_path()

So the root or user PATH do not apply.

This led me to the wonderful world that is distutils.
Just ran across this article which is proving helpful:http://www.python.org/files/sigs/sc_submission.html
With what I have learned from it so far, I started stepping through
the build/install process.
The PATH error crops up in the build_ext stage. The install command
steps through that on its way to actually doing the install.
Now I have to figure out how to handle that.

Well that is not going to work. The various steps seem to take
place as discrete actions with no state saved. I thought of using
the pg_config setting in setup.cfg as a way of preserving state.
The thing is I can not see where that setting is read in by the
parser. The same goes for the include_dirs settings. Am I missing
something? Still might experiment with that route, just want to
make sure I am not stomping on some other process.

As soon as sudo is invoked the PATH gets restricted.
My personal short term solution is to put the following in
~/.bashrc

alias sudo='sudo env PATH=$PATH'

This adds the regular user PATH to the sudo path
temporarily.
To make this happen in the script would involve invoking a process
as the regular user to collect the ENV variables.
Not sure I am up to that yet.

the error is just moved from the PostgresConfig
constructor to the pg_config_helper.query("libdir")
below.

I don't see a way to detect build_ext has no work
to do before finalize_options() is invoked and I'm not
up to the distutils rabbit hole either. So I'll leave the ticket
around just in case somebody feels this problem deserves a solution
and shows such solution exists.

Thanks for looking at it. I am still groping my way through
distutils to see if there is a solution.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="https://github.com/psycopg/psycopg2/issues">the new tracker</a>.
<br/>
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.