Package: auto-syspath3
切换为中文1. Instructions:Installation (the syntax does not use the latestPythonversion, so it should be compatible with mostPython3versions above)Currently released to PyPI, install directly usingpip install auto_syspath3gitclonehttps://github.com/Alan3344/auto_syspath3.gitcdauto_syspath3
Pythonsetup.pyinstallOrpipinstallgit+https://github.com/Alan3344/auto_syspath3.gitUseNo need to add these two lines at the beginning of each document, because this will attract the annoyingFlake8squiggly line 〰️ promptFlake8(E402)People with OCD say they are less willing to acceptimportsyssys.path.extend(['./','../'])fromutilsimportloginfromconfigimportenvNow you only need to add this line directly in front of the custom package. If you don’t like theFlake8prompt unused, you can add# noqaafter itJust reference it in front of the custom moduleAccording to the characteristics ofPythonimported modules, this__init__.pyfile will be executed automatically, so there is no need to writeadd_path()functionimportosimportauto_syspath3# noqafromutilsimportquitfromconfigimportenv2. RefusedFlake8prompt detection: module level import not at top of file Flake8(E402)Reason: In order to make this file run independently, putimportinto the function3. Reasons for writing this module:I useVSCodeto writePythoncode, butVSCodeimport is correctly prompted, butFlake8prompts an error
Maybe usingPyCharmwill not have this problem, but he needs to be set tosource root, I don't like thisThe original appearance of this function has to be copied every time it is referencedSecondly, this file is mainly placed in thesite-packagesdirectory. After packaging and sending it to others for use, this file will be missingI found a similar package onpypi, but it failed to meet my expectations, so I wrote one myselfThe important thing is that after using this package, the customized package can be used in any terminal, no need to setsource rootdefadd_path(path=__file__,deep=0):"""Add a path to the sys.path if it is not already there."""paths=[]path=os.path.abspath(path)dirname=os.path.dirname(path)foriinrange(deep):dirname=os.path.dirname(dirname)d(f'dirname:{dirname}')forpinos.walk(dirname):p0=p[0].replace(dirname,'')ifp0.startswith('/.')or'__pycache__'inp0:continueelifnotp[0]insys.path:paths.append(p[0])sys.path.insert(0,p[0])d(f'add path:{p[0]}')returnpathsScreenshot during useSolve the error when the subpackagepyfile imports the top-level module method:ModuleNotFoundError: No module named 'package name'Normally, ourpwdpaths are all in the top-level directory. At this time, we can directly run themain.pyfile. Of course, there is no problem if I runpython test_project/main.pydirectly.If your project structure is like this, themain.pyfile is the entry file,child_packageis the custom package, andchild_package/child_packageis the subpackage of the custom package, they all have specificpyfileRun directly in the entry file in themain.pyfile like thisThis is how it works in thechild_package/call_user.pyfile. The first time the error is reported because the lineimport auto_syspath3 # noqa F401has been commented outThis is how it works in thechild_package/child_package/call_user.pyfile. The first time the error is reported because the lineimport auto_syspath3 # noqa F401is commented out