Trouble with relative / absolute functions import in scikit-image - python

I'm trying to submit a PR for scikit-image, but I get a Travis-CI error:
Traceback (most recent call last):
File "doc/examples/edges/plot_canny.py", line 22, in <module>
from skimage import feature
File "/home/travis/build/scikit-image/scikit-image/skimage/feature/__init__.py", line 9, in <module>
from .peak import peak_local_max
File "/home/travis/build/scikit-image/scikit-image/skimage/feature/peak.py", line 3, in <module>
from ..filters import rank_order
File "/home/travis/build/scikit-image/scikit-image/skimage/filters/__init__.py", line 11, in <module>
from ._frangi import frangi_filter, hessian_filter
File "/home/travis/build/scikit-image/scikit-image/skimage/filters/_frangi.py", line 2, in <module>
from skimage.feature import hessian_matrix, hessian_matrix_eigvals
ImportError: cannot import name hessian_matrix
I suppose that this might be a circular import error, but I don't quite get how to resolve the issue. I've already included frangi_filter and hessian_filter into filter's module __init__.py.
I've also tried relative import, which resulted into the same errors.
How can I do a proper import, so the circular import issue can be resolved?

One ugly hack to resolve this would be to move that import inside the function, like
def hessian_filter(image, scale=(1, 10), scale_ratio=2, beta1=0.5, beta2=15):
"""
Blah-blah-blah
"""
from ..feature import hessian_matrix, hessian_matrix_eigvals
# function body
You might want to create separate "proxy" functions for hessian_matrix and hessian_matrix_eigvals to not pollute every function with imports.

Related

Python3 error when running a pandas simple program

I'm trying to run a python program that uses the pandas library on Mac OS, and I get the next error
adan_vazquez#EPAM-C02G513RML7L automate_python % python3 test_pandas.py
Traceback (most recent call last):
File "/Users/adan_vazquez/Desktop/automate_python/test_pandas.py", line 1, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/__init__.py", line 11, in <module>
__import__(dependency)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py", line 155, in <module>
from . import random
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/random/__init__.py", line 180, in <module>
from . import _pickle
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/random/_pickle.py", line 1, in <module>
from .mtrand import RandomState
File "mtrand.pyx", line 1, in init numpy.random.mtrand
File "bit_generator.pyx", line 40, in init numpy.random.bit_generator
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/secrets.py", line 19, in <module>
from random import SystemRandom
File "/Users/adan_vazquez/Desktop/automate_python/random.py", line 3, in <module>
print(random.randint(1,10))
AttributeError: partially initialized module 'random' has no attribute 'randint' (most likely due to a circular import)
Here's the code that I'm trying to run:
import pandas as pd
simpsons = pd.read_html('https://en.wikipedia.org/wiki/List_of_The_Simpsons_episodes_(seasons_1%E2%80%9320)')
print(len(simpsons))
print("Hola")
As you can see is not a big code and I'm getting the error, I already updated my pip3 and my python3 versions, also I reinstall pandas and I keep getting the error, if I try to import pandas in the python terminal I get the same error and I don't know what's causing it, I hope you can help me, thanks in advance.
The error message says it is a circular import error.
Try renaming /Users/adan_vazquez/Desktop/automate_python/random.py with something other than random
According to the error, it is a circular import which is causing the problem.
Since I do not have the details of your project structure or file name, my best guess is that you accidentally named your working file the same as the module name and those modules depend on each other.

AttributeError: module 'datetime' has no attribute 'date' on import yaml

