RubyPython can't import nltk on OS X Lion - python

I've got a copy of Lion with all the dev tools installed. Both Python (2.7) and Ruby (1.8) are running just fine. I've installed the Natural Language Tool Kit for Python and tried it out in the Python interpreter and it works
import nltk
>>true
So that works. I've also installed the RubyPython gem, and it seems to work too, but it can't find the nltk module. It's possible I'm doing something wrong. In irb:
require "RubyGems"
require "rubypython" #both true
RubyPython.start # true
n = RubyPython.import "nltk" # RubyPython::PythonError: ImportError: No module named nltk
c = RubyPython.import "cPickle" # works!
RubyPython.stop
I can't figure this one out. The PythonError seems to indicate to me (because it's just a call to the direct C APIs) that the nltk module can't be found by any form of python. But the interpreter finds it just fine. RubyPython, however, cannot.
I've also tried forcing RubyPython to use python2.7 but no change.
What am I missing?

It's simple! For some reason, RubyPython was looking in the wrong place for my Python modules. This was verified by importing sys in both the RubyPython script and in Python, and comparing sys.path. I ended up fixing it by taking the path list of sys in the Ruby script and adding what was missing from the pure Python's path. Then I could load NLTK.

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.

J meter with Python : how to import the packages

Iam new bee to the jmeter
My code is working in the Python 2.7 with importing additional packages Dateutil, parser .
Problme : But when I am trying to run same code in the J Meter-JSR-223 PreProcessors , an error saying No module named dateutil in.
So , I have tried another approach to use Jython .
Installed the Jython ( downloaded the dateutil) and provide the packages reference under
import sys
sys.path.append('C:/Jython27/Lib/site-packages')
sys.path.append('C:/Jython27/Lib/site-packages/python_dateutil-2.4.2-py2.7/dateutil')
sys.path.append('C:/Jython27/Lib/site-packages/python_dateutil-2.4.2-py2.7/dateutil')
Now packages error is gone but string syntax error is present .
java.sql.Date' object has no attribute .
I believe dateutil package can be picked up from CPython as it doesn't require any extra wrappers for Java.
Install dateutil normally using pip like:
pip install python-dateutil
Add site-packages folder of Python (not Jython) installation to sys.path like:
sys.path.append("C:\Python27\Lib\site-packages")
That's it, now you should be able to use dateutil module functions from the JSR223 Test Elements:
Be aware that invoking Python scripts via Jython interpreter is not the best idea from performance perspective and if you're about to invoke your Python code only limited number of times and/or with a single thread - it might be better to go for the OS Process Sampler.
If you plan to use the Python code to create the main load - consider using Locust tool instead of JMeter. If you don't want to change JMeter a good approach would be rewriting your Python code in Groovy - it will be way better from the performance perspective.
hi please find follwing
import sys
sys.path.append('C:/Python27/Lib/site-packages')
sys.path.append('C:/Python27/Lib/site-packages/python_dateutil-2.4.2-py2.7/dateutil')
from dateutil.parser import *
sourceDateTimeOfEvent = ""
dateTimeOfEvent = ""
a=parse('2016-07-01 13:00:00')
sourceDateTimeOfEvent = a.isoformat()+"+05:30Z"
dateTimeOfEvent = a.isoformat()+ "Z"
vars.put("sourceDateTimeOfEvent", sourceDateTimeOfEvent)
vars.put("dateTimeOfEvent", dateTimeOfEvent)
This sourceDateTimeOfEvent and dateTimeOfEvent considered as two variables and passed it to the json file

Python module created with Boost.Python won't be imported

I have a big C++ module with Python 3 bindings using Boost.Python, that I compile to a .so file using CMake on macOS.
When I try to import it in the REPL, everything seems to work fine:
>>>import myModule
>>>
However, as soon as I run the import statement, the famous rocket icon of Python shows up in the Dock and stays there jumping for some minutes and stops after. Obviously then, I cannot access any of the functions defined in my module, so the import looks fine but doesn't actually do anything.
I tried looking in the Console and saw that whenever I import myModule, I get two launchservicesd[83]: SecTaskLoadEntitlements failed error=22.
It brought me to this and that related questions but I can't find what the exact problem is.
The C++ module is huge so I just can't look at the code and find the problem, thus I'm asking for any hints about at least how to debug that problem.
I can suggest the following steps:
Try to import that module though local python session. So, run interactive python interpreter, and 'import myModule'.
If bad, try to check:
are python version, with which myMoudle was linked with, is similiar to used interpreter
check that build architectires are the same
check that you can load even simple boost.python example module
If ok, check that you have correctly set up module search path in your python code.

Where do I download grass.scripts and/or grass.pygrass?

