Installing python with python win32 extensions on a network drive - python

I need to keep a large number of Windows XP machines running the same version of python, with an assortment of modules, one of which is python-win32. I thought about installing python on a network drive that is mounted by all the client machines, and just adjust the path on the clients. Python starts up fine from the network, but when importing win32com I get a pop-up error saying:
The procedure entry point ?PyWinObject_AsHANDLE##YAHPAU_object##PAPAXH#Z could not be located in the dynamic link library pywintypes24.dll
after dismissing the message dialog I get in the console:
ImportError: DLL load failed: The specified procedure could not be found.
I searched the python directory for the pywintypes24.dll and it is present in "Lib\site-packages\pywin32_system32" .
What am I missing and is there another way in which I can install Python + Python-Win32 + additional module once and have them running on many machines? I don't have access to the Microsoft systems management tools, so I need to be a bit more low-tech than that.

On every machine you have to basically run following pywin32_postinstall.py -install once. Assuming your python installation on the network is N:\Python26, run following command on every client:
N:\Python26\python.exe N:\Python26\Scripts\pywin32_postinstall.py -install
Another important thing is Good Luck!. The reason is that you might need to do this as admin. In my case such setup worked for all but one computer. I still did not figure out why.

Python (or precisely, the OS) searches the DLLs using os.environ["PATH"] and not by searching sys.path.
So you could start Python using a simple .cmd file instead which adds \server\share\python26 to the path (given the installer (or you) copied the DLLs from \server\share\python26\lib\site-packages\pywin32-system32 to \server\share\python26).
Or, you can add the following code to your scripts before they try to import win32api etc:
# Add Python installation directory to the path,
# because on Windows 7 the pywin32 installer fails to copy
# the required DLLs to the %WINDIR%\System32 directory and
# copies them to the Python installation directory instead.
# Fortunately, in Python it is possible to modify the PATH
# before loading the DLLs.
os.environ["PATH"] = sys.prefix + ";" + os.environ.get("PATH")
import win32gui
import win32con

You could use batch files running at boot to
Mount the network share (net use \\server\share)
Copy the Python and packages installers from the network share to a local folder
Check version of the msi installer against the installed version
If different, uninstall Python and all version dependent packages
Reinstall all packages
This would be pretty much a roll your own central management system for that software.

Related

Install Python driver only by copying directories & files?

