Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifed moudle not found: fitness.xxx #165

Closed
benpri opened this issue Feb 14, 2023 · 12 comments
Closed

Specifed moudle not found: fitness.xxx #165

benpri opened this issue Feb 14, 2023 · 12 comments

Comments

@benpri
Copy link

benpri commented Feb 14, 2023

My fitness class is in src/fitness and has the same name as the file.
My grammar is in grammars.
And I'm running:
python3 ponyge.py --fitness_function xxx --grammar_file xxx.pybnf

Any help would be appreciated

@jmmcd
Copy link
Collaborator

jmmcd commented Feb 14, 2023

Can you run:

python3 ponyge.py --fitness_function string_match

Notice we just use string_match, not string_match.py.

Also, to run this we have to be in the src directory.

@benpri
Copy link
Author

benpri commented Feb 14, 2023

Yes I can run that.
Sorry I had tried with and without .py and still getting the error

@jmmcd
Copy link
Collaborator

jmmcd commented Feb 14, 2023

I also tried copying the file string_match.py to string_match2.py (and of course changing the name inside the file also). And now python3 ponyge.py --fitness_function string_match2 also works for me. So the mechanism is working as expected. I have to suspect something in the file xxx.py - either location, name, or name inside the file is not as expected. Would you like to post it?

@benpri
Copy link
Author

benpri commented Feb 14, 2023

Yeah I just tried to copy the string match and changed the name and it works fine. I'll include my fitness function.

from fitness.base_ff_classes.base_ff import base_ff
import time
from DOOMGame import mybehaviours

class doom_fitness(base_ff):
    def __init__(self):
        super().__init__()
    
    def evaluate(self, ind, **kwargs):
        p = ind.phenotype
        print("\n" + p)
        fitness = 0

        for trial in range(50):
            self.test_spec = getSpec()
            # generatelist should be the hand coded one
            d = {'test_spec': self.test_spec}

            try:
                t0 = time.time()
                exec(p, d)
                # algo p, parameters in d
                t1 = time.time()
                
                guess = d['return']
                print (guess)
                fitness += len(p)
                # adding length of p to represent complexity of algorithm
                
                #run game passing guess as the created tree
                # I know it's not finished here but that shouldn't affect the module not found I don't think
            except:
                fitness = self.default_fitness
                break
        return fitness

def getSpec():
    return mybehaviours.npcSpec()

@benpri
Copy link
Author

benpri commented Feb 14, 2023

I removed my usage of function from the mybehaviours file and i could run my program. Is there any way I can import functions?

@jmmcd
Copy link
Collaborator

jmmcd commented Feb 14, 2023

Yes. If the module you want to import is installed system-wide it should work directly. If it's in the source tree, then at least it works if it's in the current directory. So, if I put this in string_match.py, and I put test.py in src, then it works:

from test import f

But if I put test.py in fitness, it doesn't work.

@benpri
Copy link
Author

benpri commented Feb 14, 2023

The function I want to access is in a file in a directory which is inside src.
As soon as I add the function to the fitness file I get module not found without the import it runs without that error

@benpri
Copy link
Author

benpri commented Feb 14, 2023

The code will run if I place a test file in src.
The issue seems to be when the file is in a directory which is a child of src

@jmmcd
Copy link
Collaborator

jmmcd commented Feb 15, 2023

This works:

python3 ponyge.py --fitness_function string_match2

Where string_match2.py is in fitness, and it runs:

from fitness.testdir.test import f

and fitness/testdir includes the file test.py.

I'm not very clever at Python sub-modules etc., but yes, it seems to be possible!

@benpri
Copy link
Author

benpri commented Feb 15, 2023

I've been playing around with it and it seems to working now.
I was trying to import from a fairly large package and the error seemed to be coming from having an init.py in the subpackage. Thank you for your help :)

@benpri
Copy link
Author

benpri commented Feb 15, 2023

The issue seems to persist if I'm importing in the file I'm importing. It seems to be a mess of python submodules

@jmmcd
Copy link
Collaborator

jmmcd commented Feb 15, 2023

My example above seems to show hot to import from within the import. So I believe this can be closed but please reopen if there is something specific not working.

@jmmcd jmmcd closed this as completed Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants