LaVOZs

The World’s Largest Online Community for Developers

'; python - Module File I/O is not found despite being installed - LavOzs.Com

I'm trying to access some data from a public source, however I have trouble getting the module fileio to work despite installing it with pip. Here's my code:

from fileio import read
import gzip
odffn = 'test-data/Level1_IC59_data_Run00115150_Part00000000.odf.gz'
f = gzip.open(odffn)
ev = read(f)
hit_dist = list()

while ev :
    # do some analysis with the event
    hit_dist.append(len(ev.hits))
    # get the next event
    ev = read(f)

import pylab
pylab.hist(hit_dist,30,range=(0,1000), log=True, histtype='step')
pylab.title('IceCube Hit Distribution')
pylab.xlabel('nhit')
pylab.savefig('nhits.png')

And I get the following error:

from fileio import read
ModuleNotFoundError: No module named 'fileio'

However, I already checked using the pip installer,

python -m pip install fileio

And I get the module is already installed. I don't think it's a problem with the PATH since it works well with all the other models (i.e. numpy), so I'm not really sure what could be the problem. I appreciate in advance any insight.

I looked in pip for fileio and from what I can see, this doesn't appear to be a legitimate package. When installed from pip it doesn't install any importable Python modules or packages. All it does it create a skeleton directory under site-packages.

I think you should step back and re-evaluate what this code is doing:

from fileio import read
import gzip
odffn = 'test-data/Level1_IC59_data_Run00115150_Part00000000.odf.gz'
f = gzip.open(odffn)
ev = read(f)
hit_dist = list()

This seems fine (ignoring the import from fileio), up until the line: ev = read(f). What is the purpose of using this function to read the file object that gzip returns? That object has its own set of read methods that should be able to do the job:

import gzip
odffn = 'test-data/Level1_IC59_data_Run00115150_Part00000000.odf.gz'
f = gzip.open(odffn)
lines = f.readlines()

Assuming this is a text file, that should read the whole thing into a list of strings, one per line. You can also buffer it:

buf_size = 100
buf = f.read(buf_size)
while buf:
    <do something with 1-100 characters of input>
    buf = f.read(buf_size)

or buffer whole lines:

line_buf = f.readline()
while line_buf:
    <do something with a line of input>
    line_buf = f.readline()
Related
Calling a function of a module by using its name (a string)
How do I check whether a file exists without exceptions?
How do I copy a file in Python?
How can I get a list of locally installed Python modules?
How do I list all files of a directory?
How to read a file line-by-line into a list?
How to install pip on Windows?
How to add local jar files to a Maven project?
How to delete a file or folder?
How do I install pip on macOS or OS X?