简体   繁体   中英

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.

Check if this works

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

Your code is not finding pytorch-balanced-sampler module in the locations specified by sys.path which is initialized by PYTHONPATH .

So,

1 - Either include the sampler.py module inside your working directory and then just do from sampler import BalancedBatchSampler

2 - Add the location of this module in either PYTHONPATH or via sys.path if you have a specific location for your modules.

Edit:

Python names should not include '-' (hyphens), but if you want to stick to the same name, here what you need to do:

1 - include __init__.py in pytorch-balanced-batch folder to make it as a package.

2 - Add the following lines to the top of your main.py file:

import importlib

sampler_module = importlib.import_module('pytorch-balanced-batch.sampler', package='pytorch-balanced-batch')

Then you can do:

sampler = sampler_module.BalancedBatchSampler(train_dataset), batch_size=30)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM