Giter VIP home page Giter VIP logo

Comments (12)

jmmcd avatar jmmcd commented on July 25, 2024

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.

from ponyge2.

benpri avatar benpri commented on July 25, 2024

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

from ponyge2.

jmmcd avatar jmmcd commented on July 25, 2024

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?

from ponyge2.

benpri avatar benpri commented on July 25, 2024

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()

from ponyge2.

benpri avatar benpri commented on July 25, 2024

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

from ponyge2.

jmmcd avatar jmmcd commented on July 25, 2024

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.

from ponyge2.

benpri avatar benpri commented on July 25, 2024

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

from ponyge2.

benpri avatar benpri commented on July 25, 2024

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

from ponyge2.

jmmcd avatar jmmcd commented on July 25, 2024

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!

from ponyge2.

benpri avatar benpri commented on July 25, 2024

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 :)

from ponyge2.

benpri avatar benpri commented on July 25, 2024

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

from ponyge2.

jmmcd avatar jmmcd commented on July 25, 2024

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.

from ponyge2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.