Python, Cygwin - ImportError: No module named _psycopg - python

I used the article on https://github.com/nwcell/psycopg2-windows and https://bitbucket.org/topper/psycopg2-win64-py27/overview to get psycopg2 installed and working on Python2.7 and Cygwin.
The install ran fine and i see the package in the /usr/lib/python2.7/site-packages/psycopg2-2.5.3-py2.7.egg/ directory.
However the import does not work.. Any idea on this error?
Python 2.7.8 (default, Jul 25 2014, 14:04:36)
[GCC 4.8.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/psycopg2-2.5.3-py2.7.egg/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named _psycopg
Thanks,
Manish

I was getting this exact set of messages -- but not in my local Cygwin/Babun dev environment -- I got them when I pushed to the RHEL 7 server to test there. It took two days to flush it out, and how I solved it likely doesn't apply to you. But I learned some things that probably do.
Another cause of this error is argued at great length in these bug reports (with Python and psycopg pointing fingers at each other) -- and generally not worth the read -- read on below instead:
http://bugs.python.org/issue16047
https://psycopg.lighthouseapp.com/projects/62710/tickets/201
The gist of it? Look for some updates -- it looks like they both might have made some small changes that affect this.
Anyway... this is how I installed psycopg2 in Cygwin (assuming Babun's pact installer is available):
pact install libpq-devel
pip install psycopg2
Yup, that's it! (Some envs might also require a C compiler and python*-devel.)
An alternative to installing libpq-devel is to install Windows Postgres and add the location of its pg_config file to your PATH so that pip can compile psycopg2 -- but installing libpq-devel from the Cygwin shell is cleaner and it puts its pg_config where pip can already find it.
I installed pyscopg2 from within a virtual env but that shouldn't have mattered. What might matter, however, could be one of the following:
Python version: You were using 2.7.8 (almost the latest for the time), I was using 3.4.3. Try a later version of 2.7.
psycopg2 version: You were using 2.5.3, I was using 2.6.1.
Conflicting Python installs. Check the sys.path values from within Python to make sure it looks as you would expect. This doesn't appear to be your problem according to what you posted, but I mention it because this was ultimately my problem on RHEL 7 and how Apache was loading the virtual envs. ---- (Just another note here: I ultimately resolved the RHEL problem by compiling another mod_wsgi for Python 3.4.3, as per their instructions, to match my virtual envs. The previous mod_wsgi was running under Python 2.7.5 and that version was failing to import _psycopg from a C library. Besides, I didn't want 2.7 running at all.)

Env = python 2.7.10 (cygwin distribution), Windows 7, Cygwin 64
1) pip install psycopg2 failed as I didn't have pip
debug
2) Installed pip from here: pip.pypa.io/en/stable/installing/
3) pip install psycopg2 failed due to 'no pg_config...'
4) psycopg2 installer for win (www.stickpeople.com/projects/python/win-psycopg/) failed cause I am using cygwin's python install, not the python in the machine's registry and this installer won't allow user to install anywhere else other thatn the python that is registred with Windows
(Installer succeeded, but the files were not where I needd them.)
("Copying" the files didn't work.)
5) This (https://github.com/nwcell/psycopg2-windows) pip install "worked" (gave no errors) but when I tried to import, I got the message:
"ImportError: No module named psycopg2"
6) pip uninstall psycopg2
7) Tried to pip install libpq-dev and python-dev
"Could not find a version that satisfies the requirement libpq-dev (from versions: )
No matching distribution found for libpq-dev"
8) installed Postgres & pg_config from here http://www.enterprisedb.com/products-services-training/pgdownload#windows
9) added pg_config to path
10) pip install psycopg2 now worked
11) import psycopg2 in .py file caused core dump
Stack trace:
Frame Function Args
006001A5A60 0018007261A (000FFFFE3F4, 0000000E514, 00000000000, 000FFFFDE50)
006001A5B00 00180073DD0 (00077801FD6, 00000000000, 00000000140, 00000000000)
006001A5D50 0018012EF8F (00600010540, 00000000000, 003DB05FDB0, 6FFFFF28050)
006001A6040 0018012BC70 (0000000000D, 00000000000, 00000000000, 0018013D7DA)
00000000000 0018012C139 (006001A6030, 3630303030303030, 3036303030303030, 00000
00000B)
00000000000 0018012C30C (00000000000, 10000000A8E73, 6F007000650072, 61006C0074
0072)
00000000000 00180127BFB (00000000000, 10000000A8E73, 6F007000650072, 61006C0074
0072)
00000000000 00180071169 (01000000010, 00000000000, 00000000000, 000FFFFCE00)
000FFFFAA00 00180073D11 (B64000B00630079, 00000000088, 1D17578E1C375D4, 1D17578
E16661AE)
52004F00520052 64006F00630072 (00000000088, 1D17578E1C375D4, 1D17578E16661AE, 1
D175A09055FB4F)
52004F00520052 70002E00730065 (1D17578E1C375D4, 1D17578E16661AE, 1D175A09055FB4
F, 1D175A09055FB4F)
52004F00520052 B64000B00630079 (1D17578E16661AE, 1D175A09055FB4F, 1D175A09055FB
4F, 0000000180B)
52004F00520052 00000000088 (1D175A09055FB4F, 1D175A09055FB4F, 0000000180B, 0000
0002000)
52004F00520052 1D17578E1C375D4 (1D175A09055FB4F, 0000000180B, 00000002000, 01A0
0000020)
52004F00520052 1D17578E16661AE (0000000180B, 00000002000, 01A00000020, 45001600
000000)
52004F00520052 1D175A09055FB4F (00000002000, 01A00000020, 45001600000000, 4E004
500540058)
End of stack trace (more stack frames may be present)
(The above took ~2 days.)
12) pip install pg8000 worked
13) imported pg8000 to .py file and connected to DB
(Steps 12 and 13 took ~20 minutes.)

