7

I need to perform an import of a function on my python project.

I know there're dozens of similar questions on SO, however, unfortunately, I couldn't find the right solution for me because the answers are either too question specific, or way too general, or they're just ugly hacks (like operating with absolute paths).

Here's how my folder structure looks like:

PythonClient:.
│   .gitignore
│   des.py
│   des_test.py
│   des_var2.py
│   gui.py
│   index.py
│   __init__.py
│
├───diffie_hellman
│   │   diffie_hellman.py
│   │   diffie_hellman_test.py
│   │   __init__.py
│   │
│   └───__pycache__
│           diffie_hellman.cpython-35.pyc
│
├───hashes
│   │   collision.py
│   │   hash_function.py
│   │   __init__.py
│   │
│   └───__pycache__
│           hash_function.cpython-35.pyc
│           __init__.cpython-35.pyc
│
└───__pycache__
        des.cpython-35.pyc
        des_var2.cpython-35.pyc

I need to import the ./hashes/hash_function.py from ./diffie_hellman/diffie_hellman.py.

The ./hashes/hash_function.py file contains the only function named hash_function.

I've tried quite a number of ways to perform the import but just couldn't do it. I always get either

SystemError: Parent module '' not loaded, cannot perform relative import

when I use . in my import statement (i.e. from .hashes.hash_function)

or I get this:

ImportError: No module named 'hashes'

Every __init__.py file is empty.

Here's the list of my attempts:

  1. from hashes import hash_function

  2. from hashes.hash_function import hash_function

  3. from .hashes.hash_function import hash_function

  4. from ..hashes.hash_function import hash_function

  5. import hashes

  6. import hash_function

  7. from .. import hash_function

  8. from . import hash_function

  9. from PythonClient.hashes.hash_function import hash_function


Could you please help me to resolve my problem and to understand how to work with such imports?


PS: The solution couldn't be found here stackoverflow.com/questions/14132789/

Community
  • 1
  • 1
Denis Yakovenko
  • 3,241
  • 6
  • 48
  • 82
  • 1
    I suggest you read http://stackoverflow.com/questions/14132789/python-relative-imports-for-the-billionth-time#answer-14132912 first. 99% of python import question can be solved if you read it carefully enough. – laike9m Apr 14 '16 at 11:54

2 Answers2

9

I know you have already accepted an answer, but if you wanted a less "permanent" solution (that is to say, if you didn't want to install your code), another option would be to simply add the parent of your PythonClient directory to your path. This can be done permanently (which varies depending upon operating system) or temporarily in code:

import os
import sys

p = os.path.abspath('../..')
if p not in sys.path:
    sys.path.append(p)

from PythonClient.hashes.hash_function import hash_function

Cheers!

CaffeineFueled
  • 561
  • 2
  • 9
3

The fact that you have an __init__.py tells me that PythonClient is itself a library. Do from PythonClient.hashes.hash_function import hash_function. I always like fully qualified paths.

You need to install your library too before you can import from it. That requires a setup.py file in your home directory. After that, you should pip install your library for testing, e.g., `pip install -e .

Tommy
  • 12,588
  • 14
  • 59
  • 110
  • 1
    my root folder is called `PythonClient`. But when I do `from PythonClient.hashes.hash_function import hash_function` I get `ImportError: No module named 'PythonClient'` – Denis Yakovenko Apr 14 '16 at 11:56
  • It gives me `Directory '.' is not installable. File 'setup.py' not found.`. If I have to create the `setup.py` file, what should it be like and where should it be located? – Denis Yakovenko Apr 14 '16 at 12:04
  • Thank you, creating `setup.py` resolved the issue! Could you update your answer please, so that I could mark it as answered and so that other people could see it? – Denis Yakovenko Apr 14 '16 at 12:28