Importing script from addons\source-python\<script> folder

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Importing script from addons\source-python\<script> folder

Postby Mahi » Sun Sep 02, 2012 2:01 pm

I've been working on this problem for a while now, and decided to come ask you guys.
What I got now, are two files as following:

Code: Select all

..\addons\source-python\test\test.py
..\addons\source-python\test\classes.py

Now if I want to import classes.py file into my test.py file, this is what I'd do:

Syntax: Select all

from classes import *
However, I get an error, telling me that classes is not a module. This seems to be caused by the fact that SP default import path is not my addon's path, so I have to add the path manually:

Syntax: Select all

#Add import path: ..\addons\source-python\test\
from paths import ADDON_PATH
from sys import path
path.append(ADDON_PATH + "\\test\\")

#Import test classes
from classes import *
AND IT WORKS, HURRAYYY!!!

...Until I reload the script: sp_reload test

Code: Select all

[SP] Loading "Test"...

[SP] caught an exception:
Traceback (most recent call last):
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "..\addons\source-python\_libs\addons\manager.py", line 35, in __missing__
    instance = _LoadedAddon(addon_name)
  File "..\addons\source-python\_libs\adodns\manager.py", line 201, in __init__
    addon = __import__(addon_name + '.' + addon_name)

ImportError: No module named 'Test.Test'; Test is not a package

[SP] Addon "Test" was unable to be loaded.

Help? :) If I add a new import directory, the script can be loaded once, and works just fine, but if I wanna reload it, everything gets messed up. What am I doing wrong, or am I even doing anything wrong?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Sep 02, 2012 2:07 pm

Well, you say you are loading "test", but the code block says "Test". Which one is it, so that we can help further? Just as a note, "test" is a built-in module in Python3, so you will be unable to use a script at ../addons/source-python/test/test.py. We "could" eventually remove this module, but that is yet to be seen.

Also, instead of appending to sys.path, you should use:[python]from myaddon.classes import *[/python]Satoon
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Sun Sep 02, 2012 2:22 pm

satoon101 wrote:Well, you say you are loading "test", but the code block says "Test". Which one is it, so that we can help further? Just as a note, "test" is a built-in module in Python3, so you will be unable to use a script at ../addons/source-python/test/test.py. We "could" eventually remove this module, but that is yet to be seen.

Also, instead of appending to sys.path, you should use:[python]from myaddon.classes import *[/python]Satoon
Yeah, sorry about that, it says Test everywhere for me. I wrote it by hand (didn't c/p) so I accidentally typed "test".
The problem wasn't with test being a built-in module, cause it didn't work with "RPG" either, so name can't be the problem.

But I tested importing myaddon.classes, and it seems to work. Thank you :) It was easier than I thought

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 9 guests