It's mandatory to install libpq, which sometimes is not installed as part of libpq-devel. As of now, I succedded installing libpq5.

You must use Cygwin package installer to install BOTH of the two packages:
libecpg6, for psycopg2 runtime library support, without it you will get
'ImportError: No module named _psycopg' when you run your script.
libecpg-devel, for compiling psycopg2 without it you will get a lot of
such as pthread_*** errors when you install psycopg2 using pip tool.
As time goes on, those package name may be changed, but you should know what's
you deficiency.

Related

Import ldap in python gives gives "DLL load failed" error

I'm using the "import ldap" in a python code. This is on a windows 10 machine.
I installed the python-ldap module
pip3 install python-ldap
Installed the dependencies based on the instructions at Python Can't install packages
Also resolved all the pip deployment issues based on Installing python-ldap in a virtualenv on Windows
I'm now getting the following error when executing the import ldap statement. am I missing something here? Any ideas to resolve it?
thon39\site-packages\ldap\__init__.py", line 34, in <module>
import _ldap
ImportError: DLL load failed while importing _ldap: The specified module could not be found.
Visit the unofficial Python binaries page:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap
Download the appropriate WHL package for your system.
For example, if you're using Python 3.8 on an x64 system, download python_ldap‑3.3.1‑cp38‑cp38‑win_amd64.whl
(hint: do NOT download the +sasl version unless you have the Cyrus SASL code running on your system...)
Start the VirtualEnv for your project, if you're using one (C:\Users\youruser\.virtualenv\YourVirtualEnv\Scripts\activate.bat) -- if you're not, skip this step.
Then run pip3 install C:\Path\To\python_ldap_x.x.x-cpXX-cpXX-winXX.whl and this should install the Python DLL (pyd) file for you.

I deleted default python on fedora and now getting errors

I recently deleted the default python version on Fedora 31 and installed python 3.9 then made it as default, now I have multiple versions of python.
If I type: whereis python in my terminal this list appear:
python: /usr/bin/python /usr/bin/python3.9 /usr/bin/python3.7 /usr/bin/python3.9-config /usr/bin/python3.7m /usr/bin/python3.9-x86_64-config /usr/lib/python3.9 /usr/lib/python2.6 /usr/lib/python3.7 /usr/lib64/python3.9 /usr/lib64/python3.7 /usr/local/bin/python3.7m-config /usr/local/bin/python3.7 /usr/local/bin/python3.7m /usr/local/lib/python3.7 /usr/include/python3.9 /usr/include/python3.7m /usr/share/man/man1/python.1.gz /usr/src/Python-3.7.4/python
If I type pip then I get ModuleNotFoundError: No module named 'pip'
Also multiple packages are broken such as dnf, argcomplete, pip, etc.
I cannot update or install anything.
How can I solve this problem ?
Grab/Download the original python RPMs for your distro and reinstall them that way if they're not still cached under /var ....
With Python 3.9 you should use pip3...So install python3-pip.
That should do the trick
I tried many solutions and didn't work, however I ended up backing up my data and completely deleting the OS, then I downloaded the last version of fedora and restored my data on it.
thanks for your time
I ran into this unfortunate situation as well on Fedora 35. dnf, yum, and a bunch of other things broke.
I didn't manage to get Python 3.10 back through dnf, yum, or apt-get. I downloaded the rpm from https://fedora.pkgs.org/35/fedora-x86_64/python3-3.10.0-1.fc35.x86_64.rpm.html. It did require a dependency of python3-libs which I downloaded from: https://fedora.pkgs.org/35/fedora-x86_64/python3-libs-3.10.0-1.fc35.x86_64.rpm.html.
I installed python3-libs first with sudo rpm -i python3-libs-3.10.0-1.fc35.x86_64.rpm --force as there were some file writing conflicts. I ran the same command for the python3.10 rpm with the --force flag as well since there were 2 conflicts. After that, everything worked perfectly! Managed to dodge having to do a full reinstall.

