Implementing PyObjc NsObject Subclass - python

I have an Objc-C project in which I would like to be able to process some data trough Python. For that, I decided to implement a PyObjc NSObject subclass, which I would then link to my Obj-C classes trough the interface builder.
I added the Python framework to my project, and an python file with the following simple code:
import objc
from Foundation import *
from AppKit import *
class PythonWrapper(NSObject):
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
I created a PythonWrapper instance in my XIB (the builder automatically recognized the name of it). But, when building, even without having linked it already to the other classes, I get the following issue:
Ignoring file /Developer/SDKs/MacOSX10.6.sdk/Library/Frameworks//Python.framework/Python, missing required architecture x86_64 in file
Also, the xCode log also shows:
Unknown class 'PythonWrapper', using 'NSObject' instead. Encountered in Interface Builder file at path /Users/joao/Library/Developer/Xcode/DerivedData/Jalioneiro-ekjwzbkqqgpyekadkyebhgdsjcxo/Build/Products/Debug/Jalioneiro.app/Contents/Resources/en.lproj/Interface.nib.
What am I missing here? Is there any other way to link python code to my Obj-C classes?
Note: I'm working on xCode4

I have heard that Xcode 4 does not play so nicely with PyObjC, but I'm still using 3, so I can't be sure.
The reason for your second error (and this bites me too all the time) is that you need to import your custom classes into your project's main.py:
#import modules required by application
import objc
import Foundation
import AppKit
from PyObjCTools import AppHelper
# import modules containing classes required to start application and load MainMenu.nib
import MyAppDelegate
import MyCustomView
import MyArrayController
# pass control to AppKit
AppHelper.runEventLoop()
The first error looks like you're trying to build a 64-bit-only application. The PyObjC that shipped with Snow Leopard is only compiled as 32-bit (actually, I think the same is true of the default Python). Try switching your Target's build architecture to either 32-bit or 32/64-bit Universal. You could also go down the path of re-compiling PyObjC and including it in your app's bundle -- haven't tried it myself.

Related

pythonnet clr how to add reference to a dll assembly?

I am trying to import a dll package into python through pythonnet clr. I am aware that the package CLR and pythonnet both end up having a namespace called clr so the command "import clr" can be equivocal. Long story short, I seem to need pythonnet not the other one. I would like to be able to specify the address of the dll assembly; this one works:
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
Apath=os.path.normpath("C://Folder//Folder//Folder//AssemblyA.dll")
clr.AddReference(Apath)
but this one fails (got the idea of this one from here):
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
sys.path.append(BfolderPath)
clr.AddReference('AssemblyB.dll')
I get the following error when I try running the second one:
"System.IO.FileNotFoundException: Unable to find assembly 'AssemblyB.dll'.
at Python.Runtime.CLRModule.AddReference(String name)"
this one also fails
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
clr.AddReferenceToFileAndPath(Bpath)
"AttributeError: module 'clr' has no attribute 'AddReferenceToFileAndPath'"
ps1. I need the second or the third way to work because I have to be sure the second assembly is not confused with another one with a similar name.
ps2. I cannot find the documentation of pythonnet or see what kind of commands are available in my clr. Any idea?
Any tip is appreciated.
In second idea,
remove .dll from clr.AddReference('AssemblyB.dll') and use clr.AddReference('AssemblyB') because clr.AddReference() Requires only assembly name whether is it .exe of .dll not the folder path
That's why first idea is not working!
And for third idea clr.AddReferenceToFileAndPath() is not working because it part of Ironpython not pythonnet.
Ironpython is also used like pythonnet, but Ironpython is using Managed python code, while pythonnet is not using Managed code.

Embedded Python in C++ -- rolling in numpy -- where's the import lib?

I'm on Windows using/embedding Python 3.4
I'm trying to embed Python in an existing C++ application. I've pretty much done the job, but I can't get numpy to work.
I have numpy installed via the whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/ Everything works just fine in python.exe, but when I do something like...
PyRun_SimpleString("import numpy\n");
...in the C++ application, I get a traceback which ends with:
\Python\lib\site-packages\numpy\core\__init__.py, line 6, in <module>
from . import multiarray
cannot import 'multiarray'
Well, this made some sense to me. The module multiarray is in ...\Python34\Lib\site-packages\numpy\core\multiarray.pyd I think these .pyd files are basically DLLs, which means an import library is needed.
For example, ...\Python34\lib contains import libraries (I think) for other modules that are contained in pyd files. You have to link against the import lib. in order to import the module in the C++\Python (eg via PyRun_SimpleString("import _elementtree\n"); )
I suppose I need an import library for numpy to link my C++ application against. Anybody know off hand if building numpy from source pops these out? The build process looks tricky, but maybe it's the only options here.
Also, obviously when Python is built, it can't be linked against import libraries for modules that aren't installed yet. In other words, my python.exe wasn't linked against this magic import library I'm looking for, but it can still import numpy, how? Can I recreate this in my application?

Python: cannot import from PyQt5.QtCore (attempting example script)

