Installing netCDF4 for Python fails, how to fix? - python

I have tried installing netCDF4 for Windows (from here) and I still cannot import netCDF4 modules.
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
>>> import numpy
>>> import scipy
>>> import netCDF4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named netCDF4
There are the modules in the site-packages directory:
James#desktop /cygdrive/c/Python27/Lib/site-packages
$ ls net*
netcdf.dll netCDF4_utils.pyo netcdftime.pyc
netCDF4.pyd netCDF4-1.0.4-py2.7.egg-info netcdftime.pyo
netCDF4_utils.py netCDF4-1.0.5-py2.7.egg-info
netCDF4_utils.pyc netcdftime.py
Are there further steps I need to complete after running the installer?
Thanks!

I presume from the site-packages directory name that it's Cygwin Python.
You need to ensure that the site-packages directory is on the Python module search path. I forget just how this is done--I have notes on how I did it, but they're at work and I'm at home--but there are instructions for it on the Python site. For Cygwin Python you want the Unix instructions. Something to do with adding a file called site.pth somewhere under /usr??

Related

Python: pynhd package installation problem

I'm quite new to programming and I am struggling with package installation. I am trying to utilize the hyriver/pynhd package from github https://github.com/hyriver/pynhd. I am using PythonWin 3.7.11 [MSC v.1927 64 bit (AMD64)] on win32. I am trying to access the pynhd package by using import pynhd as nhd. However, when I try to run it, it says:
Traceback (most recent call last):
File "C:\pynhd-main\tests\test_pynhd.py", line 10, in <module>
import pynhd as nhd
File "C:\pynhd-main\pynhd\pynhd.py", line 4, in <module>
import async_retriever as ar
ModuleNotFoundError: No module named 'async_retriever'
I thought that I had successfully installed async_retriever through the python cmd prompt on my computer but it still is not found. To install async_retriever I used pip install async_retriever. Might anyone have any suggestions?
My end goal is to utilize the HyRiver application in conjunction with ArcGIS.

"ELF file OS ABI invalid" using psycopg2

I've post a question, but I think the problem is maybe specific. So I create this new post.
When I test in the python's console psycopg2, I've this error :
this-user#xxx-web:~/my-folder$ python
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys,os
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/users/this-user/.local/lib/python2.7/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: /home/users/this-user/.local/lib/python2.7/site-packages/psycopg2/_psycopg.so: ELF file OS ABI invalid
>>> quit()
this-user#xxx-web:~/massifs$ ll /home/users/this-user/.local/lib/python2.7/site-packages/psycopg2/_psycopg.so
-rwxr-xr-x 1 this-user 214K mai 21 2015 /home/users/this-user/.local/lib/python2.7/site-packages/psycopg2/_psycopg.so*
If I run the python's console in root, it's working !!?? :(
Thanks for help.
F.
Note that your import is from a 'user' install, so if you run it as root, it will import something else (a different copy of the module which happens to work). You may have two installs of the psycopg2 module and the one that is in this-user is not compatible (maybe you copied it directly from another host rather than installing it with pip).
You should probably un-install psycopg2 and re-install it properly with python -m pip uninstall --user psycopg2 and then python -m pip install --user psycopg2, so you get the correct build of the .so file.

python import works interactively but not from script

Disclaimer: I'm not a Windows expert, so this may be something idiosyncratic to Windows.
I'm observing a situation where I can import a module in interactive mode but not when requested from a file. Here is foo.py in its entirety:
#!/usr/bin/env python
import pyftdi
When I try to run this in Python 3.6.4, it fails:
E:\>py foo.py
Traceback (most recent call last):
File "foo.py", line 2, in <module>
import pyftdi
ImportError: No module named pyftdi
...but when I import the module interactively, it succeeds:
E:\>py
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyftdi
>>>
(FWIW, I don't get an error when repeating this under Mac OS X.)
Any thoughts about what's going on?
The problem is most likely that the version of python is different and has different modules installed. To fix this you can simply do py -3.6 foo.py instead of py foo.py.
Working directory
I had a situation where the script was importing from it's working directory and the shell was importing from the global libraries.

How do I diagnose ImportError: DLL load failed when loading native Windows python modules in a virtualenv?

I am using Python 3.x and a virtualenv -- not conda, just a plain virtualenv.
I activate the venv and run pip install opencv-python. However,
import cv2 gives me a DLL not found error:
(tf) C:\>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\src\venv\tf\lib\site-packages\cv2\__init__.py", line 4, in <module>
from .cv2 import *
ImportError: DLL load failed: The specified module could not be found.
>>>
Is this a virtualenv bug? How do I figure out which module/dll is missing?
On resolving "module could not be found" errors in general
Try using either Microsoft's Dependency Walker or lucasg's Dependencies on the module being loaded. Be sure to run Dependencies.exe from your virtualenv's command prompt, so it picks up your modified PATH.
The import line is from .cv2 import *, so the module being loaded is in the same directory as __init__.py (this is the leading .) and named cv2-SOMETHING.pyd (this is what native Python modules look like). Load that file into Dependencies.exe and it will show you the DLL that Windows wants but can't find.
In this case, the DLL is Python3.dll. Why is it missing? Because of a virtualenv bug that is fixed, but hasn't made its way into a release -- there hasn't been a release in more than a year.
On resolving this issue in particular
The github issue suggests a fix: use venv.
Alternatively you can copy the missing python3.dll into your virtualenv by hand. You'll have to do this for every virtualenv you create.
copy "c:\Program Files\Python36\python3.dll" "c:\src\venv\tf\Scripts\"

Python 3.6 pandas import error from 'UserDict.py'

I'm running Python 3.6.4, and am getting an import error from pandas. I'm installing it with 'pip install pandas' (which installs version 0.22). Installing it seems to go fine, but when I try to import it, an error gets thrown:
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\pandas\__init__.py", line 13, in <module>
__import__(dependency)
File "C:\Python36\lib\site-packages\pytz\__init__.py", line 32, in <module>
from pytz.lazy import LazyDict, LazyList, LazySet
File "C:\Python36\lib\site-packages\pytz\lazy.py", line 3, in <module>
from UserDict import DictMixin
File "C:\Python36\lib\UserDict.py", line 167
raise TypeError, "pop expected at most 2 arguments, got "\
Any help is much appreciated! Thank you!!
Edit:
Stephen pointed out that the root problem is pytz failing to import. Importing anything form pytz throws the same error. I've found one reference to the same error (http://www.smashcompany.com/technology/weird-that-under-python-3-4-runtime-you-can-import-from-2-7), which suggested an old path variable was the problem. I don't see any outdated python variables in my PATH, however.
Unless you use a virtual environment, pip usually corresponds to Python 2.x. The convention is to use pip3 to refer to the pip binary for Python 3.x. Since you are using 3.6.4, pip may be installing pandas for the wrong version of Python. You can confirm if it's using the correct one with
pip --version
If it says that it's using an interpreter other than the one you are using to run your code, try using
pip3 install pandas
instead.
I didn't figure out what caused the problem, but I was able to fix this by manually deleting all of the python files and directories still left behind after uninstalling python (not sure if it is normal that files were left at all) and then doing a completely fresh install.
There were no import problems after a 100% fresh install. Something must have gotten messed up, and running the default 'repair' or 'uninstall' and 'install' through the python installer (of multiple versions) wasn't correcting it.

Categories

Resources