pyplot error after installing julia - python

Installed julia v0.3.2 into MacOSX 10.9.5
Did
Pkg.add("IJulia")
Pkg.add("PyPlot")
initiate ijulia with:
ipython notebook --profile=julia
This starts an IJulia notebook (it says IJ in the top left.
I enter using pyplot into the first line of iJulia, hit shift enter, and get this:
objc[21233]: Class TKApplication is implemented in both /Library/Frameworks/Tk.framework/Versions/8.5/Tk and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[21233]: Class TKMenu is implemented in both /Library/Frameworks/Tk.framework/Versions/8.5/Tk and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[21233]: Class TKContentView is implemented in both /Library/Frameworks/Tk.framework/Versions/8.5/Tk and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[21233]: Class TKWindow is implemented in both /Library/Frameworks/Tk.framework/Versions/8.5/Tk and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
INFO: Loading help data...
Warning: requiring "pyplot" did not define a corresponding module.
OK, so my mac has the tk package installed in two places. If i remove the TK and TCL frameworks from Library/Frameworks, as suggested here:
http://michaelwelburn.com/2013/06/07/python-error-conflicting-tk-libraries-and-activetcl/
and here:
Python tk framework
then I get the following error when I try to start iJulia:
INFO: Loading help data...
Warning: requiring "pyplot" did not define a corresponding module.
The other authors said this worked so I am confused.
My /usr/local/bin directory includes these) executables:
tclselect tclsh tclsh8.5 tclsh8.6 tclvfse wish wish8.5 wish8.6
I hesitate to remove the system framework. I have the native mac python as well as the anaconda version (which has its own lib/tk) I am at a loss as to the next step.
EDIT: My julia code is this:
using PyPlot
# julia set
# (the familiar mandelbrot set is obtained by setting c==z initially)
function julia(z, c; maxiter=200)
for n = 1:maxiter
if abs2(z) > 4
return n-1
end
z = z*z + c
end
return maxiter
end
# varying the second argument to julia() tiny amounts results in a stunning variety of forms
#time m = [ uint8(julia(complex(r,i), complex(-.06,.67))) for i=1:-.002:-1, r=-1.5:.002:1.5 ];
# the notebook is able to display ColorMaps
get_cmap("RdGy")
imshow(m, cmap="RdGy", extent=[-1.5,1.5,-1,1])
each line executes fine in iJulia except the last line starting with imshow which gives this error:
PyError (PyObject_Call) <class '_tkinter.TclError'>
TclError('Can\'t find a usable tk.tcl in the following directories: \n /System/Library/Frameworks/Tk.framework/Versions/8.5/Resources/Scripts\n\n/System/Library/Frameworks/Tk.framework/Versions/8.5/Resources/Scripts/tk.tcl: version conflict for package "Tk": have 8.5.15, need exactly 8.5.9\nversion conflict for package "Tk": have 8.5.15, need exactly 8.5.9\n while executing\n"package require -exact Tk 8.5.9"\n (file "/System/Library/Frameworks/Tk.framework/Versions/8.5/Resources/Scripts/tk.tcl" line 20)\n invoked from within\n"source /System/Library/Frameworks/Tk.framework/Versions/8.5/Resources/Scripts/tk.tcl"\n ("uplevel" body line 1)\n invoked from within\n"uplevel #0 [list source $file]"\n\n\nThis probably means that tk wasn\'t installed properly.\n',)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2370, in imshow
ax = gca()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 701, in gca
ax = gcf().gca(**kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1764, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
while loading In[7], in expression starting on line 1
in pyerr_check at /Users/userme/.julia/v0.3/PyCall/src/exception.jl:58
in pycall at /Users/userme/.julia/v0.3/PyCall/src/PyCall.jl:85
in imshow at /Users/userme/.julia/v0.3/PyPlot/src/PyPlot.jl:370

I feel so stupid. My problem was the .bash_profile I had these lines
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
leftover from a previous python install. I also had a bunch of executables in my /usr/local/bin folder from this old python install. When I deleted the python 2.7 folder before putting in anaconda, I forgot to delete these.
Now the error is gone. I hope this helps someone.

For the Warning:
Warning: requiring "*" did not define a corresponding module.
In some cases it helps to check if we use a stable version.
Ran into same issues with 0.38+pre-versions.
After switching back to 0.37 stable the issue was gone.
Run into this issue with PyCall:
Warning: requiring "PyCall" did not define a corresponding module.

Related

Python subprocess call won't install R package

I have a Python subprocess to call R:
cmd = ['Rscript', 'Rcode.R', 'file_to_process.txt']
out = subprocess.run(cmd, universal_newlines = True, stdout = subprocess.PIPE)
lines = out.stdout.splitlines() #split stdout
My R code first checks if the 'ape' package is installed before proceeding:
if (!require("ape")) install.packages("ape")
library(ape)
do_R_stuff.......
return_output_to_Python
Previously, the whole process from Python to R worked perfectly - R was called and processed output was returned to Python - until I added the first line (if (!require("ape")) install.packages("ape")). Now Python reports: "there is no package called 'ape'" (i.e. when I uninstall ape in R). I have tried wait instructions in both the R and Python scripts but I can't get it working. When checking, the R code works in isolation.
The full error output from Python is:
Traceback (most recent call last):
File ~\Documents\GitHub\wolPredictor\wolPredictor_MANUAL_parallel.py:347 in <module>
if __name__ == '__main__': main()
File ~\Documents\GitHub\wolPredictor\wolPredictor_MANUAL_parallel.py:127 in main
cophen, purge, pge_incr, z, _ = R_cophen('{}/{}'.format(dat_dir, tree), path2script) #get Dist Mat from phylogeny in R
File ~\Documents\GitHub\wolPredictor\wolPredictor_MANUAL_parallel.py:214 in R_cophen
purge = int(np.max(cophen) * 100) + 1 #max tree pw distance
File <__array_function__ internals>:5 in amax
File ~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py:2754 in amax
return _wrapreduction(a, np.maximum, 'max', axis, None, out,
File ~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py:86 in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation maximum which has no identity
Loading required package: ape
Installing package into 'C:/Users/Windows/Documents/R/win-library/4.1'
(as 'lib' is unspecified)
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called 'ape'
Execution halted
Point 1: The path to R libraries when using R in a standalone mode may not be the same when using Rscript.
Point 2: The error says there was difficulty in finding the CRAN repository, so perhaps the options that set the repos were not set for the Rscript environment. They can be set in the call to install packages or with a Sys.setenv() call.
Ther OP wrote: "Thanks #IRTFM I had to set a new sub process in a new .R file specifically for the install, but the CRAN mirror was key, I never realised it would be an issue as it's not a requirement on my local machine (not sure why it becomes an issue thru subprocess)."
The places to find more information are the ?Startup help page and the ?Rscript page. Rscript has many fewer defaults. Even the usual set of recommended packages may not get loaded by default. The Rscript help page includes these flags which could be used for debugging and setting a proper path to the libraries desired.:
--verbose
gives details of what Rscript is doing.
--default-packages=list
where list is a comma-separated list of package names or NULL. Sets the environment variable R_DEFAULT_PACKAGES which determines the packages loaded on startup.
Here is a previous similar SO question with an answer that includes some options for construction of a proper working environment: Rscript: There is no package called ...?
There are a few R packages on CRAN that aid in overcoming some of the differences between programming for standalone R and Rsript.
getopt: https://cran.r-project.org/web/packages/getopt/index.html
optparse: https://cran.r-project.org/web/packages/optparse/index.html (styled after a similar Python package.)
argparse: https://cran.r-project.org/web/packages/argparse/index.html
I solved the issue (thanks to #IRTFM) by placing the if-then-install.packages code in a separate Rscript (including the CRAN mirror):
if (!require("ape")) install.packages("ape", repos='http://cran.us.r-project.org')
which I then called using a separate Python subprocess in my Python routine

How to execute python function as whole in VSCode (it splits and sends just the first line to an interpreter)

I'm getting used to VSCode in my daily Data Science remote workflow due to LiveShare feature.
So, upon executing functions it just executes the first line of code; if I mark the whole region then it does work, but it's cumbersome way of dealing with the issue.
I tried number of extensions, but none of them seem to solve the problem.
def gini_normalized(test, pred):
"""Simple normalized Gini based on Scikit-Learn's roc_auc_score"""
gini = lambda a, p: 2 * roc_auc_score(a, p) - 1
return gini(test, pred)
Executing the beginning of the function results in error:
def gini_normalized(test, pred):...
File "", line 1
def gini_normalized(test, pred):
^
SyntaxError: unexpected EOF while parsing
There's a solution for PyCharm: Python Smart Execute - https://plugins.jetbrains.com/plugin/11945-python-smart-execute. Also Atom's Hydrogen doesn't have such issue either.
Any ideas regarding VSCode?
Thanks!
I'm a developer on the VSCode DataScience features. Just to make sure that I'm understanding correctly. You would like the shift-enter command to send the entire function to the Interactive Window if you run it on the definition of the function?
If so, then yes, we don't currently support that. Shift-enter can run line by line or run a section of code that you manually highlight. If you want, you can use #%% lines in your code to put functions into code cells. Then when you are in a cell shift-enter will run that entire cell, might be the best current approach for you.
That smart execute does look interesting, if you would like to file that as a suggestion you can use our GitHub here to get it on our backlog to look at.
https://github.com/Microsoft/vscode-python
Hi you could click the symbol before each line and turn it into > (the indented codes of the function was hidden now). Then if you select the whole line and the next line, shift+enter could run them together.
enter image description here

MATLAB-generated Python packages conflict with PyQt5 on Ubuntu - possible library issue

I am building an application using Ubuntu 18.04 and PyQt 5.12.1, which imports Python packages generated from MATLAB code (these packages depend on the MATLAB Runtime). MATLAB packages in Python require the LD_LIBRARY_PATH environment variable to be set; without this, the program raises an exception when a MATLAB-generated package is imported.
However, I have found that PyQt cannot function when LD_LIBRARY_PATH is set. The program runs fine with the MATLAB Runtime installed, as long as the MATLAB package is not imported and the LD_LIBRARY_PATH is not set.
As prompted by the MATLAB Runtime installer, I added this to the environment variables in my PyCharm run/debug configuration:
LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Runtime/v96/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/extern/bin/glnxa64.
This causes a crash in the PyQt part of the program. Using the QT_DEBUG_PLUGINS=1 environment variable, the error message is as follows:
Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "<redacted>/PyMODA/venv/bin/platforms" ...
Cannot load library <redacted>/venv/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/libQt5XcbQpa.so.5: undefined symbol: _ZNK14QPlatformTheme14fileIconPixmapERK9QFileInfoRK6QSizeF6QFlagsINS_10IconOptionEE)
QLibraryPrivate::loadPlugin failed on "<redacted>/venv/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so" : "Cannot load library <redacted>/venv/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/libQt5XcbQpa.so.5: undefined symbol: _ZNK14QPlatformTheme14fileIconPixmapERK9QFileInfoRK6QSizeF6QFlagsINS_10IconOptionEE)"
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
The important part:
"Cannot load library <...>/libqxcb.so: (/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/libQt5XcbQpa.so.5: undefined symbol: _ZNK14QPlatformTheme14fileIconPixmapERK9QFileInfoRK6QSizeF6QFlagsINS_10IconOptionEE)"
The MATLAB Runtime ships libQt5XcbQpa.so.5 in /usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/, which must be exported to the LD_LIBRARY_PATH. It seems that this is being used by PyQt when the LD_LIBRARY_PATH is set, and it is an old version which is incompatible with the current version of PyQt.
Another library with the same name is in /usr/lib/x86_64-linux-gnu/, and it has a different MD5 checksum to the MATLAB version. However, adding this directory to the start of the LD_LIBRARY_PATH does not help. Setting the QT_QPA_PLATFORM_PLUGIN_PATH does not help either.
Is there a way to make the version in /usr/lib/x86_64-linux-gnu/ a higher priority than the MATLAB-supplied library? Is there another way to fix this issue?
I have discovered a workaround:
Run all MATLAB-packaged code in a new process; this is barely an inconvenience, since the computations must be run on a separate thread or process to prevent freezing the GUI anyway.
In each process which runs MATLAB-packaged code, set the LD_LIBRARY_PATH environment variable programmatically before importing the MATLAB modules. The import statements will have to be in a function rather than at the top of the file.
Here is a relatively minimal example:
class MyPlot(PlotComponent):
"""
A class which inherits from a base class PlotComponent, which is
a subclass of QWidget. In this simple example, the window
gets the data and calls the function `plot(self, data)` on an
instance of this class.
"""
def __init__(self, parent):
super().__init__(parent)
self.queue = Queue()
def plot(self, data):
"""Calculate the results from the provided data, and plot them."""
fs = data.frequency
self.times = data.times
signal = data.signal.tolist()
# Create the process, supplying all data in non-MATLAB types.
self.proc = Process(target=generate_solutions, args=(self.queue, signal, fs))
self.proc.start()
# Check for a result in 1 second.
QTimer.singleShot(1000, self.check_result)
def check_result(self):
"""Checks for a result from the other process."""
if self.queue.empty(): # No data yet; check again in 1 second.
QTimer.singleShot(1000, self.check_result)
return
w, l = self.queue.get() # Get the data from the process.
a = np.asarray(w)
gh = np.asarray(l)
# Create the plot.
self.axes.pcolormesh(self.times, gh, np.abs(a))
def generate_solutions(queue, signal, freq):
"""
Generates the solutions from the provided data, using the MATLAB-packaged
code. Must be run in a new process.
"""
import os
# Set the LD_LIBRARY_PATH for this process. The particular value may
# differ, depending on your installation.
os.environ["LD_LIBRARY_PATH"] = "/usr/local/MATLAB/MATLAB_Runtime/v96/runtime/glnxa64:" \
"/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64:" \
"/usr/local/MATLAB/MATLAB_Runtime/v96/extern/bin/glnxa64"
# Import these modules AFTER setting up the environment variables.
import my_matlab_package
import matlab
package = my_matlab_package.initialize()
# Convert the input into MATLAB data-types, to pass to the MATLAB package.
A = matlab.double([signal])
fs_matlab = matlab.double([freq])
# Calculate the result.
w, l = package.perform_my_calculation(A, fs_matlab, nargout=2)
# Convert the results back to normal Python data-types so that the
# main process can use them without importing matlab, and put them
# in the queue.
queue.put((np.asarray(w), np.asarray(l),))

NullFunctionError: Attempt to call an undefined alternate function (glGenFramebuffers, glGenFramebuffersEXT)

Currently I'm working with 3D objects rendering. In that while dealing with framebuffer part I'm getting some error.
self.fbo = glGenFramebuffers(1)
whenever interpreter hits this line its showing following error
**File "C:\Python27\lib\site-packages\OpenGL\latebind.py", line 44, in __call__
self._finalCall = self.finalise()
File "C:\Python27\lib\site-packages\OpenGL\extensions.py", line 189, in finalise
self.__name__,
NullFunctionError: Attempt to call an undefined alternate function (glGenFramebuffers, glGenFramebuffersEXT), check for bool(glGenFramebuffers) before calling**
I'm using python 2.7.3 and pyOpenGL 3.0.2.I couldn't find any answer for this error.
If bool(glGenFramebuffers) returns False, the error probably means that your computer does not have access to OpenGL >= 2.1 so Framebufffer objects won't work. Check your OpenGL supported version with GPU Caps Viewer for Windows. For Linux see here: https://askubuntu.com/questions/47062/what-is-terminal-command-that-can-show-opengl-version
If you have at least 2.1, then maybe the library you are using to create the context (pySDL, glut, pySFML, etc.) is not creating a compatible one. Fixing that depends on the library and probably already has an answer.
If bool(glGenFramebuffers) returns True, the problem might be somewhere else early in the code.
Also, remember that the context must be created and current before trying to create or use shaders, framebuffers, etc.

Matlab's cannot call Python code that imports statsmodels

This question concerns Matlab 2014b, Python 3.4 and Mac OS 10.10.
I have the following Python file tmp.py:
from statsmodels.tsa.arima_process import ArmaProcess
import numpy as np
def generate_AR_time_series():
arparams = np.array([-0.8])
maparams = np.array([])
ar = np.r_[1, -arparams]
ma = np.r_[1, maparams]
arma_process = ArmaProcess(ar, ma)
return arma_process.generate_sample(100)
I want to call generate_AR_time_series from Matlab so I used:
py.tmp.generate_AR_time_series()
which gave a vague error message
Undefined variable "py" or class "py.tmp.generate_AR_time_series".
To look into the problem further, I tried
tmp = py.eval('__import__(''tmp'')', struct);
which gave me a detailed but still obscured error message:
Python Error:
dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so, 2): Symbol
not found: __gfortran_stop_numeric_f08
Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so
Expected in: /Applications/MATLAB_R2014b.app/sys/os/maci64/libgfortran.3.dylib
in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so
I can call the function within Python just fine, so I guess the problem is with Matlab. From the detailed message, it seems that the problem lies in something is expected in the Matlab installation path, but of course Matlab installation path does not contain those things since they are third-party libraries for Python.
How to solve this problem?
Edit 1:
libgfortran.3.dylib can be found in a lot of places:
/Applications/MATLAB_R2014a.app/sys/os/maci64/libgfortran.3.dylib
/Applications/MATLAB_R2014b.app/sys/os/maci64/libgfortran.3.dylib
/opt/local/lib/gcc48/libgfortran.3.dylib
/opt/local/lib/gcc49/libgfortran.3.dylib
/opt/local/lib/libgcc/libgfortran.3.dylib
/Users/wdg/Documents/MATLAB/mcode/nativelibs/macosx/bin/libgfortran.3.dylib
Try:
setenv('DYLD_LIBRARY_PATH', '/usr/local/bin/');
For me, using the setenv approach from within MATLAB did not work. Also, MATLAB modifies the DYLD_LIBRARY_PATH variable during startup to include necessary libraries.
First, you have to make sure which version of gfortran scipy was linked against: in Terminal.app, enter otool -L /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so and look for 'libgfortran' in the output.
It worked for me to copy $(MATLABROOT)/bin/.matlab7rc.sh to my home directory and change the line LDPATH_PREFIX='' in the mac section (around line 195 in my case) to LDPATH_PREFIX='/opt/local/lib/gcc49', or whatever path to libgfortran you found above.
This ensures that /opt/local/lib/gcc49/libgfortran.3.dylib is found before the MATLAB version, but leaves other paths intact.

Categories