Python Forum
Can't import any class from other folder in the same level.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't import any class from other folder in the same level.
#1
Hi! i'm having several issues trying to import a library from other folder that is in the same level of the current file.

[Image: d761a38380.png]

I tried to use "..tests.libs.selenium_driver", "..libs.selenium_driver" and several more, but no one is working.

Im using VS Code. It might be something in the configuration? I'm not using virtual enviroment or something like that.

This is my python configuration.
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput"
            ]
        },
Thank you very much.
Reply
#2
You need __init__.py files.
one a level higher than src directory which contains directory structure and python files in that directory.
For example see: https://docs.python.org/3/tutorial/modules.html (look for packages)
in all directorys below the top __init__.py, include an empty __init__.py
Reply
#3
Hi Larz64+. Maybe I'm not understanding you but, as you can see in the picture, I have __init__.py in all the directorys.
Anyway, I'm using Python 3.6.
Reply
#4
The reference page I referred to is Python 3.6.
Not only do you need a __init__.py file in each source directory,
but also one for the parent directory that links the sub-directories.
Each of the sub-directory __init__.py files (must/should) have similar entries for sub-directories

As per the reference (post 2):
Here's an exmple of the topmost __init__ file:
Output:
sound/ Top-level package __init__.py Initialize the sound package formats/ Subpackage for file format conversions __init__.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... effects/ Subpackage for sound effects __init__.py echo.py surround.py reverse.py ... filters/ Subpackage for filters __init__.py equalizer.py vocoder.py karaoke.py ...
Reply
#5
(Feb-08-2018, 07:12 PM)Larz60+ Wrote: The reference page I referred to is Python 3.6. Not only do you need a __init__.py file in each source directory, but also one for the parent directory that links the sub-directories. Each of the sub-directory __init__.py files (must/should) have similar entries for sub-directories As per the reference (post 2): Here's an exmple of the topmost __init__ file:
Output:
sound/ Top-level package __init__.py Initialize the sound package formats/ Subpackage for file format conversions __init__.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... effects/ Subpackage for sound effects __init__.py echo.py surround.py reverse.py ... filters/ Subpackage for filters __init__.py equalizer.py vocoder.py karaoke.py ...

As I said... I have __init__.py in all the folders.

[Image: cbab648b38.png]
Reply
#6
are you actually running page1.py to start your program or is there a root file somewhere else? Are you using relative imports?

if so then try (in page1.py)
from ..libs import selenium_driver
# use selenium_driver.SeleniumDriver
Recommended Tutorials:
Reply
#7
I recreated your directory structure (I think)
Output:
automation/ __init__.py tests/ __init__.py button_test.py libs/ __init__.py selenium_driver.py page_objects/ __init__.py page1.py
I then simulated the python files as folows:

page1.py:
class page1:
    def __init__(self):
        self.page1_id = 'Page1 stuff'
selenium_driver.py:
class selenium_driver:
    def __init__(self):
        self.sel_driver_name = 'Selenium Driver 1'
__init__.py in sutomation directory:
automation/
    __init__.py
    tests/
        __init__.py
        button_test.py
        libs/
            __init__.py
            selenium_driver.py
        page_objects/
            __init__.py
            page1.py
and __init__.py in tests directory:
tests /
    __init__.py
    button_test.py
    libs /
        __init__.py
        selenium_driver.py
    page_objects /
        __init__.py
        page1.py
button_test:
import libs.selenium_driver as selenium_driver
import page_objects.page1 as page1

class button_test:
    def __init__(self):
        self.btn_tst = "Button Test"
        sd = selenium_driver.selenium_driver()
        print(f'sd.sel_driver_name: {sd.sel_driver_name}')
        p1 = page1.page1()
        print(f'page1_id: {p1.page1_id}')

button_test()
finally form the tests directory:
λ python button_test.py
sd.sel_driver_name: Selenium Driver 1
page1_id: Page1 stuff
And it finds all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cant access root environment folder from django folder using __init__.py Sanjish 0 1,874 Dec-25-2020, 05:56 AM
Last Post: Sanjish
  How do I use shortcut to import class in VS code? cheers100 0 1,571 Nov-28-2020, 08:11 AM
Last Post: cheers100

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020