How to import a function from parent folder in python?

后端 未结 2 372
离开以前
离开以前 2021-01-21 03:17

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 so

2条回答
  •  失恋的感觉
    2021-01-21 03:40

    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!

提交回复
热议问题