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

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.

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.

PyPy Sandbox: Cannot import rpython module

I'm trying to use PyPy to create a server-side sandbox with limited access to my file system. I am working on Ubuntu 12.04 64 bit machine and have been trying to install the full source code for PyPy from here: http://pypy.org/download.html#sandboxed-version (scroll down to the section "Building from source").
My problem is that whenever I try running pypy_interact.py (located in pypy/pypy/sandbox), I get the following error:
ImportError: No module named rpython.translator.sandbox.sandlib
The module that cannot be imported has the following path: pypy/rpython/translator/sandbox/sandlib.py. The contents of pypy_interact.py are as follows:
import sys, os
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..\
', '..', '..')))
from rpython.translator.sandbox.sandlib import SimpleIOSandboxedProc
from rpython.translator.sandbox.sandlib import VirtualizedSandboxedProc
from rpython.translator.sandbox.vfs import Dir, RealDir, RealFile
import pypy
LIB_ROOT = os.path.dirname(os.path.dirname(pypy.__file__))
I feel like this is a really simple fix -- I just started learning Python a few days ago so I'm not exactly sure how to go about fixing the issue/don't understand imports too well yet. Any advice? Thanks very much.
Rpython typically expects that you set PYTHONPATH to include the root of your pypy checkout and not mess with the sys.path.
So you typically call the script via
PYTHONPATH=$PYTHONPATH:path/to/pypy/source path/to/pypy_interact.py

issue with vtk python wrapping: can't import vtk in interpreter but can import in console

I compiled vtk with python wrapping and I can us it on the command line.
However, I am using eclipse IDE and want to use vtk but no matter what I do with my PYTHONPATH variable, I still get the errors below:
from filtering import *
File "C:\Development\third-party\vtk-5.6.1\build\Wrapping\Python\vtk\filtering.py", line 9, in <module>
from vtkFilteringPython import *
ImportError: DLL load failed: The specified procedure could not be found.
My PATH and PYTHONPATH contain:
"C:\Development\third-party\vtk-5.6.1\Wrapping\Python"
"C:\Development\third-party\vtk-5.6.1\build\bin\Release" which has the *.lib, *.pyd, *.dll
In the DOS console, that is all I need and I can "import vtk" with no errors.
However, in eclipse I set the "External Libraries" of my project to contain the same directories above and I get an error.
The error happens at in the file "C:\Development\third-party\vtk-5.6.1\Wrapping\Python\vtk__init__.py" :
just after loading vtk "common" library.
...
# Load all required kits.
from common import *
from filtering import *
...
The funny thing is that both vtkcommon and vtkfiltering python (.dll, .lib and .pyd) are all in the same folder here: "C:\Development\third-party\vtk-5.6.1\build\bin\Release"
Can someone please help ? Why would import vtk work on console and not in eclipse IDE ?
I am using Version: Juno Service Release 2
Build id: 20130225-0426 for eclipse, vtk-5.6, python 2.6.5 and pydev 2.7.3
Can anyone with pydev + vtk experience help me?
I was having problems with VTK on PyDev and I just found this article, to do with PyCUDA on PyDev. It helped me get things working on Linux.
I followed the instructions there: Go to Run->Run Configurations and add a new environment variable LD_LIBRARY_PATH
I also found this forum, which deals with a similar problem on Windows. They suggest adding
-Djava.library.path="{dll path}"
to your runtime args in eclipse. See this page on the eclipse wiki.

Importing a dll in python on Ubuntu

I am using python 2.6.5 on an Ubuntu intalled server.
I need to integrate an API for our applicaion, in that case, i needed to use a DLL given to me by the API provider. Their example of code about api integration is written in Visual Basic... I made a search on google and found some examples of using ctypes , and i try using cdll and pydll, which caused the following error...
OSError: /home//some.dll: invalid ELF header
One possibility is using IronPython, but i do not have much information about ironpython so i am not sure if it will handle my needs completely..
Is there any available module that let me use that dll on python (or aynthing that i am missing from the exixting ones). It is hard to upgrade my python version?
DLLs may be windows creatures, but if a DLL is 'pure .NET' and doesn't utilize executables specific to windows etc., then it can work often in Linux, through Mono. (mono ipy.exe).
Ironpython's System and similiar windows modules are customized to be os agnostic (to a untested degree).
I have successfully run NHibernate, FluentNHibernate, log4net, and a few other commonly used DLLS in Ubuntu.
import clr
import sys
sys.path.append(os.path.abspath('./DLL')) #where your dlls are
clr.AddReference('System')
clr.AddReference('FluentNHibernate')
from FluentNHibernate.Cfg.Db import PostgreSQLConfiguration
The key seems to be to import DLLs in this fashion. If a dll imports another (fluentnhibernate imports nhibernate), you don't need to import Nhibernate for example.
DLLs are Windows creatures. The only way you'll be able to use a DLL is by using a Windows build of Python. You'll be able to run Windows Python on Ubuntu by having Windows installed inside a virtual machine. You also might be able to run it using Wine.
An alternative, of course, is to ask your API provider if they have a Linux version of the API.
First, check if your DLL is a .NET Assembly file. An "Assembly DLL file" has nothing to do with the assembler. It's simply a way the .NET framework stores its bytecode inside a DLL file!
Do file library.dll in Linux. If it says something like this:
PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
then you're lucky: it's an assembly file. You can run it on Linux.
Install Mono. Install Python.NET. Forget IronPython: it's dead.
Now, in Python.NET, you can do this:
import clr
clr.AddReference('./library.dll')
# the library has just registered a namespace we can use
from LibraryName import *
but how do you know what to import?
Auto-complete.
Or use monop tool to inspect the DLL like this:
$ monop -r library.dll
Assembly Information:
LibraryName
Version=9.9.3.0
Culture=neutral
PublicKeyToken=null
LibraryName.ClassName
...
$ monop -r library.dll LibraryName.ClassName
public class ClassName {
public ClassName (string inputString);
...
}
and it will tell you everything about that library

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