Where should the sys.path.append ('...') statement go?

Only after standard pythonmodule import? If I deferred it to the main function and until my import of the module to it, it will give an error (which is quite obvious). The Python style guide is no, which shows the correct location for it.

+3
source share
5 answers

He must go in front of the operators importor fromwho he needs (which, as you say, is obvious). For example, a module may begin with:

import sys
import os
import math
try:
  import foo
except ImportError:
  if 'foopath' in sys.path: raise
  sys.path.append('foopath')
  import foo

, ( , sys.path), sys.path, foopath, ; -).

+8

, PEP 8 Python, , sys.path - , ; . , -, sys.path, PYTHONPATH .

+3

script python: sys.path.insert (append) , "" python " python script".

sys.path.insert(0, ...) "" .

+1

, -. , stdlib Python, !

0

, . , , import sys:-) :

def importmod_abs(name):
   sys.path.append() ..

   __import__ ...

   sys.path.pop()

... sys.path . , . , , , sys.path.

0

Source: https://habr.com/ru/post/1734261/


All Articles