How to Resolve ImportError

Asked 2 years ago, Updated 2 years ago, 234 views

To use the ICA that was previously class-defined,

from ica import ICA

When you run the

ImportError: cannot import name 'ICA' from 'ica'

The error occurred.
I don't know how to resolve this.

python python3

2022-09-30 21:59

1 Answers

If "class-defined ICA" is equivalent to ica.py in this article Sound Source Separation by Independent Component Analysis

:
  • The simplest thing is to put the caller script file you import and ica.py in the same folder and run the caller script file using that folder as the current folder.
  • Equally simple is to set or add the folder where the ica.py file resides to the environment variable PYTHONPATH.
  • You can also place the caller script file to import and ica.py in the same folder, and move the current folder during import as follows:
importos
curdir=os.getcwd()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
from ica import ICA
os.chdir (curdir)
  • Similarly, you can add a folder where the ica.py file resides to the beginning of the module path variable sys.path.
import sys
sys.path [0:0] = 'path/to/ica.py folder'
from ica import ICA
    You can also add
  • ica.py to the folder where the ica folder is located in place of the folder where ica.py is located in one of the above ways:
    • icaCreate folder
    • Form as a package in one of the following ways:
        Create a _init__.py file in the
      • ica folder and leave the contents of the ica.py file intact
      • icaCopy the ica.py file into the folder, create an additional _init__.py file, and write the following:
  • icaCreate folder
  • Form as a package in one of the following ways:
      Create a _init__.py file in the
    • ica folder and leave the contents of the ica.py file intact
    • icaCopy the ica.py file into the folder, create an additional _init__.py file, and write the following:
    Create a _init__.py file in the
  • ica folder and leave the contents of the ica.py file intact
  • icaCopy the ica.py file into the folder, create an additional _init__.py file, and write the following:
from.ica import ICA


2022-09-30 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.