Can a driver be added to Python just by copying the appropriate directory and files (on a Raspberry Pi)? All the info I found uses a script or pip to install but those end in an error such as upper module unknown or just don't work.
The driver is for the Waveshare 2 inch OLED (https://www.waveshare.com/wiki/2inch_LCD_Module).
The installation procedure installs (using proper methods) wiringPi, RPi.GPIO, and spidev so I expect those are OK.
The actual OLED driver files (C and py dirs) however are installed under /home/andy/bcm2835-1.71/WiringPi/LCD_Module_RPI_code/RaspberryPi/python/lib and the C is then recompiled.
The py examples work if run from their directories but which directories & files need to be copied into the actual python3.9 tree so they're known and available to new projects, in the right (new?) sub-dirs and not just dumped in where they happen to work?
update:
The project can be made to work by adding the path but it seems like a messy hack.
sys.path.append("..")
sys.path.append("/home/andy/bcm2835-1.71/WiringPi/LCD_Module_RPI_code/RaspberryPi/python/")
# import LCD_2inch
from lib import LCD_2inch
Tried several installation methods, pip install and with path name.
Best answer seems to be from "ghp" on the raspberrypi forum. Copy the lib folder of the driver into the application folder structure, the sys.path.append used int in the demo code can be removed, and the entire project including the specific drivers required are all together.
Never manually copy drivers into the python /lib or subdirs of /lib since they will disappear if/when you update Python.
https://forums.raspberrypi.com/viewtopic.php?p=2083101#p2082883

Matlab error: Not find Qt plataform plugin 'windows'

I'm running a python function in Matlab and when I run I got the error:
This application failed to start because it could not find or load the
Qt plataform plugin 'windows' in '' '' , Available plugins are:
minimal offscreen, windows. Reinstalling the application may fix this
problem.
I already followed the steps in this video copying the platform folder to pyqt tools and in this post add to the environment path.
If I run my code in python it works fine, It seems that Matlab is not finding the Qt platform plugin 'Windows'
Someone can help me, please?
This is the problem of deploying Qt applications. Depending on platform you're using you can find tool for deploying, it is in Qt directory. For example, on my PC with Windows this is:
c:\Qt\5.15.0\mingw81_64\bin\windeployqt.exe
You can call this tool like this:
windeployqt <path-to-app-binary>
// OR
windeployqt --qmldir <path-to-app-qml-files> <path-to-app-binary>
There are many other options like --debug, --release, etc. Too see all of them, just call windeployqt.
On other platforms it could be linuxdeployqt, macdeployqt, also pyqtdeploy.
After calling this tool it will copy all required submodules to (near) your app, so it will find all it needs.
On my Windows system, I had the same experience that the Python function ran natively, but would not run via MATLAB. The problem was missing or incompatible dlls (not exe) files. To solve the problem I copied the exact dlls from my conda environment into the MATLAB \bin\win64 folder. That is, I copied these five dlls
qdirect2d.dll
qminimal.dll
qoffscreen.dll
qwebgl.dll
qwindows.dll
from the \Library\plugins\platforms folder of my conda environment
C:\Users\username\AppData\Local\Continuum\envs\myenv\Library\plugins\platforms
into the MATLAB folder
C:\Program Files\MATLAB\R2020b\bin\win64\platforms
While doing the copy, I overwrote three dlls which already existed in the MATLAB folder.

How do my python files get access to installed libraries (Pip)

Sorry in advance if this question has been asked before,
So after some time, I wanted to start a new python project. My previous computer (on which my python files were) died. I had saved my projects in my Dropbox. Now I installed python (3.8, there is also an anaconda installation, but it should not interfere with the python installation) on my new PC, and I cannot import any library to those files.
The python shell can find the imported packages (imported using pip), but even when I move the files to C:\Users\Username\AppData\Local\Programs\Python\Python38-32\Scripts (single user installation). It doesn't work.
I have tried uninstalling and re-installing pygame (in this example. Any library is unusable) using pip, pip3 and even pip3.8, I have added the .whl file by hand, it all didn't work. I have tried a virtual environment, but I can't get that to work either.
I run Windows 10 on a 64-bit computer.
first be sure you know which python installation you use with which import files etc.
you can copy your files not in scripts, but in lib somewhere in site-packages dir.
add your scripts to the python path! sys.path.add(.....) Otherwise python is blind and can't see them

Import numpy without installing

Is there a way to import numpy without installing it?
I have a general application built into an .exe with PyInstaller. The application has a plugin system which allows it to be extended through Python scripts. The plugin import system works fine for basic modules (lone .py files, classes, functions, and simple packages). Internally, it globs a plugin directory and then imports accordingly using __import__ or importlib.import_module.
The application is built with minimal dependencies in order to reduce the overall size of the executable. Also, it's not possible to know what dependencies a future plugin will require nor practical to include everything. However, some plugins will inevitably require dependencies. numpy is a good test case for solving this class of problems.
Here's what I've tried.
A wheel file is really just a directory. It can be added to sys.path and the contents imported.
import sys
sys.path.append(r"C:\path\to\numpy-1.16.3+mkl-cp36-cp36m-win_amd64.whl")
import numpy as np
The wheel file is read, but the import generates an error.
*** ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using c:\projects\zip_test\venv\Scripts\python.exe),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
The puzzling part is that the wheel file contains a .pyd for _multiarray_umath.
C:\projects\zip_test\plugins\New folder\numpy\core>dir
Volume in drive C is OS
Volume Serial Number is FE3D-6596
Directory of C:\projects\zip_test\plugins\New folder\numpy\core
07/25/2019 02:37 PM <DIR> .
07/25/2019 02:37 PM <DIR> ..
....
04/22/2019 02:55 AM 101,376 _multiarray_tests.cp36-win_amd64.pyd
04/22/2019 02:55 AM 2,494,976 _multiarray_umath.cp36-win_amd64.pyd
....
74 File(s) 583,173,551 bytes
5 Dir(s) 309,925,851,136 bytes free
This feels like a pathing issue. Yet adding the direct path for core/ to sys.path generates the same error.
sys.path.append(r"C:\projects\zip_test\plugins\numpy-1.16.3+mkl-cp36-cp36m-win_amd64.whl\numpy\core")
What's the deal? Why can't Python find numpy.core._multiarray_umath?
In response to repsones:
The wheel file was obtained from Christoph Gohlke.
My operating system is Windows 10 Pro 64-bit.
I am running Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32 (installed via Python Foundation build 3.6.8150.0) in a venv. The "system" Python is not on PATH.
I can install the numpy-1.16.3+mkl-cp36-cp36m-win_amd64.whl file via pip and import numpy as np works fine. I then uninstall numpy via pip uninstall -y numpy.
Running dumpbin /dependents _multiarray_umath.cp36-win_amd64.pyd produces:
Microsoft (R) COFF/PE Dumper Version 14.22.27905.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file _multiarray_umath.cp36-win_amd64.pyd
File Type: DLL
Image has the following dependencies:
mkl_rt.dll
python36.dll
KERNEL32.dll
VCRUNTIME140.dll
api-ms-win-crt-heap-l1-1-0.dll
api-ms-win-crt-stdio-l1-1-0.dll
api-ms-win-crt-math-l1-1-0.dll
api-ms-win-crt-runtime-l1-1-0.dll
api-ms-win-crt-string-l1-1-0.dll
api-ms-win-crt-convert-l1-1-0.dll
api-ms-win-crt-time-l1-1-0.dll
api-ms-win-crt-utility-l1-1-0.dll
api-ms-win-crt-locale-l1-1-0.dll
Summary
77000 .data
1000 .gfids
1C000 .pdata
30000 .rdata
3000 .reloc
1000 .rsrc
1BD000 .text
Of the dependencies, I can find mkl_rt.dll, python36.dll, and VCRUNTIME140.dll in the either the .whl or the venv. There are apparently two versions of KERNEL32.dll, one for x86 and one for x64. One of these resides in C:\Windows\System32.
The only information I could find about the api-ms-win-crt-*.dll are described on MSDN within the "Update for Universal C Runtime in Windows",
The Windows 10 Universal CRT is a Windows operating system component
that enables CRT functionality on the Windows operating system. This
update allows Windows desktop applications that depend on the Windows
10 Universal CRT release to run on earlier Windows operating systems.
These are for non-Windows 10 systems. There seems to be no "official" way to obtain them for a Windows 10 system. I was able to copy the dlls from a Windows 7 system. Naively putting them in the PYTHONPATH (not surprisingly) doesn't work. If they were to work at all, they would likely need to be registered. But 1) registering Windows 7 dlls on Windows 10, I am told, can make the system unstable and 2) definitely doesn't seem like a universal, portable solution.
First.
Build application with and without numpy. Check difference between builds.
Original error was: No module named 'numpy.core._multiarray_umath' Can means two things:
cannot found file
some dependence of this this pyd file is not satisfied. You may try to check it with http://dependencywalker.com/.
Yes, depending on how you define "install".
Numpy requires the use of .pyd files. A pyd file is essentially a dll. These are compiled code files which the Python interpreter references during run-time. Unfortunately, the Windows API calls used to load dll files requires them to exist at the filesystem level. When you try to import directly from a wheel, the pyd files (and the functions/classes they contain) aren't accessible. Hence, the error.
The (Relatively) Easy solution
One solution is to make the pyd files accessible on the filesystem. To do that, it's probably simplest to unpack the whole wheel file to disk1.
In that case, consider "how you define 'install'":
If you could run numpy straight from the wheel, wouldn't that mean numpy was installed?
What difference does it really make whether you're writing a wheel file or the contents of that wheel file?
Since the plugin system is downloading a wheel file onto the filesystem, the application must have write access. Download the wheel file and extract it into the plugins directory. As long as the plugin directory is on the PYTHONPATH (i.e. sys.path.append), the import will work. Smarts and so forth can be added from there. Of course, what level of smarts you need is a whole other issue. But that's the basic idea.
The Wizard's Path
Another solution is to create your own functionality to import a dll from memory. This is precisely the goal of Joachim Bauch's MemoryModule project. It seems the Py2Exe project used the MemoryModule at one point, through a module called zipextimporter.py. Unfortunately, the code is hard to find and seems outdated (Python 2.4). There is also a similar module called pymemimporter.py which is slightly more recent.
1 Numpy expects the pyd files to be in specific locations. If you want to extract only the pyd files, they'll need to be nested in the appropriate folders (e.g. numpy/core/_multiarray_umath.cp36-win_amd64.pyd). This is sufficient to import numpy, but unless the other modules are available, rather pointless.

