".c" files called by fiona. How to import them? - python

I downloaded fiona today. when I try to import it in Python using 'import fiona', I get the following error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import fiona
File "C:\Python27\lib\site-packages\fiona\__init__.py", line 72, in <module>
from fiona.collection import Collection, supported_drivers, vsi_path
File "C:\Python27\lib\site-packages\fiona\collection.py", line 7, in <module>
from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
ImportError: No module named ogrext
I checked in my site-packages folder, and ogrext is a "C" file. I tried commenting out the import to see if it wasn't necessary, but this of course threw another error.
Specifically, how do I resolve this import error?
More generally, how does one resolve errors involving importing C files into a python library?

You can't just install any module by copying all the files to site-packages. Some modules are pure Python, but there are many with extensions written in other languages (C, C++, Fortran, etc.) that need to be compiled and linked into libraries before being used, and fiona is one of them. This compilation can be done at several stages - by the author, before distributing the module as a wheel, during the pip install process, or by downloading the package's source, unzipping/tarring it, and running python setup.py install. Unfortunately, Windows doesn't come with a compiler by default, so you either need to install and configure your system for gcc or Visual Studio, or use another method, such as a precompiled installer. Fortunately, fiona is available from Christoph Gohlke's Python Extension Packages for Windows Repository here. Download the installer for your version and bit-ness of Python, delete the fiona folder in site-packages, then run the installer. This site contains a large number of packages for scientific computing, and is my go-to resource when I need to install a new module, especially if it has extensions.
EDIT
Upon further inspection, it appears that fiona also requires the GDAL module, as well as six, both of which can be downloaded from Gohlke's repository. I first installed fiona only (I already had six installed), and got a missing DLL error. I then installed GDAL, and import fiona worked just fine - I'm not familiar with the module, so I didn't do any further testing, but hopefully everything should work now.

Related

ImportError Pyo

I'm trying to import pyo in python3.8.8 and I'm not getting any results. When I try to run the command:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in
import pyo
File "C:\Users\andre\AppData\Roaming\Python\Python38\site-packages\pyo_init_.py", line 28, in
from .lib import analysis as analysis
File "C:\Users\andre\AppData\Roaming\Python\Python38\site-packages\pyo\lib\analysis.py", line 32, in
from ._core import *
File "C:\Users\andre\AppData\Roaming\Python\Python38\site-packages\pyo\lib_core.py", line 58, in
from .._pyo import *
ImportError: DLL load failed while importing _pyo: Impossibile trovare il modulo specificato.
How can I solve this?
I assume you are on windows. What version of pyo do you have installed? I saw that it was only version 1.0.1 that python 3.8 support was introduced. Try uninstalling pyo, then make sure you install the newest version. Also, did you install with pip? If you did, try installing from their downloadable releases on github. What code did you write that caused this error? I need to see your code.
Looks like you're using pc032 (032bit) Python. Last PyO version with pc032 (on Win) support is [PyPI]: pyo 1.0.1 (from 191127). But I didn't see the Python 3.8 .whl among files (neither for a couple of older versions).
Just out of curiosity: how did you install PyO?
In order to get things going either:
Switch to pc064 (064bit) Python
Use a (pc032) Python version that prebuilt .whl exists for
I've built v1.0.4 (latest at answer time) and placed the .whls at [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyO/v1.0.4. Download and install the preferred one (check [SO]: Installing pygraphviz on Windows 10 64-bit, Python 3.6 (#CristiFati's answer) (at the end) for more details about the process)

Pandas Import : ModuleNotFoundError: No module named 'pandas._libs.tslib'

