Using absolute_import and handling relative module name conflicts in python [duplicate]

2024/9/19 9:46:18

I really hope this is a simple case of me miss-understanding the complex Python2 import mechanisms. I have the following setup:

$> ls -ltr pypackage1 
total 3
-rw-r--r-- 1 pelson pelson   0 Aug 17 19:20 io.py
-rw-r--r-- 1 pelson pelson   0 Aug 17 19:20 __init__.py
-rw-r--r-- 1 pelson pelson  57 Aug 17 19:22 code.py
$> cat pypackage1/code.py 
from __future__ import absolute_importimport zipfile

i.e. I have nothing but a stub package with an empty __init__.py and io.py, and a 2 lines code.py file.

I can import pypackage1:

$> python -c "import pypackage1.code"

But I cannot run the code.py file:

$> python pypackage1/code.py
Traceback (most recent call last):File "pypackage1/code.py", line 3, in <module>import zipfileFile "python2.7/zipfile.py", line 462, in <module>class ZipExtFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'

Clearly the problem has to do with the zipfile module picking up my relative io module over the builtin io module, but I thought my from __future__ import absolute_import would have fixed that.

Thanks in advance for any help,

Answer

That's the correct behaviour. If you want to fix the error simply do not run from inside the package.

When you run a script which is inside the package, python wont interpret that directory as a package, thus adding the working directory to the PYTHONPATH. That's why the io module imported by the zipfile module is your io module and not the one inside the standard library.

I'd recommend to create a simple launcher script outside your package (or in a bin/scripts folder), and launch that. This script can simply contain something like:

from pypackage1 import codecode.main()

An alternative to this is to tell the python interpreter that the file that you want to execute is part of a module. You can do this using the -m command line option. In your case you would have to do:

python -m pypackage1.code

Note that the argument of -m should be the module name, not the file name.

https://en.xdnf.cn/q/72855.html

Related Q&A

Setting results of torch.gather(...) calls

I have a 2D pytorch tensor of shape n by m. I want to index the second dimension using a list of indices (which could be done with torch.gather) then then also set new values to the result of the index…

Why does PyCharm use double backslash to indicate escaping?

For instance, I write a normal string and another "abnormal" string like this:Now I debug it, finding that in the debug tool, the "abnormal" string will be shown like this:Heres the…

Execute Shell Script from Python with multiple pipes

I want to execute the following Shell Command in a python script:dom=myserver cat /etc/xen/$myserver.cfg | grep limited | cut -d= -f2 | tr -d \"I have this:dom = myserverlimit = subprocess.cal…

Python: Is there a way to split a string of numbers into every 3rd number?

For example, if I have a string a=123456789876567543 could i have a list like...123 456 789 876 567 543

How to override the __dir__ method for a class?

I want to change the dir() output for my class. Normally, for all other objects, its done by defining own __dir__ method in their class. But if I do this for my class, its not called.class X(object):de…

Equivalent of wget in Python to download website and resources

Same thing asked 2.5 years ago in Downloading a web page and all of its resource files in Python but doesnt lead to an answer and the please see related topic isnt really asking the same thing.I want t…

Python plt: close or clear figure does not work

I generate a lots of figures with a script which I do not display but store to harddrive. After a while I get the message/usr/lib/pymodules/python2.7/matplotlib/pyplot.py:412: RuntimeWarning: More than…

PCA of RGB Image

Im trying to figure out how to use PCA to decorrelate an RGB image in python. Im using the code found in the OReilly Computer vision book:from PIL import Image from numpy import *def pca(X):# Principa…

delete the first element in subview of a matrix

I have a dataset like this:[[0,1],[0,2],[0,3],[0,4],[1,5],[1,6],[1,7],[2,8],[2,9]]I need to delete the first elements of each subview of the data as defined by the first column. So first I get all elem…

How to scroll QListWidget to selected item

The code below creates a single dialog window with QListWidget and QPushButton. Clicking the button fires up a scroll() function which finds and selects an "ITEM-0011". I wonder if there is a…