ImportError: No module named spwd - python

Could someone help, please? I can't understand why I see this.
I'm trying to use spwd module from Python. I just imported it, but see this, absolutely unexpected, error when running:
Traceback (most recent call last):
File "./library/system_users.py", line 25, in <module>
import spwd
ImportError: No module named pwd
I thought it should be available by default.
Python 2.7.10
pip 8.1.1 from /Library/Python/2.7/site-packages (python 2.7)
OS X El Capitan 10.11.4
I know sometimes pip install --upgrade pip can help. Unfortunately, not now.
Thank you for the advance.

As you guessed in your comment on this question, spwd is a built-in module, so it is usually built as you compile Python. For this reason, I would guess that spwd simply isn't supported on the system you are using—i.e. OS X 10.11—unless whoever built your installed version of Python made a configuration mistake or manually disabled the module.
However, we can do more than guess about this. When building Python, much of the system-specific configuration is handled in a script called setup.py. Support for many built-in modules is determined in the detect_modules method of the PyBuildExt class; the function contains the following code (taken from here).
if (config_h_vars.get('HAVE_GETSPNAM', False) or
config_h_vars.get('HAVE_GETSPENT', False)):
exts.append( Extension('spwd', ['spwdmodule.c']) )
else:
missing.append('spwd')
We can see that the function adds the Extension 'spwd' (which consists of the spwdmodule.c source file) to the lists of extensions only when config_h_var has either HAVE_GETSPNAM or HAVE_GETSPENT. The strings in config_h_vars correspond to the macros that are defined in the pyconfig.h header file that is generated when you run Python's configure script before compiling.
According to comments generated with the pyconfig.h file, HAVE_GETSPNAM is defined (to 1) if your system has the getspname function, and HAVE_GETSPENT is defined (again, to 1) if your system has the getspent function.
The problem is that OS X 10.11 (and, seemingly, newer versions of macOS) have neither of these functions. (The manual for gnulib confirms this, albeit for an older version of Mac OS X. See this page and this page.) Thus, when compiling Python on OS X/macOS, the spwd is not built, and so you see an ImportError when you try to import it on that system.
dooms's answer suggests installing spwd from Anaconda. Of course, this isn't expected to work since spwd is a built-in module, not a package you would expect to install from a package manager. I also don't see a spwd package on PyPI or any Anaconda channels.
However, since Python does not seem to prevent you from installing a module named spwd (at least when you don't have the built-in spwd), it is conceivable that you could install some sort of drop-in replacement spwd made for macOS. macOS and iOS use /etc/master.passwd instead of /etc/shadow, but maybe it might still be possible to make something work.
Unfortunately, as of this writing, it doesn't seem like any such replacement exists for macOS. I would treat any software that requires spwd as incompatible with macOS.

You should use Anaconda.
Anaconda is a completely free Python distribution (including for commercial use and redistribution). It includes more than 400 of the most popular Python packages for science, math, engineering, and data analysis. See the packages included with Anaconda and the Anaconda changelog.
After that, just type :
conda install spwd

Related

How to detect whether a third-party python package is available on a fixed python version like python3.4

I am embedding python code in my cpp application to provide python-code-runner service. Recently I encountered a problem when I really need to downgrade my python version from 3.8.10 to 3.4.4, so that my application can fit on windows xp.
I used to provided some automatically-installed python 3rd-party packages for my customers to use. Now, as the downgrade of python version, I have to downgrade the 3rd-party packages' version to let my app function fine.
The problem is that I have no idea how to determine whether a 3rd-party package is suitable for python3.4. Is there a way I can test it?
I searched for pypi to get a version suitable for python3.4, but it turns out that this does not work well, as some 3rd-party-libs may rely on other packages with a semanticVersion mark >=.
Take astroid==1.3.8 for an example. I googled and found this version satisfies 3.4.4, so I installed it, and found syntax error when I use import astroid in my python script. The error indicates a syntax error in the lib logilab-common.
File "C:\Python34\lib\site-packages\astroid\__init__.py",line 54, in <module>
from astroid.nodes import *
...... // some traceback
......
File "C:\Python34\lib\site-packages\logilab\common\deprecated.py", line 105
self._proxied: Any = proxied
^
SyntaxError: invalid syntax
It turns out that the package astroid requires logilab-common>=0.63.0, and when I use pip install astroid==1.3.8, it installed the latest logilab-common, which is 1.9.7, and it used a type annotation, which caused this syntax error.
I managed to install logilab-common==0.63.0, and it worked fine when I use import astroid.
Does that mean I can use astroid all fine in python 3.4.4? I am not quite sure whether other functions or apis in this package will raise some syntax error or crash unexpectedly. Is there a way I can test it?
In the case, always installing the minium-satisfied-version of dependency-libs definitely works fine, but that's too much work for me. I want to find a way to test the utility of the libs and their dependency-libs.

Cannot use numpy, pandas, matplotlib on my new mac

I just changed my computer and it seems like I cannot run my machine learning code anymore. Here are some examples:
main.py file
import matplotlib
...
...
command
File "main.py", line 1, in <module>
import matplotlib
ModuleNotFoundError: No module named 'matplotlib'
Here is just one example of matplotlib. Although I can found this module in pip list, I cannot successfullt plot the graph. Anyone knows why?
Do yourself a favor... before you start installing things and creating symlinks, go install Homebrew. Once you have that installed, use it to install Python3 (it should install under /usr/local/bin). Once that is set, open your .bash_profile ($HOME/.bash_profile`) and add to the end:
PATH=/usr/local/bin:$PATH
Now the Homebrew version of your Python binaries will be found first.
Apple has added an "extra special feature" to MacOS called System Integrity Protection which prevents you from being able to edit / replace files in certain locations, which includes /usr/bin. This means you can't go in there and replace the default Python2 binaries with something newer. Only option is to do what I've suggested, or something similar (well, that or just live with Python2, but that's not a very good option).
EDIT:
Please do not install anaconda and Python from Homebrew (I personally do not like Anaconda, YYMV). Go with one or the other, or unpleasant library conflicts surely will be in your future.

Illegal instruction: 4 when importing python pandas

Since my Macbook with an i7 CPU is currently with AppleCare, I am now working on an older Mac mini with a core duo CPU. I simply connected the Macbook's internal disk via USB to the Mac mini.
Now back at my Python scripts, I ran into a problem which I don't fully understand and do not know how to debug. When I import pandas in Python 2.7.9, Python crashes completely and I get the error Illegal instruction: 4. After some googling I assume, that some packages are compiled for the wrong architecture. But I don't know which ones.
I installed Python, numpy and scipy with homebrew and pandas, etc. with pip into a virtual environment. My system is OS X 10.10.5.
The output of python -vc "import pandas" is very long and given here.
I tried re-installing Python, pandas, numpy, and scipy.
How can I find out which package is causing the error?
Do I need to set an architecture flag or something?
How can I fix this?
Removing the .pyc files might work too.
Since it happens right after the call to
dlopen("/usr/local/lib/python2.7/site-packages/matplotlib/_pabc.so", 2);,
you can try checking the arch type that file was built for with:
file /usr/local/lib/python2.7/site-packages/matplotlib/_pabc.so
then check the arch type of your hardward:
uname -a
If the shared object file (_pabc.so) was not built for that machine you may need to re compile/install/whatever, matplotlib or one of its dependancies.
In my recent experience, this was indeed caused by a linked library being of the wrong architecture as the module's library (as chown suggested).
In particular, a C-compiled python library as part of the python module you're importing (the _mymodule.so file in the module directory) calling a linked system library (eg. libgfortran.dylib), and there being an architecture mismatch between the two.
As aforementioned, you can check the architecture of your system with uname -a and check the arch of an offending dylib via the file /path/to/lib.dylib command.

Python 2.6 ImportError: No module named argparse

I'm trying to run git-cola from Red Hat Enterprise Linux Server release 6.5 and receive:
Traceback (most recent call last):
File "....../bin/git-cola", line 24, in <module>
from argparse import ArgumentParser
ImportError: No module named argparse
I think I have all of the required packages installed:
* git-1.7.1-3.el6_4.1.x86_64
* python-2.6.6-51.el6.x86_64
* PyQt4.x86_64 0:4.6.2-9.el6
* /usr/lib/python2.6/site-packages/argparse-1.2.1-py2.6.egg
I read in other blogs that there may be a problem with Python 2.6 and may need to move to 2.7.
Additional information - #iljau noticed that argparse is in a 32 bit lib, while the rest of python is in 64 bits. I would have expected that:
easy_install argparse
would have sorted this out and installed the 64 bit version.
Additional question: Does anyone know how to install the 64 bit version of argparse. It is not apparent to me from searching the internet. I will continue looking.
I installed argparse by downloading the tar file and
python setup.py install
However, it still installed it in the lib rather than lib64 path - and it looks like a 64 bit install. So something else must be the problem in referencing argparse properly. I don't know Python enough to debug it, but I suspect that git-cola needs some work for Python 2.6.
As a simple solution copy argparse.py from https://code.google.com/p/argparse/source/browse/argparse.py to your project folder.
And indeed, for Python 2.6 argparse needs to be installed separately.
From: https://pypi.python.org/pypi/argparse
As of Python >= 2.7 .. the argparse module is maintained within the Python standard library. For users who still need to support Python < 2.7 .. it is also provided as a separate package, which .. also supports older Python versions.
But even after you install argparse, it may refuse to work for some mysterious reasons.
Additional debugging tips may be found in answers and comments to question "ImportError: No module named argparse".
I had the same problem on RHEL6 and the solution was installing the package python-argparse.noarch:
yum install python-argparse.noarch
then everything was fine.
The new packages were installed in the path:
/usr/lib/python2.6/site-packages
Installing them with sudo left the newly installed directories and files unreadable by all. A recursive chmod to open all the installed paths as readable to all solved the problem:
chmod -R u+rwX,go+rX,go-w <new directories and files>
Looks like argparse is missing.
yum install python-argparse

stanford corenlp not working

I'm using Windows 8, and running python in eclipse with pyDev.
I installed Stanford coreNLP (python version) from the site:
https://github.com/relwell/stanford-corenlp-python
When I try to import corenlp, I get the following error message.
Traceback (most recent call last):
File "C:\Users\Ghantauke\workspace\PythonTest2\test.py", line 1, in <module>
import corenlp
File "C:\Python27\lib\site-packages\corenlp\__init__.py", line 13, in <module>
from corenlp import StanfordCoreNLP, ParserError, TimeoutError, ProcessError
File "C:\Python27\lib\site-packages\corenlp\corenlp.py", line 28, in <module>
import pexpect
File "C:\Python27\lib\site-packages\pexpect.py", line 85, in <module>
support it. Pexpect is intended for UNIX-like operating systems.""")
ImportError: No module named resource
A critical module was not found. Probably this operating system does not
support it. Pexpect is intended for UNIX-like operating systems.
Does that mean the python fork of it doesn't work in windows? Is there any way I can make it work in windows?
If not then could you suggest other alternatives that I could use in windows. I just need the parser.
This is a record of my attempts to get corenlp-python, the python wrapper for CoreNLP running on Windows Server 2012, as-is.
Disclaimer: should you only need to run an executable, check this first. Consider subprocess.
Starting out
Since corenlp-python uses pexpect fairly heavily, and that library works on UNIX only, my first thought was to find a Windows port.
wexpect.py was fairly easy to find and claims to be a drop-in replacement for Pexpect (emphasis mine):
In order to use WExpect, you must install CygWin, and then install the WExpect script into your system (dropping the py file into your working directory is usually good enough).
I’ve found the functionality is pretty much the same, so you should be able to use the PExpect manual and examples and apply them to this Microsoft Windows variant.
So I did just that, downloading and installing CygWin, then copying wexpect.py into C:\Python27\lib\ where all the other libraries were. I tried to import wexpect from a Python shell and got an error similar to when I first tried Pexpect on Windows:
ImportError: No module named pywintypes
This module requires the win32 python packages.
A critical module was not found. Probably this operating system does not
support it. Pexpect is intended for UNIX-like systems.
Et tu, wexpect?
No matter, this is standard frustration for finding equivalents. Press on.
I opened wexpect.py and saw that it would only try pywintypes on a Windows system. Logical, so I tried:
$ pip install -U pywintypes
...which failed, and led me to Google for the name of the python Win32 packages (this answer helped):
$ pip install -U pywin32
...which prompts for --allow-external and then --allow-unverified, both of which expect the package name, ergo:
$ pip install --allow-external pywin32 --allow-unverified pywin32 pywin32
Which, of course, does not work. No such package is found.
sf.net
So I head off to search for pywin32 on PyPI and realise that only a readme is left and I have to jump through four MORE hoops to get to something more substantial, then two more to find this list.
I downloaded Build 219 for Python 2.7 32-bit. At least now import wexpect doesn't puke.
What did you expect?
So I run the corenlp-python command again, and this time it's missing unidecode. This was easier to fix, and finally I got to a usable state - an error, no less, but familiar - where the path to the JARs was not correct.
OK.
When you run corenlp.py, since pexpect is invoked, remember to import wexpect as pexpect near the top and comment out the real import pexpect line, or you will get a NameError:
#import pexpect
import wexpect as pexpect
Even with Java installed, this does not seem to work, regardless of path.
$ python lib\corenlp\corenlp.py
It returns an ExceptionPexpect.

Categories