I'm pretty new to Vim and I just set it up so that I can write Python code, with code completion, folding, etc. and am able to compile it also with a shortcut via plug-ins.
The thing is, I would also like to write some HTML/CSS in Vim as well and I'd like to install some similar plug-ins. I know that I could do this and configure different short-cuts for each language, but I'd like to set it up into two separate workspaces, so that am either working in my python or html workspace but not both. Is there any way to do this? Thanks in advance!
That's what compilers scripts are for!
The idea is to put a "compiler script" in your vim's compiler directory. That script is actually a settings file(the difference between script files and settings files in vim is only conceptual - technically they are the same), just like your .vimrc file. That script should contain configurations that are only loaded when you want them to. For example :compiler python loads your python settings.
Check out :help compiler for more info.
There are also "filetype plugins" - the main difference between them and compilers is that they are loaded automatically by the vim's filetype detection mechanism - which is actually an extensive set of scripts that can detect pretty much any filetype - unless you use an exotic language, or define your own extension, and even then you can extend that mechanism with your own ftdetect scripts. This is different from compiler scripts, which you need to explicitly call via a :compiler command, or define an :autocmd that calls the :compiler command.
Check out :help filetype for more info.
Compiler scripts more suitable for compiler-specific settings like make settings and build/run shortcuts, and filetype plugins more suitable for settings. It makes sense to build a C program the same way either if you are in a .c or .h file, if you are in the makefile, or if you are in an one of the program's resource text files.
Filetype scripts are more suitable for filetype-specific settings like syntax or code completion. It doesn't make sense to use C syntax and code completion for a C program's makefile or .ini file.
That said - for interpreted languages it doesn't really matter(unless you use a makefile to run them)
Related
In this guide to not being a total mess doing research, the authors talk about using a .py file to execute a directory in order -- that is, delete all the output files (.pdf, .txt, etc) and run just the .py and everything will be recreated from the raw data, stata files, maybe other .py's, etc etc.
What is the best way to do this in Python? I know one option is to use subprocesses, but is that the only option? Basically, how can I best mimic a .bat file using Python on a Mac.
You can certainly use Python for shell-script type stuff - with the bonus that it will be relatively portable.
Another option you could consider is "BASH" "(The Bourne Again SHell). That will do everything you can do with .BAT files (and much more). Search for BASH shell scripting.
Whether Python or BASH is the right tool for the job depends on whether you're mostly just writing glue (to call a bunch of other programs) or if you're actually writing complex logic yourself. If it's the former, then I'd go with BASH.
I have developed an application for a friend. Aplication is not that complex, involves only two .py files, main.py and main_web.py, main being the application code, and _web being the web interface for it. As the web was done later, it's kept in this format, I know it can be done with one app but not to complicate it too much, I kept it that way. Two two communicate with some files, and web part uses Flask so there's "templates" directory too.
Now, I want to make a package or somehow make this easier for distribution, on a OSX system. I see that there is a nice py2app thingy, but I am running Windows and I can't really use it since it won't work on Win. I also don't know will py2app make problems since some configs are in text files in the directory, and they change during the runtime.
So, I am wondering, is there any other way to make a package of this, some sort of setup like program, or maybe some script or something? Some simple "way" of doing this would be to just copy the files in the directory in the "Documents", and add some shortcuts to the desktop to run those two apps, and that would be it, no need for anything else. DMG would be fine, but not mandatory.
I believe what you are looking for is to add: #!/usr/bin/python to the first line of your code will allow your friend to just double click on the file and it should open. Just as a warning osx does not tell us what version and as such what version of python and what standard libraries are going to be present.
Also, just make sure that if they have played around with their settings to much and they double click on python it does not work they will have to choose to open the file in "terminal.app" in the Utilities Applications folder (/Applications/Utilities/terminal.app)
The other idea is borrow a mac and compile it with the py2app program that you already mentioned. Otherwise there is no generic binary file that you will be able to compile in windows and have run on mac.
I have a python application that is trying to load some Java libraries (specifically Axis2 web services). When I add the necessary jars in Eclipse via PyDev Project Source Folders, everything seems to work fine. However, I want to be able to do this at run time by adding to sys.path, but then my application doesn't seem to work.
In both cases I can load the jars just fine, but something must be different for there to be different results. My question is, is there a difference between adding jars via the sys.path at run time with sys.path.append() versus passing -D to the jython interpreter?
The problem turned out to be a difference in the way that Eclipse starts up the Jython interpreter as opposed to starting Jython manually from the command line. In the Eclipse Run Configuration pane, there is a way to see the command that is used to run your application. Mine looked like this:
/usr/lib/jvm/java-7-openjdk-i386/bin/java
-classpath /usr/local/lib/jython2.5.3/jython.jar:...
org.python.util.jython
-Dpython.path=...
myScript.py
(Note: I added line breaks for readability)
So it seems that Jython is launched from Java and that the Java classpath had to be fed the paths in addition to just the python path.
I use PyDev in Eclipse with the Qt integration. With an external tool I can create python source in a .py from a qt .ui file. This is the external tool:
http://permalink.gmane.org/gmane.comp.python.xy.devel/413
The problem is that the generated python .py file has a name like MyGeneratedFile.ui.py.
How can I adapt the external tool to have the extension of the generated file without .ui thus MyGeneratedFile.py ?
So it seems the problem boils down to ${resource_loc}, since this gives you the full path name /path/to/file/filename.ui - Yes, it does include the .ui hence when you say ${resource_loc}.py this translates into /path/to/file/filename.ui.py
So probably the simplest way to correct this problem since I couldn't find a way to make eclipse remove the file extension for me was making a very small script to do work.
You might need to modify it slightly to work for your pyuic installation.
/usr/bin/pyuicEclipse:
#!/bin/bash
pyUICCommand="/usr/bin/pyuic" # change this per your installation
x=$1
f=`basename $x`
d=`dirname $x`
fNoUI="`echo $f | sed 's/\.ui$//'`" # removes .ui extension from file basename
$pyUICCommand -o ${d}/${fNoUI}.py $x
make it executable and the eclipse configuration I used was much simpler:
PyUIC->Main->Location: /usr/bin/pyuicEclipse ---obviously change this to yours
PyUIC->Main->Arguments: ${resource_loc}
PyUIC->Refresh - check "Refresh Resources upon Completion"
PyUIC->Build - uncheck "Build before Launch"
PyUIC->Common - don't do the File option that was mentioned in that article
This works on linux, so if you're on another OS it may need some slight modification, but I hope this solves your problem :)
In the interests of maintaining the cross-platform nature of Eclipse, I've knocked up a DOS equivalent of platinummonkey's bash script. It's not quite so robust, but it does the job:
#echo off
set pyUICCommand="pyuic"
set fname=%1
set fname=%fname:.ui=.py%
%pyUICCommand% -o %fname% %1
There is an easy solution to this problem that requires no scripting at all.
Install pathtools plugin either through Eclipse updates or via the Eclipse marketplace:
Setup an External Tools Configurations option in Eclipse as follows
In Main:
Name: pyuic_run. (or something similar)
Location: path to the python interpreter (or pyside-uic.exe if you use this)
Arguments: On the first line, put the path to pyuic.py (not needed if you use pyside-uic.exe as it will be above). Use double quotes around the path if it contains spaces. On the second line put "${resource_loc}" (this will set the name of the resource file)
In refresh: Enable "Refresh resources upon completion" (to see the final file)
In Build: Disable "Build before launch" #not necessary here
In Environment: No changes
In Common: Activate the "File" option and set the path to be:
${parent-path}/${name-sans-extension}.py
Note that ${parent-path} and ${name-sans-extension} are arguments made available through the pathtools plugin.
If you apply this and then run the configuration on a .ui resource file, you'll see a new .py file created.
Is there a way to make Python ignore any .pyc files that are present and always interpret all the code (including imported modules) directly? Google hasn't turned up any answers, so I suspect not, but it seemed worth asking just in case.
(Why do I want to do this? I have a large pipeline of Python scripts which are run repeatedly over a cluster of a couple hundred computers. The Python scripts themselves live on a shared NFS filesystem. Somehow, rarely, after having been run hundreds of times over several hours, they will suddenly start crashing with an error about not being able to import a module. Forcing the regeneration of the .pyc file fixes the problem. I want, of course, to fix the underlying causes, but in the meantime we also need the system to continue running, so it seems like ignoring the .pyc files if possible would be a reasonable workaround).
P.S. I'm using Python 2.5, so I can't use -B.
You could use the standard Python library's imp module to reimplement __builtins__.__import__, which is the hook function called by import and from statement. In particular, the imp.load_module function can be used to load a .py even when the corresponding .pyc is present. Be sure to study carefully all the docs in the page I've pointed to, plus those for import, as it's kind of a delicate job. The docs themselves suggest using import hooks instead (per PEP 302) but for this particular task I suspect that would be even harder.
BTW, likely causes for your observed problems include race conditions between different computers trying to write .pyc files at the same time -- NFS locking is notoriously flaky and has always been;-). As long as every Python compiler you're using is at the same version (if not, you're in big trouble anyway;-), I'd rather precompile all of those .py files into .pyc and make their directories read-only; the latter seems the simplest approach anyway (rather than hacking __import__), even if for some reason you can't precompile.
It's not exactly what you asked for, but would removing the existing .pyc files and then not creating any more work for you? In that case, you could use the -B option:
>python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
In case anyone is using python 2.6 or above with the same question, the simplest thing to do is:
Delete all .pyc files
Run all your python interpreters with the -B option, so they won't generate .pyc files.
From the docs:
-B
If given, Python won’t try to write .pyc or .pyo files on the import of source modules. See also PYTHONDONTWRITEBYTECODE.
New in version 2.6.
If you can't delete all the .pycs, then you could:
1) Run all your python interpreters with the -B -O options.
This will tell python to look for .pyo files for bytecode instead of .pyc files (-O) and tell python not to generate any bytecode files (-B).
The combination of the two options, assuming you haven't used them before, is that Python won't generate any bytecode files and won't look for bytecode files that would have been generated by older runs.
From the docs:
-B
If given, Python won’t try to write .pyc or .pyo files on the import of source modules. See also PYTHONDONTWRITEBYTECODE.
New in version 2.6.
-O
Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. See also PYTHONOPTIMIZE.
Perhaps you could work around this by, for example, scheduling a job to periodically shut down the scripts and delete the .pyc files.
Well, I don't think Python ever interprets code directly if you're loading the code from a file. Even when using the interactive shell, Python will compile the imported module into a .pyc.
That said, you could write a shell script to go ahead and delete all the .pyc files before launching your scripts. That would certainly force a full rebuild before every execution.
You may find PEP 3147 - PYC Repository Directories to be of great interest from Python 3.2 onwards.