ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.85k stars 462 forks source link

The example_clustering_2.py not working #182

Open KelvinChung2000 opened 1 year ago

KelvinChung2000 commented 1 year ago

I have installed PYGAD with pip and tried to run the example_clustering_2.py and it gives the following error:

  File "/home/GeneticAlgorithmPython/example_clustering_2.py", line 3, in <module>
    import pygad
  File "/home/GeneticAlgorithmPython/pygad.py", line 9, in <module>
    from pygad import utils
ImportError: cannot import name 'utils' from partially initialized module 'pygad' (most likely due to a circular import) (/home/GeneticAlgorithmPython/pygad.py)
ahmedfgad commented 1 year ago

Please consider installing PyGAD using pip if not already installed.

Then run the example_clustering_2.py script outside of the cloned repository directory. It imports the pygad.py module in this directory.

KelvinChung2000 commented 1 year ago

Thanks a lot, I have got it working when using standard pip install. However, when I install it from the git repo using pip install -e . the same error pops up.

ahmedfgad commented 1 year ago

The GitHub repository does not have the complete package. We are working in to have all the modules in the library in another branch before merging it. At the moment, the modules are split into multiple repositories.

Moreover, even running the example_clustering_2.py script in a folder containing the pygad.py module, the same issue will rise. This is because Python searches for the module in the same folder first. If found, then it uses it and will not use the installed library in the site-packages folder.

The way it works is by installing the library using pip install --upgrade pygad and the script will definitely work.

KelvinChung2000 commented 1 year ago

Sure, I will wait for the release than. I need to install it with the git repo since I need to work on a optimization with a complex data structure that cannot be done with an Numpy array. I need to modify the file to do what I need to do.

ahmedfgad commented 1 year ago

You can still modify the code locally after installing the library using pip.

Just locate the installation directory in the site-packages folder. Then edit the code.

KelvinChung2000 commented 1 year ago

This is not recommended, but this will do for now. Thanks a lot. By the way, would you be interested in improving the formatting of the code and including typing to the library? I am happy to contribute if you are interested.

ahmedfgad commented 1 year ago

You are completely welcome to contribute. Just create a pull request of your contributions. Thanks a lot.