It is strange that the spss.py file can be found but not PyInvoke.pyd, which is in the same folder.
Check what is being used externally by running
import sys
print(sys.executable)
Compare that with what you see when you run that code as a program within Statistics.
But PyInvokeSpss should not be imported directly. The spss module should handle that automatically. You should be able to code like this
import spss
spss.PyInvokeSpss.IsUTF8mode()
What are you using for your IDE? If it is IDLE, which is part of the standard Python distribution, it doesn't have a way to configure the Python executable being used AFAIK.
But you should be able to do something like this to get external mode working. Try this before importing spss in a session
import sys
sys.path.insert(0, r"the location shown for the executable in internal mode/lib/site-packages")
If that works, you can put that in a sitecustomize.py file in the site-packages directory under the sys.executable folder shown in external mode. That way, it would look in the proper site-packages directory even though you started a different python executable.
If you are using IDLE - I don't know whether Anaconda now includes an IDE, I suggest trying out the free version of Wing, which is Wing Personal at
Download Wing Personal v. 8.2.0 - Wing Python IDE or the free version of pycharm. I use the Professional version of Wing, which is not free but is inexpensive. It has many more features, but the free version might be enough for your needs.
------------------------------
Jon Peck
------------------------------
Original Message:
Sent: Sat March 19, 2022 08:58 PM
From: Kristian Canler
Subject: How to install/import SpssClient in external mode for Python IDE
I can successfully import spss in the SPSS window with
begin program Python3.
import spss
end program.
In my IDE, however, when I try to import spss I get the error about not being about to find PyInvokeSpss: "ImportError: DLL load failed while importing PyInvokeSpss: The specified module could not be found." I think this is because Anaconda is running on a Python executable in the Anaconda directory, but I can't find a way of redirecting Anaconda to the Python executable in the SPSS 27 directory, if that's what's needed. I'm not really particular on what solution to use, I'm just trying to successfully import spss in an Anaconda environment (or any IDE really).
Original Message:
Sent: 3/18/2022 5:20:00 PM
From: Jon Peck
Subject: RE: How to install/import SpssClient in external mode for Python IDE
PyInvokeSpss is part of the standard Statistics Python3 installation. It lives in the site-packages\spss directory. It's actually a pyd file, and PACKAGE INSTALL would never find it as it isn't on PyPI. If Python can find the spss module, it should have no trouble finding that module. You would never import it directly. The spss module depends heavily on it.
Can you successfully import the spss module in internal mode and external mode? Try that by itself.
These warnings are strange
C:\PROGRA~1\IBM\SPSS\STATIS~1\27\Python3\Lib\site-packages\pip\_vendor\ipaddress.py:1106: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
raise TypeError("%s and %s are not of the same version" (a, b))
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
They make me wonder whether there is something unusual about your installation, but let's get an answer to my question above.
--
Original Message:
Sent: 3/18/2022 4:48:00 PM
From: Kristian Canler
Subject: RE: How to install/import SpssClient in external mode for Python IDE
Thanks for the direction to spss.Submit.
STATS PACKAGE INSTALL worked great for installing Python packages, but my IDE is still having trouble importing spss with an error message saying that PyInvokeSpss, couldn't be found:
---------------------------------------------------------------------------ImportError Traceback (most recent call last)~\AppData\Local\Temp/ipykernel_6052/2942303256.py in <module>----> 1 import spssauxC:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spssaux\__init__.py in <module> 10 # ************************************************************************/ 11 ---> 12 from .spssaux import * 13 from .spssaux import __version__, _buildvarlist, _smartquote, _isseq, _listifyC:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spssaux\spssaux.py in <module> 14 # ************************************************************************/ 15 ---> 16 import spss 17 import random 18 import copy, sys, os.path, osC:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spss\__init__.py in <module> 254 __SetErrorMessage() 255 --> 256 from .spss import * 257 from .cursors import * 258 from .pivotTable import *C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spss\spss.py in <module> 21 22 import atexit,os,locale,sys,codecs---> 23 from . import PyInvokeSpss 24 from .errMsg import * 25 ImportError: DLL load failed while importing PyInvokeSpss: The specified module could not be found.
When I tried to install PyInvokeSpss with the install modules extension, I got this error in the output window:
C:\PROGRA~1\IBM\SPSS\STATIS~1\27\Python3\Lib\site-packages\pip\_vendor\ipaddress.py:1106: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
raise TypeError("%s and %s are not of the same version" (a, b))
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting PyInvokeSpss
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
Could not fetch URL https:pypi.org/simple/pyinvokespss/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pyinvokespss/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement PyInvokeSpss (from versions: none)
ERROR: No matching distribution found for PyInvokeSpss
------------------------------
Kristian Canler
Original Message:
Sent: Fri March 18, 2022 03:35 PM
From: Jon Peck
Subject: How to install/import SpssClient in external mode for Python IDE
Yes. I suggest that you use the STATS PACKAGE INSTALL extension command (Extensions > Install Python and R modules) to do this, although that might not be necessary. You might need to start Statistics in Admin mode to do it depending on your system configuration and where you installed Statistics. You can install that package via the Extensions > Extension Hub menu. For Python modules, PACKAGE INSTALL will look on PyPI for packages.
On the other part of your first post, when you want to run logistic regression or other SPSS commands from Python, you would use the spss.Submit api. That accepts one or more SPSS commands.
--
Original Message:
Sent: 3/18/2022 3:24:00 PM
From: Kristian Canler
Subject: RE: How to install/import SpssClient in external mode for Python IDE
Hi Jon & Rick; thanks for your answers.
Setting the Python executable to the location the SPSS uses is fine as long as it's possible to install packages like pandas there. Is this doable?
------------------------------
Kristian Canler
Original Message:
Sent: Fri March 18, 2022 03:05 PM
From: Jon Peck
Subject: How to install/import SpssClient in external mode for Python IDE
I use an external Python IDE (Wing) all the time. In order to enable external mode, I need to set the Python executable in Wing preferences, including location to the one that SPSS uses, which would normally be the one that is installed with SPSS, but if you are using the one installed with Anaconda and specified in Edit > Options > Files, it would be different.
You can find the location by running this in a syntax window.
begin program python3.
import sys
print(sys.executable)
end program.
If you are using a different Python from the Statistics installed version, you might need to create a sitecustomize.py file in the lib\site-packages folder under that that appends the Statistics site-packages folder to sys.path. But also look at the pythonenv.bat file or equivalent that is in the Statistics installation folder.
I haven't used Anaconda for a few releases, but that might add some extra quirks.
--
Original Message:
Sent: 3/18/2022 2:52:00 PM
From: Rick Marcantonio
Subject: RE: How to install/import SpssClient in external mode for Python IDE
I have never tried to do this; I've only run modules from with SPSS itself and python (without SPSS). But from what I see here, for example, it looks like for any module to be successfully called, sys.path must be correctly defined:
Python looks for modules in "sys.path"
Python has a simple algorithm for finding a module with a given name, such as a_module
. It looks for a file called a_module.py
in the directories listed in the variable sys.path
.
------------------------------
Rick Marcantonio
Quality Assurance
IBM
Original Message:
Sent: Fri March 18, 2022 02:43 PM
From: Kristian Canler
Subject: How to install/import SpssClient in external mode for Python IDE
At this point I've copy pasted the site-packages folder from C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib (working in Windows 10) to C:\ProgramData\Anaconda3\envs\spss\Lib, but the spss package still won't import.
------------------------------
Kristian Canler
Original Message:
Sent: Fri March 18, 2022 02:24 PM
From: Kristian Canler
Subject: How to install/import SpssClient in external mode for Python IDE
I need to call SPSS Logistic Regression within external mode in a Python IDE. I currently have SPSS 27 pointed to Python 3.8 installed in a virtual environment in Anaconda. The first step in the IDE of course is to import spss, SpssClient, and spssaux probably, but I'm not sure how to get these from SPSS into the virtual environment I'm launching my IDE from. Once the basic SPSS packages are loaded in the IDE and a session in SpssClient is opened, what methods would be used to run a Logistic Regression on data set up in the IDE in, say, pandas?
------------------------------
Kristian Canler
------------------------------
#SPSSStatistics