NEW! PaintShop Pro 2019

Importing a Python Module File which is Self-Programmed

Moderator: Kathy_9

Importing a Python Module File which is Self-Programmed

Postby wpslo » Tue Oct 09, 2018 11:39 pm

I put my module file in the same directory as the calling script .
The "import" statement fails with the message "ImportError: No module named moduleLocator" .

The module is definitely in the folder and it works on an "import" command run from a script with a ".py" extension
located in the same folder as the failing "PspScript".

Does anyone have any ideas?
wpslo
 
Posts: 11
Joined: Sat Jun 02, 2018 9:48 pm
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 32 Bit
motherboard: asus 1025ce
processor: intel Atom
ram: 4gb
Hard_Drive_Capacity: 2 TB

Re: Importing a Python Module File which is Self-Programmed

Postby LeviFiction » Wed Oct 10, 2018 4:39 am

Put it into the Lib directory of PSP.

You can find the list of folders PSP uses to check for libraries by importing the sys library and printing out sys.path.

Code: Select all
from PSPApp import *
import sys

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'PaintShop Pro',
        'Host Version': u'9'
        }
   
def Do(Environment):
    print sys.path


If you print out sys.path you'll see that the Scripts-Trusted and Scripts-Restricted folders are not listed as possible locations for library files. Though any of the folder locations listed will work for placing your library.

Libraries are usually installed in the site-packages folder. Here are the two folders for 32-bit and 64-bit versions of PSP 2019

C:\Program Files (x86)\Corel\Corel PaintShop Pro 2019\Python Libraries\Lib\site-packages
C:\Program Files\Corel\Corel PaintShop Pro 2019 (64-bit)\Python Libraries\Lib\site-packages

Just as a small aside, it should be noted that you can install any pure python library in these folders as well to get more functionality. However, if a library is a compiled library (PIL, Pillow, NumPy, SciPy, etc) that cannot be run by PSP.

It's also possible to add the Scripts-Trusted and Scripts-Restricted folders to as available paths. Just need to append the path to sys.path before trying to import your library.

Code: Select all
from PSPApp import *
import sys
sys.path.append("C:\\Users\\UserName\\Documents\\Corel PaintShop Pro\\2019\\Scripts-Trusted")
import YourModuleName

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'PaintShop Pro',
        'Host Version': u'9'
        }
   
def Do(Environment):
    #do what you're going to do
LeviFiction
Advisor
 
Posts: 5212
Joined: Thu Oct 02, 2008 1:07 pm
Location: USA
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PSP: 8, 9, X, XI, X2, X8,X9, 2018, 2019

Re: Importing a Python Module File which is Self-Programmed

Postby wpslo » Thu Oct 11, 2018 10:35 am

Hi LeviFiction,

Thank you for this information which has solved the immediate problem, i.e. a module will not import .
I will be using this technique to tidy up my code .

What I was actually trying to do was to get the name of the file running the script which, judging by a
trawl round the forums, seems to be problematic .
I now realise that using a module will only go down to folder level in any case and show the location of the
script file but not the name of the script file calling the module [ how could it ? ] .

If I may pose a supplementary question - do you happen to know how to do this within the PSP environment ?

Once again your help is much appreciated,
wpslo
wpslo
 
Posts: 11
Joined: Sat Jun 02, 2018 9:48 pm
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 32 Bit
motherboard: asus 1025ce
processor: intel Atom
ram: 4gb
Hard_Drive_Capacity: 2 TB

Re: Importing a Python Module File which is Self-Programmed

Postby LeviFiction » Thu Oct 11, 2018 5:27 pm

I would counter with...why?

I mean, the easiest option, especially if you're just programming the module yourself, is to hardcode the filename into the script and pass it to the module.

I honestly don't even know if it's possible to get the currently running script. None of the standard methods for doing this in Python will work with PSP. The big problem you're going to run into with PSP is that it's not like the normal python interpreter. It's an embedded interpreter and PSP actually reads in your entire file and sends it as a string to the interpreter. It's not something we normally focus on, but if you look closely at the errors that PSP throw

Code: Select all
Traceback (most recent call last):
  File "<string>", line 16, in Do
AttributeError: 'module' object has no attribute '__file_'


If you notice it says 'File "<string>", line 16, in Do'

PSP isn't sending the filename to be interpreted. So the stack traceback can only report it as <string>.

I haven't been able to find a way to do this at all short of hardcoding the script name into the script and passing it as a parameter.

You can find the file name of any module you import. That's easy. Just call modulename.__file__ but I don't think we have a reliable way of determining what script is currently being run inside PSP.
LeviFiction
Advisor
 
Posts: 5212
Joined: Thu Oct 02, 2008 1:07 pm
Location: USA
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PSP: 8, 9, X, XI, X2, X8,X9, 2018, 2019


Return to PSP Scripting

Who is online

Users browsing this forum: No registered users and 2 guests