Whenever I try to import pandas, whether inside a virtualenv or otherwise I am always getting this error.
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
Traceback (most recent call last):
File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 26, in <module>
from pandas._libs import (hashtable as _hashtable,
File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\_libs\__init__.py", line 3, in <module>
from .tslib import iNaT, NaT, Timestamp, Timedelta, OutOfBoundsDatetime
ModuleNotFoundError: No module named 'pandas._libs.tslib'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 35, in <module>
"the C extensions first.".format(module))
ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
I tried the follwoing solutions:
Cloning pandas from git and running SETUP.py (on an instance of python 3.6 installed directly into my win10 os)
Using anaconda as python distribution and conda to install pandas
Updating microsoft visual c++ 2017 redistributable
Updating C:\ProgramData\Anaconda3\Lib\site-packages\PyInstaller\hooks\hook-pandas.py
None of these seem to work. Please help me understand what the issue here is.
This exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 35, in <module>
"the C extensions first.".format(module))
ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first
suggests that pandas was not built properly during installation.
The latter sentence:
If you want to import pandas from the source directory, you may need to run python setup.py build_ext --inplace --force to build the C extensions first
Is really only something you ought to be doing if you are contributing to pandas source code (e.g. to fix a pandas bug or add a feature to pandas itself) to the pandas-dev github repository*.
Most likely you shouldn't be building from source in your project.
Generally anaconda is pretty good at installing pandas correctly, and so my guess/comment was:
My guess is the virtual env is not using anaconda, and the install of pandas is messed up (perhaps created before installing anaconda?). I would delete this directory C:\Users\ishan\AppData\Roaming\Python\Python36 and see if that helps
The reason I suggested that directory was because it was in the error message AND it doesn't look like somewhere I expect anaconda's installation of pandas to be (either generally or as a virtualenv).
* Note: this is something fun to do, to give back to the pandas community: there's some low-hanging fruit, typos or code changes, so I recommend investigating whether there's any way you can contribute.
If you are using a Conda distribution (e.g., AnaConda, MiniConda), as it seems to be the case, uninstalling and reinstalling Pandas may help.
Run the following commands on the cmd console:
conda uninstall pandas
conda install pandas
I run into same error when setting up python, keras and anything between.
Background:
I installed anaconda and followed instructions by https://www.youtube.com/watch?v=z0qhKP2liHs and instruction to downgrade to python 3.6 by http://docs.anaconda.com/anaconda/user-guide/faq/#how-do-i-get-the-latest-anaconda-with-python-3-5
Running from Jupyter I run into same problem as author
I was able to solve my problem by:
- uploading pandas version for python 3.6 per https://docs.anaconda.com/anaconda/packages/py3.6_win-64/
then I run python from command line .. it worked
then I tested with PyCharm .. it worked
Appears that either Anaconda&Jupyter combination did not work or selecting pandas version did the job.
since tslib has been deprecated for the latest version of pandas. try to remove the pd.tslib.Dataframe and replace with pd.DataFrame where ever tslib is present in ggplot library. it works !!
you can find the packages in the lib file of ggplot folder.
Thank you!
Not sure if this is anything like an exhaustive answer, but seems related.
I came by this question as I was building Python from source (were you too perhaps?) and using that then to build an installer for my app - at some point I managed to get the exception from the OP:
ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
What I then did was trying to import the module in question from a newly opened Python REPL (i.e. import pandas._libs.tslib). It turned out to be a useful effort as what I there got was an error likely about a missing _bz2.
By then I had already been through installing a good number of Linux packages that needed to be present prior to running the Python's ./configure so that they get included (for pip to operate well, for instance), and it seemed obvious that I just missed yet another package.
Indeed, as found to be suggested here in response to that error, a simple
sudo apt-get install libbz2-dev
and re-running ./configure, make, make install put me into a situation when finally pandas was willing to be loaded.
However, now it complains for the missing lzma extension :) such is life:
UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
From my perspective this finally seems like a soft error (app starts yay!)
My speculative explanation is that due to the lack of bzip2 available for Python at the time building and then this being missing, Pandas gives a slightly misleading error and fails to load, whether or not it correctly assesses that to be a show-stopper error. Possibly it's such a rare situation that from a time when it was commonplace/intuitive to fix, they may have stopped maintaining it and now it isn't telly enough?
Update: in case anyone wondered, the lzma dependency warning can be alleviated via sudo apt-get install liblzma-dev see UserWarning: Could not import the lzma module. Your installed Python
is incomplete and rebuilding in my case Python and then the PyInstaller packaged app, by the way somewhere along the sequence this includes reinstalling Pandas.

Accidentally installed two versions of Python 3.4.1... Can't run any scripts, no modules can import, how can I fix this?

