简体   繁体   中英

How to organize a python project

I am new to python and trying to figure out how to do things the "right way" and encountered the following problem:

My project has a structure that looks a little this:

├─packageA
│     functions.py
│     __init__.py
│
└─tests
      some_tests.py

packageA/__init__.py is empty.

packageA/functions.py looks like this:

def some_function(x):
    return x*x

And finally, tests/some_tests.py :

import packageA.functions

if __name__ == '__main__':
    print(packageA.functions.some_function(2))

If I run test.py using pycharm it works fine. However, when I open up a console and start it by running python.exe ./tests/some_tests.py I get

    Traceback (most recent call last):
  File ".\tests\some_tests.py", line 1, in <module>
    import packageA.functions
ModuleNotFoundError: No module named 'packageA'

While writing this, I figured out that pycharm adds source folders to PYTHONPATH - when I turn this off I get the same error. Is the folder structure above a sensible way to structure a python project? If not, how should it be organized instead?

There are several options. However, the accepted answer in a similar SO question isn't ideal.

The below answer addresses your issue by explicitly adding the parent directory path at the start of your sys.path list.

import os, sys, inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir) 

import packageA.functions

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