I'm introducing myself to PyQt5 through one of its included examples. On this windows 7 machine, I have installed Python 3.4 x64, and PyQt5 using its binary provided on riverbankcomputiong.com. The documentation says that the binary already includes everything necessary to run. I (perhaps incorrectly) assumed that i can safely skip the "configure" and "build" steps at Riverbank's installation guide, since the guide only talks about .zip, .tar, etc. files.
I used the tutorial located here:
http://www.pythonschool.net/pyqt/introduction-to-pyqt/
Which also says "just run the binary to install pyqt4 there is no step three."
When i attempt to run any tutorial containing reference to PyQt4 or PyQt5:
from PyQt5.QtCore import *
I get the following error message:
ImportError: DLL load failed: The specified module could not be found.
But when i enter the following:
import PyQt5
The interpreter seems to be okay with it -- no errors.
I can't help but think I've done something wrong installation, because even when I run the examples included with PyQt4/PyQt5, i get importerrors. It seems as though QtCore doesn't even exist in relation to PyQt4 or PyQt5. What's going on here?
There seem to be a few possibilities:
You might need to update your PATH environment variable to include the location where the library is installed (i.e. directory location where the DLLs are)
Take note of where you're launching the interpreter or scripts from as their current path may not necessarily include what you expect.
You can check this with:
>>> import os
>>> os.environ['PATH'].split(os.pathsep)
Make sure the installation process was truly successful (i.e. zero errors) and that you downloaded the correct binary package for your PC's architecture.
I ran into a similar issue with PySide where import PySide would work but import PySide.QtCore would fail.
I'd expect the code to work after verifying those things:
>>> from PyQt5.QtCore import *
>>>
If it doesn't, update the question with additional details like things you checked, their values, error messages, etc.

Import Error embedding IPython into PyQt application

I have a fully functioning PyQt4 based application that I am now trying to embed an IPython console into for use as a kind of scripting environment.
I've roughly adapted the example from IPython's Github page to fit into my application's module system. However, if any PyQt import happens before the IPython imports
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.inprocess import QtInProcessKernelManager
I get the following error:
ImportError:
Could not load requested Qt binding. Please ensure that
PyQt4 >= 4.7 or PySide >= 1.0.3 is available,
and only one is imported per session.
Currently-imported Qt library: 'pyqtv1'
PyQt4 installed: True
PySide >= 1.0.3 installed: False
Tried to load: ['pyside', 'pyqt']
I've traced this error to IPython's qt module, but essentially forcing 'pyqtv1' to be loaded (by replacing api_opts = [QT_API] with api_opts = ['pyqtv1'] breaks QString inside the IPython window.
However, if I make it so that those imports happen first (by importing the module this code is in before anything else), that error goes away and it basically breaks QString completely with an ImportError: cannot import name QString.
I've verified that if I make a self-contained QApplication in the module that contains the IPython instance, making sure to import PyQt.QtGui after doing the IPython imports, it works ask expected. It's only when I try and mix the IPython code with any other PyQt code that things break.
Any suggestions as to how to fix the error?
I'm on Ubuntu Linux 12.04, and IPython is version 2.0.0-dev pulled on March 10th.
PyQt offers two different APIs for strings: you can choose which one to use with code like this:
import sip
sip.setapi('QString', 2)
from PyQt4 import QtGui
Once you import PyQt4, that API is set and cannot be changed. On Python 2, it defaults to version 1 for backwards compatibility, but IPython requires API version 2. Version 2 is the default on Python 3, and PySide's API is equivalent to version 2.
If you've used the v1 API in your application, you cannot use the IPython Qt console embedded in that application, except by porting it to the v2 API. However, you can embed an IPython kernel in your application, and connect to it from a Qt console in a separate process.

The difference between 'from pylons import config' and 'import pylons.config'

Im trying to import a company module into my software and I get the error:
ImportError: No module named config
from:
from pylons.config import config
So obviously, the module that im importing requires pylons.config but cant find it in my virtual environment.
If I go to the terminal and try some Python scripts I can seem to find the config file if I try:
from pylons import config
but will error if I try:
import pylons.config
Why is this?
And does anybody how or where I can get:
from pylons.config import config
to work. Bearing in mind that I cannot change the code for this module, only mine which is importing it or my own system files.
UPDATE
If anyone finding this page has a similar problem you may find that you are trying to run two modules with different versions of Pylons.
For example, you are creating a login application called myApp. You have some Python modules which help with login handling called pyLogin.
First you install pyLogin with python setup.py install. This adds the libraries to your site packages and updates any libraries it depends on, such as SqlAlchemy.
Next you install myApp in the same way which again updates libraries and dependencies.
This problem will occur if pyLogin and myApp are using different versions of Pylons. If pyLogin is using Pylons 0.9.6 and myApp is using Pylons 1.0 for example, then the pyLogin code will be called from myApp but it will be running in the wrong Pylons framework and hence will require EITHER from pylons import config or from pylons.config import config, but will only work with one. If it is using the wrong call for Pylons then you will find yourself with this error message.
So the only solution to this error is to either find earlier or later libraries which use the same Pylons version as your application or to convert your application to the same Pylons version as the libraries you are using.
There is a diffrence between two usages...
import loads a Python module into its own namespace, while from loads a Python module into the current namespace.
So, using from pylons import config imports config to to your current namespace. But trying to import a class or function using import is not possible since there is no namespace to keep them... You can only import modules, and use functions or classes via calling them with their own namespace like
import pylons
....
pylons.config #to retreive config
More about import in Python

Categories

Resources