Get programmatically Python3 default path in Octave base directory - octave

I am using GNU Octave. I have script written in python3 and it is working if I do something like that: (it's .m file)
setenv PYTHON python;
[output,status]=python('python_file.py');
output
But some users do not have installed Python on their own. I found that python3.exe is available in "C:\Program Files\GNU Octave\Octave-6.2.0\usr\bin" for default users.
It is possible to get Octave default path and then setenv python pointing to GNU Octave Python directory programically, everything wrote in octave_file.m?
Python file:
import sys
import subprocess
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', 'numpy'], stdout=subprocess.DEVNULL)
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
...
result from octave:
/usr/bin/python.exe: No module named pip
Traceback (most recent call last):
File "C:\Users\X\Desktop\cw1.py", line 9, in <module>
subprocess.check_call([python, '-m', 'pip', 'install', 'numpy'], stdout=subprocess.DEVNULL)
File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/python.exe', '-m', 'pip', 'install', 'numpy']' returned non-zero exit status 1.

Related

No module named json in python3

I want to use json parser in my python script.
When I import json module there is an ImportError:
root#msc-nr-imx6x:~# python3
Python 3.5.2 (default, Aug 9 2017, 22:59:34)
[GCC 6.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'json'
>>>
I supposed that in python3 json is built-in.
Python is running on - arm imx6, yocto 2.2, python 3.5.2
So how can I install json module?
Thank you
Edit - add output of test.py script:
try:
import json
except ImportError:
import simplejson as json
output:
root#msc-nr-imx6x:~# python3 test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
import json
ImportError: No module named 'json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 4, in <module>
import simplejson as json
ImportError: No module named 'simplejson'
Yocto creates embedded systems: this is why python does not automatically come with all of the modules it usually does. Whoever created the image you're using had the option of including all standard modules but did not do that.
If you are building your own image you can include more python packages in your image install, either python3-json for just what you need or something like python3-modules to get all the common ones: see meta/recipes-devtools/python/python3/python3-manifest.json for more details on the split.
EDIT: Actually, you are using an old Yocto version so probably want to look at meta/recipes-devtools/python/python-3.5-manifest.inc for the details
In your case you can try this code snippet:
try:
import json
except ImportError:
import simplejson as json
you can visit python json module import error

how to fix nltk stanford import error in python

I am trying to import stanford ner but getting error:
NLTK was unable to find the
C:/Users/.../stanford-ner-2018-10-16/classifiers/all.7class.distsim.crf.ser
file! Use software specific configuration paramaters or set the
STANFORD_MODELS environment variable.
Below the code:
from nltk.tag import StanfordNERTagger
import os
java_path = "C:/Program Files/Java/jre1.8.0_201/bin/java.exe"
os.environ['JAVAHOME'] = "JAVA_PATH" #this java path you get from command 'echo %PATH%'in terminal
st = StanfordNERTagger('C:/Users/.../stanford-ner-2018-10-16/classifiers/all.7class.distsim.crf.ser',
'C:/Users/.../stanford-ner-2018-10-16/stanford-ner.jar',encoding='utf-8')

Reraising an exception in Cython on Python 2 and Python3

I have some Cython code that currently looks
exc = sys.exc_info()
raise exc[0], exc[1], exc[2]
This doesn't work on Python3, since the "raise from tuple" form is no longer allowed. Were this normal Python code, I would just use six.reraise, but that's not available to me here. What's the Cython friendly way to do the same, which works on both Python2 and Python3?
One great Cython feature is that the generated C code can be compiled for either Python 2 or Python 3. So your example above will work with either version of Python, unmodified.
You can tell Cython to compile code assuming Python 2 syntax and semantics (the -2 argument, which is on by default) or assuming Python 3 (the -3 argument). In either case, the resulting extension module source code can be compiled and used for Python 2 or Python 3, as long as the dynamic components (imports, etc.) are compatible.
For example:
def raises_exception():
raise KeyError("what you doin'?")
def foobar():
try:
raises_exception()
except Exception:
import sys
exc = sys.exc_info()
raise exc[0], exc[1], exc[2]
Here's a setup.py that will work on either Py2 or Py3:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("reraise.pyx"))
I can run python setup.py build_ext -i on either version of Python (provided I have cython installed for each), and the resulting extension module will work.
$ python setup.py build_ext -i # Py3 python
$ ipython3
Python 3.3.2 (v3.3.2:d047928ae3f6, Oct 4 2013, 15:49:17)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import reraise
In [2]: reraise.foobar()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-2-9e20eacfd84e> in <module>()
----> 1 reraise.foobar()
/.../reraise.so in reraise.foobar (reraise.c:916)()
/.../reraise.so in reraise.foobar (reraise.c:847)()
/.../reraise.so in reraise.raises_exception (reraise.c:762)()
KeyError: "what you doin'?"
In [3]:

ImportError: No module named exceptions

i'm trying to install kitsune in the ubuntu terminal. I am not able to initialize the database. When i give the command ./manage.py syncdb --migrate , i get the following error
Error: Tried importing 'settings_local.py' and 'settings.py' but neither could be found (or they're throwing an ImportError). Please come back and try again later.Traceback (most recent call last):
File "./manage.py", line 30, in <module>
from kitsune import settings # Assumed to be in the same directory.
File "/home/user/kitsune/kitsune/settings.py", line 740, in <module>
import djcelery
File "/usr/local/lib/python2.7/dist-packages/djcelery/__init__.py", line 22, in <module>
from celery import current_app as celery # noqa
File "/usr/local/lib/python2.7/dist-packages/celery/five.py", line 307, in __getattr__
module = __import__(self._object_origins[name], None, None, [name])
File "/usr/local/lib/python2.7/dist-packages/celery/_state.py", line 20, in <module>
from celery.utils.threads import LocalStack
File "/usr/local/lib/python2.7/dist-packages/celery/utils/__init__.py", line 24, in
<module>
from celery.exceptions import CPendingDeprecationWarning, CDeprecationWarning
File "/usr/local/lib/python2.7/dist-packages/celery/exceptions.py", line 13, in <module>
from billiard.exceptions import ( # noqa
ImportError: No module named exceptions
I can't find the exceptions file in billiard. How do I fix this error?
I had this error with fabric. The error message was the same:
ImportError: No module named exceptions
It turned out to be my fabric was out of date. The error was resolved by upgrading fabric to the latest version (1.10) with:
$ sudo pip install fabric --upgrade
For your information, I am running Ubuntu 12.04 64-bit with Python 2.7.3
Exceptions should be a python file in a directory called billiard, for example, you create a file called exceptions.py and add the classes and methods that you need .. this can work around the problem.
You should follow kitsune installation process described in http://kitsune.readthedocs.org/en/latest/hacking_howto.html
There is a link to separate chapter on vendor-packages http://kitsune.readthedocs.org/en/latest/vendor.html#vendor-chapter. You should install all the vendor packages needed by kitsune including billiard and celery (your errors).

I get an error message when I try FreqDist() in NLTK -- NameError: name 'nltk' is not defined

I'm learning about the NLTK and my mac
is working fine except I have trouble with the FreqDist(). (I saw another question about FreqDist() but he was getting a different error message. TypeError: unhashable type: 'list')
Here's an example:
>>> from nltk.corpus import brown
>>> news_text = brown.words(categories='news')
>>> fdist = nltk.FreqDist([w.lower() for w in news_text])
Traceback (most recent call last):
` File "<stdin>", line 1, in <module>`
`NameError: name 'nltk' is not defined`
This error message is pretty consistent. I get this message every time I try the FreqDist(). Other commands like - >>> brown.fileids() are fine.
Thanks for your help!
Before you can use FreqDist, you need to import it.
Add a line as follows:
import nltk
or if you just want to use FreqDist you should try this:
>>> from nltk.corpus import brown
>>> from nltk import FreqDist
>>> news_text = brown.words(categories='news')
>>> fdist = FreqDist([w.lower() for w in news_text])
which means you haven't installed nltk.
follow these steps to install nltk:
1:go to this link https://pypi.python.org/pypi/setuptools at the end of page you find setuptools-7.0.zip (md5) download it, then unzip it. you can find easy_install.py python script.
2:use the command sudo easy_install pip. By this time pip will be installed ready to use, (make sure you are in the directory where you can find easy_install script file).
3:use this command sudo pip install -U nltk. successful execution ensure that nltk is now installed.
4:open the IDLE then you type the following:
import nltk
if nltk is installed properly then you will be returned with console.
setuptools are required for older versions of Python. There is no need for the same if you are running 3.2+
You can easily download the same from https://pypi.python.org/pypi/nltk
For more information on http://www.nltk.org/install.html
nltk requires data you need to download first.
Then run the following code:
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
stopwords.words("english")