0

So, I have a project structure like following.

Project
   |
   |--- Module1
          |----- __init__.py 
          |----- A.py
          |----- 1B.py
                   |---- update()
   |--- Module2
          |----- __init__.py 
          |----- C.py
          |----- D.py
   |--- Tests
          |----- Test1.py

I am very new to modular structure in python. So all the examples I have seen, have init.py as an empty file. I suppose it needs to be there to make Module1 as module.

I am trying to import A.py in Test1.py

1B.py has a class B which has a method update(self,x). The 1B.py filename is intentional because I want to know how to import file starting with a number and file starting with letter.

How can import 1B.py which is from Module1 and then use update method

import sys
sys.path.insert(0,"../")

class Test1(unittest.TestCase):
    def test_update(self):
        target = __import__("Module1.1B",fromlist=[])
        print(target.Solution)
  • I tried from Module1.1B ... which throws error hence I used import where I get <module 'Module1' from '..\\Module1\\__init__.py'> when I print target

  • Does this mean that I was successful to access module Module1?

  • Also to make this work, I added sys.path.insert to the path so that it can refer to the parent folder.

  • What I am trying to do is that I will have a dedicated test function for every class or function in my test file.

I am not sure what I am doing wrong here.

Anurag P
  • 337
  • 3
  • 11

2 Answers2

1

It is recommended to use import_lib.import_module rather than __import__. As specified by the documentation, you can specify the name argument in absolute or relative terms.

Also, you shouldn't need to use sys.path.insert: just call your test script using python -m pkg.script_name (omit the .py).

0

As long as your package is in the same directory where you will be calling your main function you can import that class, create an instance of it, then call the method. Also your directories which contain init files are called packages, your individual files such as A and 1B are known as modules.

   from Module1 import 1B.B
   bclass = B()
   bclass.update()

Additionally your init.py files should import the modules you want as well

   import Module1.A
   import Module1.1B
jgonz1
  • 40
  • 7
  • Ohh, I see. I messed up with the terminology. Also, import won't work in the case of the file starting with a number. according to [this](https://stackoverflow.com/questions/9090079/in-python-how-to-import-filename-starts-with-a-number) I have to use __import__. Further I shoulf use importlib than using __import__, according to [this](https://stackoverflow.com/a/38813946/3786108) – Anurag P Aug 14 '19 at 00:10
  • So, I found the answer to this. I had to insert `sys.path.insert(0,"..")` and use the `importlib` which worked for me. But according to [this](https://stackoverflow.com/a/6466139/3786108), using sys.path is a dirty way to do this. I will try to do the mentioned `setuptools` method. Although I don't know why there is so much of a hassle to just get a package from parent directory. where in other languages it is kind of easy. – Anurag P Aug 14 '19 at 00:16