Python version of AddFontResource() - python

I'm currently trying to install fonts across a bunch of servers. I've been able to use a script to copy the fonts over and "install" them onto the server but I need to be able to access the fonts without having to turn off the server or log off the account.
I found Windows AddFontResource() which is done using C++, but is there an equivalent function in Python or Powershell?
(I've been using Python and Powershell to do checks and installations.)

Have you tried using the win32api library? It has the SendMessage() function which can be used in conjunction with the windll.gdi32.AddFontResource() in ctypes
For example installing a TTF font file:
import win32api
import ctypes
import win32con
ctypes.windll.gdi32.AddFontResourceA("C:\\Path\\To\\Font\\font.ttf")
win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_FONTCHANGE)

Related

Is it possible to specify the search path for a module in a python script? If it is, how do I do that?

I have been coding in python for about 2 months, but I'm only familiar with basic object-oriented programming, so I do not really understand things like how searching for modules is implemented. (Basically I'm a noob.)
I pip installed a package called Opentrons Opentrons 2.5.2 and all its dependencies into the samefolder as a python script I'm currently writing. However when I tried to import the module below[1], I get an error saying that "Opentrons is not a module". Then, I tried shifting it into the python library because I found out the search path using the pprint module and it seems to work. I was wondering if I can specify the search path from the .py file itself instead of manually printing the search path and putting the file into the library that the script searches for. (Willing to put in images of the directories I put the opentrons package in if it helps.)
[1]
import sys
import pprint
pprint.pprint(search.path)
from opentrons import robot, containers, instruments
Edit: I realise that the fact that I am running all my scripts in a Spyder console located in a python 3.6 environment might be important.
You can try using the __import__ function, or importlib. This should allow you to specify the path.

Using Python in Grasshopper3D – Solution exception: No module named AppKit

I am using the Mac version of Rhino/Grasshopper.
I am trying to import a module for using Python inside Grasshopper.
When running the script, I receive the following error.
I have AppKit installed. I’ve even tried saving AppKit and PyObjC in a separate directory and using sys.path.append to access it.
Any idea what I am doing wrong or can try?
Code:
import rhinoscriptsyntax as rs
import AppKit
Code:
import rhinoscriptsyntax as rs
my_path = '/Users/author/Desktop/pyobjc'
sys.path.append(my_path)
import AppKit
The IronPython implemented in Rhino 3D will have search paths that are separate from other Python environments you may have installed.
On Rhino for Windows, you would have been able to run EditPythonScript in the Rhino command line to configure these paths via a dialog. Or _EditPythonScript for a strictly command line approach.
But on Rhino for Mac configuring these paths is less straightforward. There is not yet an editor, debugger, or user interface for configuration fully baked in.
There is a work around using the Atom editor that works with Rhino 5.2 WIP 5C41w and later. After installing the rhino-python package for Atom:
Press the control + option + s to open the Rhino Python Search Paths panel.
Add or edit one or more paths. You cannot edit the default system paths, but can add custom search paths and edit these as needed.
Click Save to update
From your Python scripts in Rhino, you should now be able to import AppKit and other packages from the newly specified locations.

How do i convert my Python script (that has imported modules) to a windows exe?

Here are the import modules that my script uses :
import datetime
from dateutil import parser
from tkinter import filedialog
import tkinter
import mailbox
import pprint
import json
import urllib.request
from tkinter import *
#my script code here
How can i convert it into a windows exe. Im using python 3.4. People have suggested cx_freeze however there is no documentation on it therefore have no idea how to use it? Py2exe worked on a test script with no imported modules, but when i tried to compile my script, it didnt work? If my script is called test.py, what would the cx_freeze command be to covnert it?
Try www.py2exe.org/
py2exe is a nice module that you may find useful.
Or, if you are in linux/mac then you might try freeze method try https://wiki.python.org/moin/Freeze
I highly suggest PyInstaller.
I used to use it in order to create the exe file with multiple library files, then compress all these files into a self extracting archive, obtaining a fully working standalone exe file.
It doesn't require other scripts or code, you only have to create the file using "Makespec.py" and "Build.py".
If I'm not wrong, there is a new version compatible with Python 3.4...otherwise you could convert your script to Python 2.7.

Tkinter OpenGL context in Python

I need to create an OpenGL context in Tkinker, for using it with PyOpenGL Python module.
Tkinker doesn't natively support OpenGL context, but I found this page on PyOpenGL docs, explaining how to use a wrapper included in the module for this:
http://pyopengl.sourceforge.net/documentation/context/
I tried to run the provided code but I got a message saying TOGL module was not found.
I downloaded the module from http://togl.sourceforge.net/, but couldn't get it to work.
PS. I did the test on Mac OS X, with Python 3.2, using virtualenv.
PyOpenGL provides Python bindings for the Tk OpenGL widget (Togl) but not Togl itself, that is why you had to download it. Now, to install Togl is easy but there isn't a tool ready to perform the task. Since the Python bindings will use Tcl to load the Togl module, the widget needs to live in one of the directories present in Tcl's auto_path, which is where Tcl looks for loading libraries. What you can do is start a Tcl interpreter, tclsh, and check which are these directories by doing puts $auto_path. In my case I copied the directory lib/Togl2.0 (inside the Togl's .tar.gz) to /opt/local/lib/tcl8.5. You can also extend auto_path to look for other directories, but I'm not covering that here.
Then I tested using Python 2.7 on Mac OSX. Doing import OpenGL.Tk tries to load Togl, too bad it fails. The reason is that Togl comes precompiled for i386, since I built Python as a universal binary all I did was run it as arch -i386 python2.7, and now import OpenGL.Tk works.

module "random" not found when building .exe from IronPython 2.6 script

I am using SharpDevelop to build an executable from my IronPython script. The only hitch is that my script has the line
import random
which works fine when I run the script through ipy.exe, but when I attempt to build and run an exe from the script in SharpDevelop, I always get the message:
IronPython.Runtime.Exceptions.ImportException: No module named random
Why isn't SharpDevelop 'seeing' random? How can I make it see it?
When you run an IronPython script with ipy.exe the path to the Python Standard Library is typically determined from one of the following:
The IRONPYTHONPATH environment variable.
Code in the lib\site.py, next to ipy.exe, that adds the location of the Python Standard Library to the path.
An IronPython executable produced by SharpDevelop will not do these initial setup tasks. So you will need to add some extra startup code before you import the random library. Here are a few ways you can do this:
Add the location of the Python Standard Library to sys.path directly.
import sys
sys.path.append(r'c:\python26\lib')
Get the location of the Python Standard Library from the IRONPYTHONPATH environment variable.
from System import Environment
pythonPath = Environment.GetEnvironmentVariable("IRONPYTHONPATH")
import sys
sys.path.append(pythonPath)
Read the location of the Python Standard Library from the registry (HKLM\Software\Python\PythonCore\2.6\PythonPath).
Read the location of the Python Standard Library from a separate config file that you ship with your application.
Another alternative is to compile the parts of the Python Standard Library your application needs into one or more .NET assemblies. That way you will not need the end user of your application to have the Python Standard Library installed.

Categories