Installing python bottle framework for python 3.6.1 version instead of python 2.7.1 version?

I am currently working on a mongo db assignment using pymongo, as part of it we need to install python bottle framework. I have installed bottle on the Mac using the below command:
$ pip install bottle
got the below message returned.
Requirement already satisfied: bottle in /Library/Python/2.7/site-packages
when I run a program from terminal which uses bottle, it throws the below error:
$ python hello_m101p.py
Traceback (most recent call last):
File "hello_m101p.py", line 2, in <module>
import bottle
ModuleNotFoundError: No module named 'bottle'
I noted that i also have python 3.6.4 version installed on my system. I need bottle framework to be saved or installed for this version instead of the default 2.7 version. After a quick lookup around stack overflow, I tried the below command from one the suggested answers. I get the command not found error:
sudo pip-3.6.4 install bottle
sudo: pip-3.6.4: command not found
Any help to correct this error ? I am not sure how to address this. Please let me know if the question is unclear or need more context.
The default name for pip for Python version X.Y.Z is not pip-X.Y.Z, but pipX.Y. Some linux distros use different names for their versions, but I believe on Mac, both the python.org Python installer and the Homebrew python package (and you probably have one of those two) use the default names, at least as of 2018.
If you only have one 3.Y.Z, it will probably also be available as pip3, which is convenient.
If you've got different names and are hopelessly confused, but you do know how to run your Python 3 itself, you can always use the -m flag to run pip.
Also, most Mac Python installs don't need sudo. If you don't know whether you do, try it without first. If you get a bunch of permissions errors, then you do need sudo after all, but otherwise, don't use it.
So, what you want is probably any of these:
pip3.6 install bottle
pip3 install bottle
python3 -m pip install bottle

How do I pip install pattern packages in python 3.5?