I am trying to learn how to do some scripting to run Grass externally with Python but I can't seem to even find the basic modules called for this type of script. All of the documentation I have seen describes using grass.scripts module but I don't know where to download this. I also saw some information on grass.pygrass but I can't find this either.
I thought that maybe it was builtin to a newer version of Grass so I just downloaded 7.0.0 beta2 and I still find grass.scripts files. Is this a python module like others (matploblib, numpy, scipy etc.) or is it internal to Grass? Sorry for the remedial questions but I am lost here.
I ran the following script (taken from http://grasswiki.osgeo.org/wiki/GRASS_and_Python with quotations added since I run Python 2.7)
GISBASE= 'C:\GRASS-64'
GISRC= 'C:\Documents and Settings\user\.grassrc6'
LD_LIBRARY_PATH= 'C:\GRASS-64\lib'
PATH= 'C:\GRASS-64\etc;C:\GRASS-64\etc\python;C:\GRASS-64\lib;C:\GRASS-64\bin;C:\GRASS-64\extralib;C:\GRASS-64\msys\bin;C:\Python26;'
PYTHONLIB= 'C:\Python26'
PYTHONPATH= 'C:\GRASS-64\etc\python'
GRASS_SH= 'C:\GRASS-64\msys\bin\sh.exe'
and it is fine (though I don't know what it does) but when I add
from grass.pygrass.modules import Module
it returns
ImportError: No module named gras.pygrass.modules
Normally I would download and install the module and the problem would be fixed but I can't find it anywhere.
I ran the following script (taken from http://grasswiki.osgeo.org/wiki/GRASS_and_Python with quotations added since I run Python 2.7)
GISBASE= 'C:\GRASS-64'
GISRC= 'C:\Documents and Settings\user\.grassrc6'
LD_LIBRARY_PATH= 'C:\GRASS-64\lib'
PATH= 'C:\GRASS-64\etc;C:\GRASS-64\etc\python;C:\GRASS-64\lib;C:\GRASS-64\bin;C:\GRASS-64\extralib;C:\GRASS-64\msys\bin;C:\Python26;'
PYTHONLIB= 'C:\Python26'
PYTHONPATH= 'C:\GRASS-64\etc\python'
I don't know why you think Python 2.7 requires you to add quotes. It doesn't.
But it does require you to replace all those Python26 with Python27. And that could easily be your problem. You don't have anything at C:\Python26.
And if you installed Grass somewhere other than C:\GRASS-64 you obviously need to change that as well. (From elsewhere in the file, I get the feeling that 64 refers to version 6.4, not to being 64-bits or something else, and you've downloaded 7.0.0, so I suspect it got installed to something like C:\GRASS-70.)
Or it could be this:
ImportError: No module named gras.pygrass.modules
If you import gras.pygrass.modules instead of grass.pygrass.modules, that's obviously not going to work.
Also, are you actually running that script in the same cmd.exe window that you use to start Python? If not, it's not going to help.
Meanwhile:
All of the documentation I have seen describes using grass.scripts module but I don't know where to download this.
The documentation you linked to answers that question, although it may not be obvious to a novice (once you figure this out, you might want to suggest documentation improvements upstream):
The related files are at $GISBASE/etc/python/grass/script/*.py.
On Windows, of course, that's $GISBASE isn't literally right; it's actually %GISBASE%, which is something like C:\GRASS-64, but it's the same thing. The files are already on your system; there is nothing to download. That's the whole point of setting PYTHONPATH to C:\GRASS-64\etc\python.
Is this a python module like others (matploblib, numpy, scipy etc.) or is it internal to Grass?
Well, it's not exactly "internal to Grass", but it's distributed with Grass, rather than as a separate module.
I'm still not sure I understand everything but I seem to be past this hurdle. I used the script from (https://gis.stackexchange.com/questions/89452/problem-with-python-script-to-control-grass-gis-from-outside-how-to-import-gra/90160#90160) and changed all the relevant path information to suite my installation and apparently, now the grass.script module is accessed. Here is my working script
import os
import sys
gisbase = os.environ['GISBASE'] = 'C:\program files\grass gis 6.4.3' #GISBASE needs to point the root of the GRASS installation directory
gisrc = 'C:\grassdata'
gisdbase = 'C:\grassdata'
location = 'newLocation'
mapset = 'TC'
LD_LIBRARY_PATH = 'C:\program files\grass gis 6.4.3\lib'
PATH = 'C:\program files\grass gis 6.4.3\etc';'C:\program files\grass gis 6.4.3\etc\python';'C:\program files\grass gis 6.4.3\lib';'C:\program files\grass gis 6.4.3\bin';'C:\Python27';'C:\program files\grass gis 6.4.3\Python27';'C:\program files\grass gis 6.4.3\msys'
PYTHONLIB = 'C:\Python27'
PYTHONPATH = 'C:\program files\grass gis 6.4.3\etc\python'
GRASS_SH = 'C:\OSGeo4W64\apps\msys\bin\sh.exe'
sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))
import grass.script as grass

Why does simplejson work in Terminal and not TextMate?

I'm using simplejson to get data from the New York Time API. It works when I run the file through the terminal with the command "python test.py" but not when I run through TextMate using command + R. I'm running the exact same file. Why is this?
I am running Snow Leopard 10.6.4, TextMate 1.5.10, and Python 2.6.4.
Edit: Sorry for forgetting to include this: by "doesn't work," I mean it says "No module named simplejson". I also noticed that this happens for PyMongo as well ("No module named pymongo").
What doesn't work? You should provide more information like error messages and what-not. However, I assume that the version of python is different, and simplejson isn't on your PYTHONPATH when launched from textmate.
Just so you know, simplejson was incorporated into the Python 2.6 distribution's standard library as json. So if you don't feel like wrestling with the import problem, try simply changing all your references to simplejson to json instead.
But, as suggested, this is going to turn out to be a PythonPath issue. Run these lines in the Python interpreter and from TextMate and compare the results.
import sys
print sys.path
To find out where simplejson is installed (if you don't know), do this in the Python interpreter:
import simplejson
print simplejson.__file__
If you want/need to set PYTHONPATH manually for TextMate, you can do that by adding it under Preferences > Advanced > Shell Variables.

Categories