0

Normally I have to do in every file something like this :

 import os, sys
 sys.path.extend(['lib', '../lib'])

Is there a easy way. So I dont have to do this in every file ?

So that I can simply do :

 from abc import *

cant understand how this work... https://docs.python.org/2/library/pkgutil.html#module-pkgutil

but probably some __init__.py trick may work !

sten
  • 7,028
  • 9
  • 41
  • 63
  • Is the code in lib code you have written or third party packages? – progmatico Apr 19 '20 at 15:28
  • i have written the lib code – sten Apr 19 '20 at 15:57
  • Do you know about organizing code with regular import packages (those using `__init__.py` inside folders)? – progmatico Apr 19 '20 at 16:03
  • Something similar to [this](https://stackoverflow.com/questions/47499730/a-clean-way-to-organize-and-load-files-with-subclasses/47501913#47501913) – progmatico Apr 19 '20 at 16:07
  • You can just place an `__init__.py` inside your `libs` or utils, whatever, and start organizing your reusable code there. Then you import things from your runnable modules in the outer folder. – progmatico Apr 19 '20 at 16:10
  • i know init.py , but dont know how syspath behaves for libs outside of the current dir hierarchy – sten Apr 19 '20 at 16:10
  • Ah ok. Python just searches all the paths listed there. Only be careful with search order. Current dir (where the script was called to execute) is searched first and can override modules in other locations. – progmatico Apr 19 '20 at 16:16
  • so what do i put in init.py – sten Apr 19 '20 at 16:18
  • You can leave it empty. Just put `__init__.py` files in every folder and sub folder of your package so that internal modules can reach each other with relative imports. Remember that execution entry point should stay out of the libs package, in another module. Import your package or names from there. You can also put code that runs on import inside __init__.py, for initialization or to further managed names available to import. But in many cases the `__init__.py` can remain with no problems. – progmatico Apr 20 '20 at 19:24
  • I am referring to access code that is always inside your libs folder. Were you asking about extending to scattered folders in arbitrary places outside your project structure? That is a different problem I was not commenting about. – progmatico Apr 20 '20 at 19:29
  • yes.. outside of the current dir – sten Apr 20 '20 at 21:17
  • 1
    Do as you were doing (or use PYTHONPATH ) to add absolute paths to your external lib folders. Maybe gather the paths in some central file and configure your sys.path from a module you can import (run) from all modules with a single statement... You may get trouble if you use relative paths, depending on running module relative locations to each other. – progmatico Apr 21 '20 at 17:02

1 Answers1

2

You can modify the environment variable PYTHONPATH. See the documentation.

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.
jkr
  • 17,119
  • 2
  • 42
  • 68
  • thanks .. subquestion ...that probably wont work well... if I have many projects with their own libs dir and a shared lib dir, right ? too many dirs in the path and if I relocate the prj have to update it – sten Apr 19 '20 at 16:01