2

I know name of the module should not have a dash.

Here is my repository structure

my-repo-name/
     src/ 
     tests/
        __init__.py
        tests.py
     fab/
        __init__.py
        fabfile.py

     README.rst
     __init__.py

In my tests, I need to import fabfile.py to run a test. But because the name has a dash, I can't do from my-module-name.fab.fabfile import X,Y,Z or relative import because it's a non-package.

Any recommendation how to do this without hacking with __import__(...) or adding it to the sys path? Should I just add another directory?

user1012451
  • 3,343
  • 7
  • 29
  • 33
  • 3
    I think changing the name of your repo is probably your best bet as you can't do anything with it in python with dashes in the name. Second best bet would be to create a symbolic link to that directory under a different (importable) name. – mgilson Jul 25 '12 at 17:47
  • Thanks.. But I've seen people using repository with dash in the name. A symlink... I thought about it, but `hg` makes hard link. Maybe the question is more about how to structure my repo so that it doesn't actually import from `my-repo-name` ? Thanks. – user1012451 Jul 25 '12 at 17:48
  • 1
    a repository, sure. Most of the time, a repo isn't the module/package, it's a container for the module/package directory... and THAT can't have a dash in the name. – Colin Dunklau Jul 25 '12 at 17:49
  • 1
    Just create one more level of directory. `my-repo-name -> myproject -> __init__.py, src, tests, ...` – mgilson Jul 25 '12 at 17:49
  • possible duplicate of [How to import python module when module name has a '-' dash or hyphen in it?](http://stackoverflow.com/questions/8350853/how-to-import-python-module-when-module-name-has-a-dash-or-hyphen-in-it) – inspectorG4dget Jul 25 '12 at 17:52
  • Why would you use the name of the repository as package name? I'd simply avoid that. People is free to pull your repository and save it in the directory `user1012451-source-repository` and then all the imports would be broken. I believe the `src` directory should be "self-contained", not requiring access to things outside it. Also, keep in mind that to setup the directory layout on target machines you can use a `setup.py` file. – Bakuriu Mar 07 '13 at 19:02
  • Does this answer your question? [Is it ok to use dashes in Python files when trying to import them?](https://stackoverflow.com/questions/761519/is-it-ok-to-use-dashes-in-python-files-when-trying-to-import-them) – Georgy Aug 12 '20 at 14:38

1 Answers1

8

Use importlib.import module:

import importlib
fabfile = importlib.import_module('my-repo-name.fab.fabfile', None)
X = fabfile.X

But you should really just change the name of the repository. To transition, you can create a temporary symlink with

$ mv my-repo-name my_repo_name
$ ln -s my_repo_name my-repo-name
phihag
  • 278,196
  • 72
  • 453
  • 469
  • Yes. That's certainly is one solution. THanks. But I guess the question is more like how to restructure my repo so that I don't need to rename it and still able to import. I've seen people using repo name like that too. – user1012451 Jul 25 '12 at 17:49
  • 1
    I think you wanted `mv` not `my` – mgilson Jul 25 '12 at 17:52
  • Thanks guys! I went with adding an extra directory. But this solution is also promising :) – user1012451 Jul 25 '12 at 21:03