How do I pip install pattern packages in python 3.5?
While in CMD:
pip install pattern
syntaxerror: missing parentheses in call to 'print'
Shows error:
messageCommand "python setup.py egg_info" failed with error
code 1 in temp\pip-build-3uegov4d\pattern
seaborn and tweepy were all successful.
How can I solve this problem?
pip install pattern3 - Python 3.x
pip install pattern - Python 2.7.x
As of writing, Python 3.6 support is still not merged with master. However, it is available in the python3 branch.
To install via pip:
pip install https://github.com/clips/pattern/archive/python3.zip
Note that ThReSholD's answer for Python 3 (pattern3) is for a:
deprecated pattern3 repository which contains a completely different code base that is not maintained anymore
It looks like from the documentation that, for python 3, pattern is only supported in 3.6 and up. https://github.com/clips/pattern#installation
This worked for me to get pattern.en working in python 3.6:
git clone -b development https://github.com/clips/pattern
cd pattern
sudo python3.6 setup.py install
https://github.com/clips/pattern/issues/62
I had some SSL errors during installation on my mac (10.11.6) that were fixed by running this code in python (3.6):
import nltk
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
nltk.download('wordnet_ic')
apparently there's a better way to deal with ssl stuff like this fwiw:
https://stackoverflow.com/a/41351871/8870055
sanity check:
user#USDR00253 ~> python3.6
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from pattern.en import conjugate, lemma, lexeme, parse
>>>
>>> print(parse('ridden', relations=True, lemmata=True))
ridden/VBN/B-VP/O/O/ride
>>>
pattern.en finally running in python 3!
The short answer at the moment is - you can't. They haven't quite finished the port to python3 yet.
There is alleged compatibility in the development branch but the recommended manual setup didn't work for me (in virtualenv) - it fails in a different way.
https://github.com/clips/pattern/tree/development
The porting issue thread is here, spanning 2013 to yesterday:
https://github.com/clips/pattern/issues/62
The official A contributing port repo is here, but it's not finished yet (the readme says there is no Python3 support).
https://github.com/pattern3/pattern
So you could try pip install pattern3 which does install it - but it has a different package name, so you would have to modify any references to it. For me this is "impossible" as it's required by other 3rd party packages like GenSim.
UPDATE
I did get it working in the end in Python3 with Gensim, by manually installing it from the development branch as suggested and fixing up a few problems both during install and execution. (I removed the mysql-client dependency as the installer isn't working on Mac. I manually downloaded the certs for the NTLK wordnet corpus, to fix an SSL error in the setup. I also fixed a couple of scripts which were erroring, e.g. an empty 'try' clause in tree.py). It has a huge set of dependencies!
After reading more on the port activity, it seems they're almost finished and should release in a few months (perhaps early 2018). Furthermore the pattern3 repository is more of a "friend" than the official Python3 fork. They have already pulled the changes from this contributor into the main repo and they are preparing to get it released.
Therefore it should become available on pip in the main pattern package (not the pattern3 one which I presume will be removed), and there should be no package name-change problems.
For Mac OS:
brew install mysql
export PATH=$PATH:/usr/local/mysql/bin
pip3 install mysql-connector
pip3 install https://github.com/clips/pattern/archive/python3.zip
In the upgrade from python 2.x to 3.x, the print statement was made into a function call rather than a keyword. What used to be the line print "Hello world!" is now the line print("Hello world!"). So now all code written for 2.x that prints to the console does not work in version 3.x, as the compiler hits a runtime error on the print statement.
There are really only two fixes to this problem: Use version 2.x instead, or find a library built for version 3.x.
Additionally, I was facing :
"BadZipFile: File is not a zip file" error while importing from pattern.
This is because sentiwordnet which is out of date in nltk. So comment it in :
C:\Anaconda3\Lib\site-packages\Pattern-2.6-py3.5.egg\pattern\text\en\wordnet\_init.py
Make sure the necessary corpora are downloaded to the local drive
for token in ("wordnet", "wordnet_ic"): , "sentiwordnet"
try:
nltk.data.find("corpora/" + token)
Using Windows Subsystem for Linux, I made pattern to work using conda from (miniconda) in
Python 3.6:
conda create -n test -c conda-forge python=3.7 pattern
conda activate test
works without issues
Python 3.7:
conda create -n test -c conda-forge python=3.7 pattern
conda activate test
I discovered that there is a bug with StopInteration due to PEP-479, and replacing raise StopIteration with return in pattern\text\__init__.py fixes it.
To find the location if the file, I executed
cd $(python -c "from distutils.sysconfig import get_python_lib;print(get_python_lib())")
nano pattern/text/__init__.py
Line 605, just above class Lexicon(lazydict): ... replace raise StopIteration with return.
And all is working fine.

How do I install web2py in a virtualenv in Windows 7?

I'm new to Python and I'm trying to install web2py in a virtualenv. I'm running a 32 bit python 2.7.3 installation on a 64 bit Windows 7.
From what I read, this should be the process:
install virtualenv (done)
create the virtualenv (done)
install pywin32 in that env (I've read it needs to be installed before the web2py installation)
install web2py in that env
First and foremost: is that right? If so, then...
I tried installing pywin32 through
'''easy_install pywin32-218.win32-py2.7.exe'''
It went fine. Then, I checked the virtual environment's site-packages for a pywin32 folder and it was there, but I couldn't import it into python ('''ther's no module pywin32'''). The environment was definitely active (I double-checked).
As a test, without using virtualenv, I double-click-installed the pywin32 binary (downloaded the 32 bit version for python 2.7 -- pywin32-218.win32-py2.7.exe) but got an error at the end:
'''close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr'''
I downloaded the file again (could be corrupted), tried installing it again but got the same result.
Could anybody guide me or point me to a step-by-step install of pywin32 (if necessary) and then web2py for windows 7? I'd really appreciate it.
its a bug: http://sourceforge.net/tracker/?func=detail&aid=3402824&group_id=78018&atid=551954,
workaround here : http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/rev/a64d8ac23e61
similar to this: Error installing and running pywin32 2.7
In my experience, the command easy_install pywin32-218.win32-py2.7.exe under an active virtualenv works fine.
To test the installation of pywin32, from python you can try:
from win32api import GetVersionEx
print GetVersionEx()
Obtaining something similar to (windows major version, minor version, build number, platform id, additional informations):
(6, 1, 7601, 2, 'Service Pack 1')
You cannot import module pywin32 because it doesn't exist.
After the installation of pywin32, you can install web2py under the active virtualenv (I tested it until to opening the admin interface, no guarantee after this :-):
download the source code from http://www.web2py.com/init/default/download
unzip it to ...\virtualenvdir\Lib\site-packages\ (obtaining: ...\virtualenvdir\Lib\site-packages\web2py\...)
cd to ...\virtualenvdir\Lib\site-packages\web2py
start the web2py server: python web2py.py
At the date, I couldn't install web2py using pip. First of all, it grabs an old version of web2py from PyPy. When I tryed to use this version of web2py I got an error ImportError: No module named .... The same using pip on a copy of the web2py source file.

Categories