Sublime Text 3 on OSX.
I am writing a plugin that loads up a particularly poorly-written 3rd-party Python module that relies on the value of an Environment Variable to function properly (in particular, DYLD_LIBRARY_PATH).
Of course, I could set this globally, but I would love for the Sublime Plugin to be self-contained. I notice that plugins run in a child process of Sublime itself - is there any way to tell Sublime to provide the plugin_host process with a particular Environment Variable before it spins it off?
If not, does anyone know of another way to solve this problem? For performance and simplicity reasons, I would greatly prefer to have the python script be self-contained, rather than calling out to an external script that utilizes the library. Thank you.
Check out os.putenv() and os.environ for details on setting environment variables from Python. So, for your case, before importing your 3rd-party module, use something like this:
import os
os.environ['DYLD_LIBRARY_PATH'] = '/usr/lib/foo:' + os.environ.get('DYLD_LIBRARY_PATH')
import third_party
# and so on...
Related
I am trying to run COPASI in commandline but there is very little documentation.
I have downloaded and unzipped binary, I am not sure how to proceed in order to do something as simple as import an SBML file?
Here is all the documentation I found:
http://copasi.org/Support/User_Manual/Model_Creation/Commandline_Version_and_Commandline_Options/
It doesn't say which command do I use to call COPASI?
The command line version of COPASI is CopasiSE, which you can install besides the GUI CopasiUI via any source here http://copasi.org/Download/.
For example, see https://github.com/ICB-DCM/solverstudy/blob/master/Bash_Scripts/install_copasi.sh, in which case the executable resides in a local folder.
Regarding usage, I am not sure whether extensive API documentation is available. It probably won't help much, but here's how we wrapped it in a study via Python to call an underlying CPS model file https://github.com/ICB-DCM/solverstudy/blob/master/Python_Scripts/simulation_wrapper_copasi.py#L77.
I have the following piece of AppleScript.
use framework "Foundation"
display dialog "foo"
Note: the Foundation framework is not actually used in this example, because that part of the code is irrelevant for this example. This code alone already produces the error. In real life, we obviously only import a framework to use it, duh :-)
When I run this I get the unhelpful error:
Expected end of line, etc. but found identifier.
The identifier on which the macOS Script Editor stops is "dialog".
When I change the code to:
display dialog "foo"
The script runs as I expect it.
I have two questions:
Why does the top example produce an error?
Why does it produce this exact error? Or in other words: why is this error so unhelpful? Is this the case for AppleScript in general?
The predication in your answer is outdated. You can import frameworks in standard scripts nowadays (AFAIR since Yosemite).
If you apply an use framework statement you have to add
use scripting additions
to be able to access display dialog
! The information I based my answer on was out of date, see under my answer for the update.
The reason why the script errors is because:
"The importation of frameworks via the use statement is only supported in script libraries, not in other scripts or applets."
So basically: you're not allowed to use the use statement in a regular AppleScript script. You can only use it in script libraries.
To fix this we create a "script library", which is just another AppleScript file. Let's say we call this script library chewbacca.scpt. You need to place the script in a specific location on your Mac. (You have a few options for this location). AppleScript only looks in those locations when trying to import script libraries.
Then, to use the script library do something like:
tell script "chewbacca"
display dialog "foo"
end tell
That should give the desired result.
Update:
After reading some answers and reading some more documentation:
The way AppleScript is extended is by importing a library, these libraries are called osax (or "Scripting Additions") because their file names end in .osax. OSAX stands for "Open Scripting Architecture eXtension". To import an osax we write use library in our script (I'm not 100% sure about this). By importing an osax we can use the commands in that osax.
AppleScript (the language) does not have commands for things like: user interaction dialogs (display dialog), reading and writing files, file system commands, date functions, and text and mathematical operations.
But: Apple does provide an osax that offers these commands: StandardAdditions.osax, it's not hard to see why this is one of the most commonly used osax.
To import this osax:
use scripting additions
Now back to my question:
I see AppleScript behaving differently under certain conditions:
a script does not import an osax
a script imports an osax (but not StandardAdditions.osax)
In situation 1 it seems like AppleScript (the runtime?) silently auto-imports StandardAdditions.osax. I think this is the case because I can use display dialog.
In situation 2 AppleScript (the runtime) does not auto-import StandardAdditions.osax.
I can theorize about this different behavior:
I suspect for situation 1 they want to make it easier for people to get started with AppleScript so they auto-import the basic commands most people/beginners probably want to use.
The thinking behind situation 2 might have been something like:
"the developer is explicitly importing an osax so they may not have a
need for StandardAdditions.osax so let's not auto-import it".
I've found somewhere that it's a good idea to always explicitly import StandardAdditions.osax by adding use scripting additions to your script.
I'll follow this advice in the future.
XCode has really good completion when it comes to library functions.
But is there any way to accomplish simple 'in-file' completion like sublime or vim (with ctrl-p) does?
For example I define a variable named myCustomSuperVariable.
Now when I enter myCu, there should be a possibility to complete it, right?
I downloaded everything described as in pygtk for installation. Everything went fine until when I tried to type "import gtk", it threw an ImportError as follows:
from gtk import _gtk
ImportError: DLL load failed: ...(something unreadable)
Then I re-install the pygtk-2.22.0 again, the same problem existed. So what to do please? Thanks in advance!
The error you describe is usually caused by the python bindings (pygtk/pygobject/pycairo) being unable to load a dll it needs to function properly. Most of those errors are either caused by:
the GTK+ runtime not being on your PATH environment variable. This has long been
the advice on how to get pygtk working on Windows. Please don't change your user
or system PATH environment variable, it is no longer needed with the all-in-one
installer.
multiple GTK+ runtime versions are on your PATH environment variable and the first
(leftmost) one is not compatible with the pygtk/pygobject/pycairo versions you use.
This is why adding the GTK+ runtime to your PATH environment variable is a bad idea:
it is easy to mix up versions (sometimes GTK+ related installers add their bin
directory to PATH on installation which contains an older or incomplete runtime).
a rare case where some software package installed libintl.dll and iconv.dll into
%WINDIR%\system or %WINDIR%\sytem32
The most straightforward way to avoid the dll hell described above is to use the
PyGTK All-in-one installer (http://download.gnome.org/binaries/win32/pygtk/2.22/).
It contains both the Python bindings, the GTK+ runtime and even Glade and does
no longer require you to change the PATH environment variable.
Small warning: if you decide to use the all-in-one installer, you'll have to uninstall
the separate pygtk/pygobject/pycairo packages you've used before (or you'll be in
a world of trouble...)
read the source code, perhaps there is a need for a specific version of pygtk
edit the source code to work with your pygtk version
I had this issue as well. You didn't mention for sure in your answer, so I'll suggest the obvious (well...the obvious to people that have used it a while, perhaps). Did you use the following three lines of code to import? You have to use these, in order, to import PyGTK.
I'm assuming your version here is 2.24 like mine. If not, change it to the version you have.
import pygtk
pygtk.require('2.24')
import gtk
That should suffice in importing gtk.
Problem
As others have noted, don't put gtk in the path. I know, its tempting, and it works for XYZ, but it (to say the least) gets confusing. Dependency hell is bad enough on a platform like Linux which tries to make it easy for you.
For clarification, what the PyGTK All In One does for you is to install (what appears to be) a full gtk+ runtime directly in your python packages folder, so e.g. in C:\Python\Lib\site-packages\gtk-2.0\runtime
Solution
NOTE: %YOURPYTHONPATH% is an EXAMPLE variable which contains the path of your Python installation (e.g. C:\Python, or C:\Python27, or whatever it actually is). I suggest setting %PYTHON_DIR% or %PYTHON_PATH% if you want to use a variable to do this, as more programs are likely to use this.
To get your XYZ program requiring GTK to work, add %YOURPYTHONPATH%\Lib\site-packages\gtk-2.0\runtime\bin to the PATH when running your program; the correct versions of the DLLs it needs to link against are in that folder. All other GTK+ runtimes I had installed (GTK+/GTK2-Runetime) gave me errors.
Again; Do not attempt to set a user or system level variable (don't open up the dialog pictured below) as this will likely cause problems for you later unless you VERY sure you know what you are doing. If you are reading this, you most likely don't know as much as you think you do. Instead, alter the path in a cmd prompt, or use a batch/script file to set it up for you.
After you install the pygtk.org package, install each of these in the following order:
pycairo-1.8.6.win32-py2.6.exe
pygobject-2.20.0.win32-py2.6.exe
pygtk-2.16.0+glade.win32-py2.6.exe
gtk+-bundle_2.16.6-20100912_win32.zip
For more information:
http://freetstar.com/windows7-pygtk-gtk/
I got the secusses according the link on my PC.
I've written a little library that uses implicits to add functionality that one only needs when using the REPL in Scala. Ruby has libraries like this - for things like pretty printing, firing up text editors (like the interactive_editor gem which invokes Vim from irb - see this post), debuggers and the like. The library I am trying to write adds some methods to java.lang.Class and java.lang.reflect classes using the 'pimp my library' implicit conversion process to help you go and find documentation (initially, with Google, then later possibly with a JavaDoc/ScalaDoc viewer, and maybe the StackOverflow API eventually!). It's an itch-scratching library: I spend so much time copying and pasting classnames into Google that I figured I may as well automate the process.
It is the sort of functionality that developers will want to add to their system for use only in the REPL - they shouldn't really be adding it to projects (partly because it may not be something that their fellow developers want, but also because if you are doing some exploratory development, it may be with just a Scala REPL that's not being invoked by an IDE or build tool).
In my case, I want to include a few classes and set up some implicits - include a .jar on the CLASSPATH and import it, basically.
In Ruby, this is the sort of thing that you'd add to your .irbrc file. Other REPLs have similar ways of setting options and importing libraries.
Is there a similar file or way of doing this for the Scala REPL?
On the command line, you can use the -i option to load a file while starting the REPL:
scala -cp mystuff.jar -i mydefs.scala
Ofcourse you could wrap this in a shell script or batch file and run that instead of the normal scala command.
(I'm using Scala 2.8.0 RC3).
Not sure if this is what you are looking for, but if you put any jars in your SCALA_HOME\lib directory. Then those jars will be available for import in the REPL (using the import keyword).
EDIT: The most convenient option as of now is by setting the CLASSPATH environment variable. Any jars referenced in the CLASSPATH variable are also available for import in the REPL.
Quick answer probably not what you are looking for, but what about typing
:load path/to/some/scala/script/file.scala
in the console?
:load will read in a scala file and execute it as a script.
Another option is to use sbt set up your dependencies and execute the console command.
The final option I can think of is to set the classpath on the command line manually and point it to the jars / class file folders that you want the jvm to know about.
Let me know if any of this interests you and I can provide more details if needed.