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?
-
have you set the proper path. import sys; sys.path.append(your_path)theBuzzyCoder– theBuzzyCoder2017-06-16 05:34:00 +00:00Commented Jun 16, 2017 at 5:34
-
You don't need special permissions when you use a virtual environment for your packages.Klaus D.– Klaus D.2017-06-16 05:42:34 +00:00Commented 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.Aakash Goyal– Aakash Goyal2017-06-16 07:41:28 +00:00Commented 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.Aakash Goyal– Aakash Goyal2017-06-16 07:42:59 +00:00Commented Jun 16, 2017 at 7:42
Add a comment
|
1 Answer
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.