0

I want to use psycopg2 for db operations. But as the code will be packaged and sent, I dont have to permission to install it on the remote server where it will be used. Can you please tell how to use the code directly from the tar.gz bundle? I have put the path in site-packages, but still getting ImportError. Can anyone please tell how to do that for psycopg2 and for that matter, any python package we want to use directly without installing, just by using the source code?

4
  • have you set the proper path. import sys; sys.path.append(your_path) Commented Jun 16, 2017 at 5:34
  • You don't need special permissions when you use a virtual environment for your packages. Commented Jun 16, 2017 at 5:42
  • @KarthikBhatK: Yes, I did it via site packages. But the structure inside the downloaded tar is different from the usage mentioned. Commented Jun 16, 2017 at 7:41
  • @KlausD.: The local machine we have here doesn't have pip and all installations are blocked. So, that is why looking for a way to directly use the tar bundle. Commented Jun 16, 2017 at 7:42

1 Answer 1

1

For most python packages/modules, you should be able to build the module without installing it:

python setup.py build

and copy the built module/package from ./build directory and use it. In case of psycopg2 package, I started with cloning the psycopg repository:

git clone [email protected]:psycopg/psycopg2.git

and then built it:

cd psycopg2
python setup.py build

I then copied the package directory to a new directory where my own code lives:

cp -a build/lib.linux-x86_64-3.6/psycopg2 ~/myproject

and imported it:

cd ~/myproject
python

Python 3.6.1 (default, Mar 27 2017, 00:27:06) 
[GCC 6.3.1 20170306] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2

Note that psycopg2 is a C binding and hence compiled code when built. Therefore the build package runs only on machines with the same architecture as your own.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.