"Unable to locate the SpatiaLite library." Django

I'm trying to make Django's SQLite3 accept spatial queries. This tutorial suggests that I add this to settings:
SPATIALITE_LIBRARY_PATH = 'mod_spatialite'
Which produces this error:
django.core.exceptions.ImproperlyConfigured: Unable to load the
SpatiaLite library extension "mod_spatialite" because: The specified
module could not be found.
I also tried doing this :
SPATIALITE_LIBRARY_PATH = r'C:\\Program Files (x86)\\Spatialite\\mod_spatialite-4.3.0a-win-x86\\mod_spatialite-4.3.0a-win-x86\\mod_spatialite.dll'
If I don't add this variable I receive this error when I migrate:
django.core.exceptions.ImproperlyConfigured: Unable to locate the
SpatiaLite library. Make sure it is in your library path, or set
SPATIALITE_LIBRARY_PATH in your settings.
Thank you..
Amusingly enough 5 days later I'm having the same issue. After a little bit of poking around I got it working:
Set
SPATIALITE_LIBRARY_PATH = 'mod_spatialite'
and extract ALL the DLL files from the mod_spatialite-x.x.x-win-x86.7z to your Python installation directory. The dll's apparently need to be in the same folder with python.exe. Also I imagine the mod_spatialite package needs to 32/64 bit according to your python installation. If you're missing some dll's, you get the same error "specified module not found" regardless of what dll file is missing, so it's a bit misleading.
Downloaded from http://www.gaia-gis.it/gaia-sins/
I used mod_spatialite stable version 4.3.0a x86 with Python 3.5.2 32-bit.
Other threads on the same issue with all sorts of answers:
Use spatialite extension for SQLite on Windows
Getting a working SpatiaLite + SQLite system for x64 c#
https://gis.stackexchange.com/questions/85674/sqlite-python-2-7-and-spatialite
On Ubuntu18.04,
adding SPATIALITE_LIBRARY_PATH = 'mod_spatialite.so'
with libsqlite3-mod-spatialite installed worked for me.
Note: The answer has mod_spatialite, while for me mod_spatialite.so worked.
This is how to install SpatiaLite (almost) inside virtualenv for Python 3:
Download cyqlite a special SQLite build with R-Tree enabled. It is required by GoeDjango.
Download mod_spatialite (Windows binaries are in the pink box at the bottom of the page) i.e. mod_spatialite-[version]-win-x86.7z
Unzip mod_spatialite files (flatten the tree and ignore the folder in the archive) into the virtuelenv Scripts folder.
This part I don't like but I not could find a workable solution without touching main Python3 installation.
Rename or otherwise backup c:\Python35\DLLs\sqlite3.dll
From cyqlite uznip sqlite3.dll file into the c:\Python35\DLLs\
Kudos: https://gis.stackexchange.com/a/169979/84121
I ran into this problem when trying to deploy GeoDjango on AWS Elastic Beanstalk. Turns out I needed to set SPATIALITE_LIBRARY_PATH = 'mod_spatialite.so' to SPATIALITE_LIBRARY_PATH = 'libspatialite.so' (installed at /user/lib64/libspatialite.so after running sudo yum install libspatialite and sudo yum install libspatialite-devel from my .ebextensions).
HOW ACTIVATE CORRECTLY SPATIALITE IN VIRTUAL DJANGO ENVIRONNEMENT IN WINDOWS OPERATING SYSTEM LIKE 7, 8, 10
I hope my answer will come to the safety of my developer friends who use Sqlite delivered by default in django to manage their geographic data by the spatialite link which is an extension also delivered by default in python (3 or +). I'm starting from the fact that you have a preconfigured virtual environment. The first thing to do is to download two zip: sqlite-dll-win32-x86-[version].zip and mod_spatialite-[version]-win-x86.7z and unzipp this in the same directory(overwrite if there are any conflicts)
Copy all previously unzipped files into your directory and paste them into your Scripts directory of your virtual environment
It all restart your pc if necessary, deactivate and reactivate your virtual environnement and code...

Categories

Resources