Access another python file method

Asked

Viewed 117 times

0

I have a very simple code that clicks on an image on the screen:

from src.imagesearch.functions import clickwithwait


class Home:

    def clickNEWS(self):
        NEWS = r"..\NEWS.png"
        clickwithwait(NEWS)

This file being the name objecthome.py

And I have a code that performs that function:

from src.objects.home.objectshome import Home

home = Home()
home.clickNEWS()

However, when rotating, the following error occurs:

OSError: Failed to read ..\NEWS.png because file is missing, has improper permissions, or is an unsupported or invalid format

He didn’t find the image, and I believe it’s because I’m running from main. How could I do this execution?

Here is a picture of the structure of the code:

Estrutura do código

  • Have you tried assigning the absolute path to the NEWS variable? Relative paths are always relative to the current directory.

  • i tried to do the following: NEWS = r"src Objects home NEWS.png", and it worked, but my idea is to use this function of several other py files, so it would have to have a function for each file, which would not be ideal kkk

  • left so NEWS = r"src Objects home NEWS.png" and changed the main location, and it didn’t work

  • 1

    Use the absolute path to the file. For example: C:\src\objects\home\NEWS.png, and then it will work from anywhere you run this script main.py. Just curious, why do you need to run the main.py in different places?

  • really, with the absolute path worked, I’ll have to rethink a little logic to take the kkkk path, in case I won’t use the main in several places, I will use the home.clickNEWS()

  • I still can’t understand what you’re trying to do. The other modules that will contain home.clickNEWS() will be executed independently or is always from the main.py? If it’s always from main.py, the relative paths will always have directory desste script as starting point, so assign src\objects\home\NEWS.PNG for the variable, it shall function.

  • always independently, in general, the idea is to create a bot for a game, in case, would have several routes, for example, do mission, make pvp, etc., and many of these will go through the same page, but will be in other files py, I do not know if I could pass the idea huahuaa

  • but I managed to solve doing as you said, with the absolute path, but I had to do a function to get the start of the path + the rest of the current directory, anyway, I don’t know if it was well understood huahauhaua

  • Take a look at this answer and her comments: https://stackoverflow.com/a/918178/8841282, I think it will help you.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.