python importlib from subdirectory, module not found - Stack Overflow

I'v been struggling with importing a sub module. In main.py two modules are imported located under

I'v been struggling with importing a sub module. In main.py two modules are imported located under 220_04_070. I'm using importlib since the directory name only consist of numbers. common.py just contains a number of variables.

The problem I get is:

 .../220_04_070/unicorn.py", line 1, in <module>
 from  common import *
 ModuleNotFoundError: No module named 'common'.

I can get around by adding a dot in front of common in unicorn.py. But since unicorn is used standalone at other parts I prefer not to do that.

from mon import *

Both main.py and unicorn.py are using common.py. The structure is as below:

|-- main.py
|
|-- 220_04_070
     |-- common.py
     |-- unicorn.py

main.py

import importlib
uc = importlib.import_module('220_04_070.unicorn')
cm = importlib.import_module('220_04_070mon')

mycorn = uc.unicorn()

unicorn.py

from common import *
class unicorn():
  def __init__(self):
    None

All is run with python3.12.3 (Ubuntu 24.04).

So, my question is how I should handle this?

I'v been struggling with importing a sub module. In main.py two modules are imported located under 220_04_070. I'm using importlib since the directory name only consist of numbers. common.py just contains a number of variables.

The problem I get is:

 .../220_04_070/unicorn.py", line 1, in <module>
 from  common import *
 ModuleNotFoundError: No module named 'common'.

I can get around by adding a dot in front of common in unicorn.py. But since unicorn is used standalone at other parts I prefer not to do that.

from mon import *

Both main.py and unicorn.py are using common.py. The structure is as below:

|-- main.py
|
|-- 220_04_070
     |-- common.py
     |-- unicorn.py

main.py

import importlib
uc = importlib.import_module('220_04_070.unicorn')
cm = importlib.import_module('220_04_070mon')

mycorn = uc.unicorn()

unicorn.py

from common import *
class unicorn():
  def __init__(self):
    None

All is run with python3.12.3 (Ubuntu 24.04).

So, my question is how I should handle this?

Share Improve this question asked Feb 11 at 12:37 hzcodechzcodec 571 gold badge1 silver badge4 bronze badges 4
  • 1 Show us the exact command you use to run the code. – John Gordon Commented Feb 11 at 13:06
  • python3 main.py – hzcodec Commented Feb 11 at 14:36
  • Can you give us the full package structure? Are you using __init__.py or __main__.py? Are you creating a package that will later be installed with pip? – Markus Hirsimäki Commented Feb 11 at 17:31
  • Under /220_04_070 I have init.py file. I don't however have any main.py file. No, I'm not creating a package to be installed with pip. Just import in various local projects. – hzcodec Commented Feb 11 at 19:49
Add a comment  | 

1 Answer 1

Reset to default 0

I manage to get it running. I have a solution, however maybe there is another way.

I change my common.py file to:

import importlib

#check if it's run from above or from local main()
if __name__ == '__main__':
    common = importlib.import_module('common')
else:
    common = importlib.import_module('220_04_070mon')


class unicorn:
    def __init__(self):
        None

    def get_data(self):
        print("var: ", common.VAR1)


def main():
    mycorn = unicorn()

if __name__ == '__main__':
    main()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745211595a4616873.html

相关推荐

  • python importlib from subdirectory, module not found - Stack Overflow

    I'v been struggling with importing a sub module. In main.py two modules are imported located under

    14小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信