Page 1 of 1

import from same folder

Posted: Tue Feb 04, 2014 3:53 am
by edcolmar
I have some general configuration variables that I want to keep outside of my main plugin.

config.py exists inside my plugin folder.

I also put a __init__.py in there for good measure.

I try:

Code: Select all

from config import *


but the variables don't make it into my running script.

Any idea as to why this is?

Posted: Tue Feb 04, 2014 4:25 am
by satoon101
Source.Python, unlike EventScripts, does not add the loaded plugin's package to sys.path. Therefor, you must either be explicit and use the full import path:

Code: Select all

from <package>.config import *

or simply use a period . to denote going up one level from the current module:

Code: Select all

from .config import *

Also, with Python3, there is no need to add __init__.py files unless you specifically need them for some functionality.

Satoon

Posted: Tue Feb 04, 2014 4:44 am
by satoon101
Sorry for the double post, but I also need to mention that config is a Source.Python package. This sort of thing is one reason why we decided to not add the plugin to sys.path. It could become very difficult to discern between which config you are wanting to import from. Requiring full import paths is the best way to handle this situation. As a note, the following are the only paths we add to sys.path (all paths relative to ../addons/source-python):

  • ../Python3
  • ../packages/source-python
  • ../Python3/plat-win - if Windows binary
  • ../Python3/plat-linux - if Linux binary
  • ../Python3/lib-dynload - if Linux binary
  • ../packages/site-packages
  • ../packages/custom
  • ../plugins


Also of note is that these are checked in this exact order when importing.
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/src/core/core/sp_python.cpp#L74

Satoon