Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

python - How to import a module that has a dash "-" in its name without renaming it

Python version : Python 3.8.5 (default, Sep 4 2020, 02:22:02)

With a git submodule, I got some code that I want to use.

The name of the repository is pytorch-balanced-sampler and can be found here https://github.com/galatolofederico/pytorch-balanced-batch.

I essentially want to use the code example in the repo :

train_loader = torch.utils.data.DataLoader(train_dataset, sampler=BalancedBatchSampler(train_dataset), batch_size=30)

First thing that I tried was to :

from pytorch-balanced-sampler.sampler import BalancedBatchSampler

But this doesn't work.

Second thing was :

from importlib import import_module
pytorch_balanced_sampler = import_module('pytorch-balanced-sampler')
from pytorch_balanced_sampler.sampler import BalancedBatchSampler

AND

from importlib import import_module
pytorch_balanced_sampler = import_module('pytorch-balanced-sampler.sampler.py')
from pytorch_balanced_sampler.sampler import BalancedBatchSampler

I got the error message :

    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'pytorch-balanced-sampler'

This is not the first time I have encountered this problem, and I was wondering if there was a work around from this issue without renaming the repository folder. I have tried some other solutions given on stack overflow but none seem to work.

I am open to a __init__ like solution to solve this issue too.

I'd also not want to use since it not best practice:

import sys
sys.path.append('path/to/module')

My work directory is followed as such :

├── README.md
├── environment.yml
├── main.py
├── pytorch-balanced-sampler
│   └── sampler.py
└── src
    ├── gnn.py
    └── rgcn.py

The wanted import takes places in the main.py.

question from:https://stackoverflow.com/questions/65861999/how-to-import-a-module-that-has-a-dash-in-its-name-without-renaming-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check if this works

    pytorch_balanced_sampler = __import__("pytorch-balanced-sampler")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...