one of my code lines is
import yaml
which was installed on python 3.7 using pip install pyyaml
the following error arises
Traceback (most recent call last):
File
"C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/peer_changing_send_rate.py",
line 1, in
from TestPeer.TestPeerChangingSendRate import TestPeerChangingSendSpeed
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeerChangingSendRate.py",
line 1, in
from .TestPeer import TestPeer
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeer.py",
line 4, in
from BaseProcess.ZmqPeerClass import ZmqPeer
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPeerClass.py",
line 2, in
from .ZmqPublisherClass import ZmqPublisher
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPublisherClass.py",
line 10, in
from . import ZmqProcessClass
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqProcessClass.py",
line 5, in
from .ConfigBaseClass import ConfigBase
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ConfigBaseClass.py",
line 3, in
import yaml
File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml__init__.py", line 9, in
from .dumper import *
File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\dumper.py", line 6, in
from .representer import *
File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\representer.py", line
263, in
SafeRepresenter.add_representer(datetime.date, AttributeError: module 'datetime' has no attribute 'date'
How to I get import yaml to work?
You probably have a file called datetime.py in one of the directories shown in the error message (most probably C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/), if you do then you need to rename it to something that will not shadow any other Python module.
The reasoning is that it masks the actual datetime module, since files/directories/modules in the current working directory have precedence over the modules installed in the site-packages directory (in which the built-in and installed modules live). If both locations contain an a module then import a will import the local a module instead of the (probably) intended a module from site-packages.
When yaml\representer.py did import datetime it imported your datetime.py file/module which does not have a date attribute, which is why an AttributeError was raised when it later tried to use datetime.date.

Python: import variable from main file at package module

i try to use a complex structure in flask restfull, my structure is the following:
main.py
-models
-- __init__.py
--modelA.py
-resources
--__init__.py
--resourceA.py
I have a variable in main.py and i need this variable in models.modelA and i need also models.modelA in resources.resourceA. At this point everything is ok. When i start my app, i get the following error:
Traceback (most recent call last):
File "main.py", line 12, in <module>
from resources.resourceA import functionA
File "/var/www/project/resources/resourceA.py", line 11, in <module>
from models.modelA import *
File "/var/www/project/models/modelA.py", line 8, in <module>
from main import mainvariable
File "/var/www/project/main.py", line 12, in <module>
from resources.resourceA import functionA
ImportError: cannot import name functionA
I hope your help
What you basically have here is called a circular import. As the traceback states, your main module is importing functionA from resources.resourceA. And resourceA is importing models.modelA which is in turn trying to import main which again requires resources.resourceA.
Unless a particular order is resolved, python interpreter cannot understand how to resolve the modules. You can however solve this problem in an easier way.
If the models.modelA doesn't require a module level import for main, you can shift the import to the function / class scope where you need its imports.
See here for more on circular imports.

ImportError when using scipy.io

So I am having a bit of trouble using scipy. I have been importing data from a .mat file (matlab variables) and up until today it has worked perfectly. I have no idea what changed or when because I have been developing a text - based data format so that I can avoid depending on Matlab for writing my data. Anyways, I am getting a bizzare import error when I import scipy.io
import scipy.io
myData = scipy.io.loadmat('some_data_file')
When I run this I get the following error:
>>> import scipy.io
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\pyzo2014a\lib\site-packages\scipy\io\__init__.py", line 85, in <module>
from .matlab import loadmat, savemat, whosmat, byteordercodes
File "C:\pyzo2014a\lib\site-packages\scipy\io\matlab\__init__.py", line 13, in <module>
from .mio import loadmat, savemat, whosmat
File "C:\pyzo2014a\lib\site-packages\scipy\io\matlab\mio.py", line 13, in <module>
from .mio4 import MatFile4Reader, MatFile4Writer
File "C:\pyzo2014a\lib\site-packages\scipy\io\matlab\mio4.py", line 11, in <module>
import scipy.sparse
File "C:\pyzo2014a\lib\site-packages\scipy\sparse\__init__.py", line 217, in <module>
from .csgraph import cs_graph_components
File "C:\pyzo2014a\lib\site-packages\scipy\sparse\csgraph\__init__.py", line 148, in <module>
from ._shortest_path import shortest_path, floyd_warshall, dijkstra,\
ImportError: No module named 'scipy.sparse.csgraph._shortest_path'
I thought there might be something wrong with my python distribution so I reinstalled it, however the problem persists, and now the installer says it can't find '_shortest_path.py' when it is installing. When I navigate to .../Libs/site-packages/scipy/sparse/csgraph/ I find that indeed there is no module named _shortest_path. I don't understand how this error came about or how anything in my code would change it. Has anyone else come across this problem?
I am using Pyzo 2014 on Windows 7 x64.
Restore shortest_path.pyd in your virust chest
http://www.blendernation.com/2014/06/28/getting-a-virus-warning-with-blender-2-71-heres-why/

Python design - cross imports

I'm stuck with imports and don't know how to address the issue.
I have 3 modules:
test_project.py
modules/__init__.py
r.py
module.py
module_configuration.py
The list of dependencies:
test_project.py IMPORTS modules/__init__.py
modules/__init__.py IMPORTS r.py
r.py IMPORTS > module_configuration.py
module_configuration.py IMPORTS > modules/__init__.py
So as you can see, we have a circular import here.
modules/__init__.py keeps dict of class definitions (class like R).
R class makes instance of ModuleConfiguration in its constructor
ModuleConfiguration needs dict of classes from modules/__init__.py.
Error message I get:
ERROR: controller.test_project (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: controller.test_project
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/loader.py", line 252, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python2.7/unittest/loader.py", line 230, in
_get_module_from_name__import__(name)
File "/media/103AEB9B3AEB7C5A/Projekty/c/svn/tests/controller/test_project.py",
line 9, in <module>
from c.core.modules import MODULES
File "/media/103AEB9B3AEB7C5A/Projekty/c/svn/tests/../c/core/modules/__init__.py", line 5, in <module>
from R import R
File "/media/103AEB9B3AEB7C5A/Projekty/c/svn/tests/../c/core/modules/R.py", line 6, in <module>
from c.core.module import Module
File "/media/103AEB9B3AEB7C5A/Projekty/c/svn/tests/../c/core/module.py", line 13, in <module>
from c.core.module_configuration import ModuleConfiguration
File "/media/103AEB9B3AEB7C5A/Projekty/c/svn/tests/../c/core/module_configuration.py", line 7, in <module>
from c.core.modules import MODULES
ImportError: cannot import name MODULES
Any ideas on how to solve it?
Instead of making the instances at module load time, implement functions returning the relevant results and keep these functions in their respective modules. Then once the modules are loaded everything is available to all.
There's nothing wrong with importing moduleA from moduleB and moduleB from moduleA.
Do you need module global objects that must be created at module load time? This is usually not needed. Instead try to construct whatever module globals are required at first use once all modules are in place.

Categories