I was having problems with a 'zlib' import error on Python3.4.1, so I followed the instructions here, but instead of doing it for Python 2.6 I did it for 3.4. I had not uninstalled my existing version of Python3.4 before doing this new installation process. I can now import zlib, however all of my custom packages cannot be imported, such as sklearn, scipy, numpy, flask, and pandas.
First, I get an import error:
>>> import scipy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'scipy'
So, I try to remedy the issue with:
pip3 install scipy
But, I am greeted with:
Requirement already satisfied: scipy in /usr/local/lib/python3.4/dist-
packages
Requirement already satisfied: numpy>=1.8.2 in
/usr/local/lib/python3.4/dist-packages (from scipy)
I tried running echo $PYTHONPATH but it returned blank.
I can import sys, os, time, and zlib just fine. It just won't import all of the dependencies that I already have on my computer.
I tried deleting the folder in /tmp where I installed the "newer" Python3.4.1 folder, however this did not work. I don't know how to fix this issue.
I can still run python scripts in an anaconda virtual environment that I had created a while ago, but I can't use virtualenv's, which is a problem since I use zappa a lot (which requires an active virtualenv).
This is what I get when I try to run a virtualenv:
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007fb1bc7f4740 (most recent call first):
Aborted (core dumped)
The python version that contains all of my dependencies in dist-packages is located in /usr/local/bin/python3.4. How can I make it so that when I run python3 that it will use the dependencies from that folder? Is the fact that the pythonpath variable is blank bad?
I just want my old python back.
PS. Everything works fine for python2. It's just causing these issues for python3.
I had a similar issue, but with another package. In my case it turned out that I had a egg-info file present in the site-packages without the directory for the package. Deleting the egg-info file (actually I moved it first) allowed a clean install to occur.

OpenCV in Python: "ImportError: DLL load failed"

So many questions already about this topic, but I didn't find any satisfying answer about the "DLL not found" traditional issue.
I'm using Python 3.5 installed via Anaconda 3;
I have installed with pip the 'official' opencv_python-3.2.0.6-cp34-cp34m-win_amd64.whl (right from here https://pypi.python.org/pypi/opencv-python);
Windows version is 7 - 64 bits;
I have uninstalled all the redistributable VC++ that were present on my machine and reinstalled its 2015 version - so the famous as well as mandatory DLL msvcp140.dll is present on my laptop;
the cv2 module is present in the Lib/site-packages directory;
it contains, among others, file 'cv2.cp35-win_amd64.pyd'.
Meanwhile, I still can't get access to cv2:
>>> import cv2
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import cv2
File "C:\Program Files\Anaconda3\lib\site-packages\cv2\__init__.py", line 7, in <module>
from . import cv2
ImportError: DLL load failed: The specified module could not be found.
Modifying the name of the .pyd --> cv2.pyd does not solve the problem, nor does the installing via pip of the unofficial but great wheel version from Gohlke (http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv)
Any idea about what is going on?
As mentioned in the comment which I didn't notice at first, you can resolve this by installing from the unofficial site (Gohlke)
(http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv)
I uninstalled the official one by calling:
pip uninstall opencv-python
And then downloaded the .whl package from the mentioned site (you need to carefully choose the correct python version and the correct processor architecture x86 or x64).
And then, go to the path where the .whl file is downloaded and call:
pip install opencv_python-x.x.x-cpxx-cpxxm-win_xx.whl
Regarding the comment in the question, always remove any existing opencv package before installing another one because, opencv from unofficial and official sites will be considered as 2 packages by pip.
You can install official opencv if you follow my answer here. You get that error because of a problem with Anaconda.
But if you install official opencv it will autocomplete and give suggestions in your python IDE (see this). So, if you need autocomplete as well, you need to go with unofficial opencv.
I had the same issue. This helps me:
conda install -c menpo opencv3
Found at https://www.scivision.co/install-opencv-python-windows/

Creating Python Wheels for libraries like numpy

We are currently running our own private PyPi server, and uploading wheels of our internal Python libraries to speed up installs.
Many of our tools require numpy, scipy, pandas, etc.
We built wheels for all of our dependencies by installing all of our dependencies from source, and then using
pip wheel .
which outputs wheels for all of our dependencies - or so we thought.
We have found that when we install, say, numpy from our private PyPi, where it is available as a wheel, the following happens.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "redacted/lib/python2.7/site-packages/numpy/__init__.py", line 199, in <module>
from . import random
File "redacted/site-packages/numpy/random/__init__.py", line 99, in <module>
from .mtrand import *
ImportError: redacted/lib/python2.7/site-packages/numpy/random/mtrand.so: undefined symbol: PyFPE_jbuf
It is clear to me that these wheels were not created properly. What is not clear to me is how to go around fixing this. We are not distributing these to the public, only for our internal tools.
I am far from being an expert on wheel packaging but here is what I know about wheel building so far.
For OS X and Windows you can build binary wheels and use the to deploy your software.
On Linux this process doesn't always work and that's because many binary wheels will try to use specific versions of OS libraries (.so) and these are different from one distribution to another, or even inside the same distribution.
Here is some further reference:
http://lucumr.pocoo.org/2014/1/27/python-on-wheels/

Categories