import from same folder

Please post any questions about developing your plugin here. Please use the search function before posting!
edcolmar
Junior Member
Posts: 21
Joined: Tue Oct 15, 2013 10:59 pm

import from same folder

Postby edcolmar » Tue Feb 04, 2014 3:53 am

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?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Feb 04, 2014 4:25 am

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
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Feb 04, 2014 4:44 am

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

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 35 guests