Error installing and use PyPlot in windows 10 x 64 in julia 0.6.1

Hello … this post is new, any posts have a solution for this problem …

how to fix PyPlot in windows 10 x64 using julia 0.6.1 ?? any topics that i look have a simple solution
Anyone here uses windows ?

when i pkg add pyplot, this is the error

and i cannot use this … pkg.build pycall does not solve and many more tries…

who know a specific action to fix it ?

I would be useful to see the transcript of what you have tried and how it failed.

Also, please copy-paste terminal output between backticks (```), instead of a screenshot.

1 Like

Please list your Python installations along with the results of this command in Julia.

ENV["PYTHON"]

ERROR: KeyError: key “PYTHON” not found
Stacktrace:
[1] access_env(::Base.##288#289, ::String) at .\env.jl:14
[2] getindex(::Base.EnvHash, ::String) at .\env.jl:78

I TRIED somany things…

installed miniconda external, other command lines…

tell me what to do and i will do it

env python does not work

if i try to plot using pyplot, appears a message of runtime error 6041 and does now show any plot

can i do this ? Persistent environment variables on Windows « Python recipes « ActiveState Code

if i use this, in the figure, i can have only one plot, not more … can you help ?

erro2

Have you tested that the version of Python you want to use can import matplotlib?

Have you added the PYTHON environmental variable to Windows with the path of that Python? Do it directly through Windows.

If you read the error message, it says that your tt and s vectors differ in length: length(tt) is 188.

sorry … i have two computers using windows …

in one, i can fix it … but in another, i cant … look

julia> plot(1:10)
ERROR: ArgumentError: haskey of NULL PyObject
Stacktrace:
 [1] haskey(::PyCall.PyObject, ::String) at C:\Users\Lucas\.julia\v0.6\PyCall\src\PyCall.jl:268
 [2] #plot#85(::Array{Any,1}, ::Function, ::UnitRange{Int64}, ::Vararg{UnitRange{Int64},N} where N) at C:\Users\Lucas\.julia\v0.6\PyPlot\src\PyPlot.jl:169
 [3] plot(::UnitRange{Int64}, ::Vararg{UnitRange{Int64},N} where N) at C:\Users\Lucas\.julia\v0.6\PyPlot\src\PyPlot.jl:169

what are this error ?

What is the sequence of commands you type starting from a new Julia session?

i use atom to do my program …

but when i start julia prompt, i use

using PyPlot

plot(1:10)

Note that ENV["PYTHON"] only affects building the PyCall package, i.e. when you run Pkg.build("PyCall") manually, when PyCall is first added, or when PyCall is upgraded. It doesn’t affect using PyCall.

If you run using PyCall; PyCall.python, you will see what python it was configured with.

To force PyCall (and hence PyPlot) to use its own Conda installation, which is generally the most reliable thing, especially if you don’t know much about installing stuff, do

ENV["PYTHON"]=""
Pkg.build("PyCall")

(You only need to do this once and it will remember what Python you selected on future upgrades.)

1 Like

Was my post unclear on that point? It’s good to know that is can be checked after building instead of assuming it’s whatever is in ENV["PYTHON"].

In my experience simply adding PyPlot doesn’t always work in Windows and it somehow often comes down to which python is being used or the environmental variable. Explicitly testing the version of python being pointed to is quite helpful.
I really should write down the problem and solutions so I could later submit them as a bug reports but I never think about it at the time.

Your post was unclear because you said that the PYTHON variable “must exist”, which is false, that its value indicates the python that PyPlot is currently using, which is false, and setting it in Julia may not work and that you may have to set it globally on your system, which is false.

ENV["PYTHON"] does not need to be set at all. If you do set it, you only need to set it during the Julia session where you run Pkg.build("PyCall"). If you set it to be "" then Pkg.build will configure PyCall to install/use its own python distro via Conda.jl. It has no effect otherwise.

I stand corrected. Rather than leave it open for future confusion I’ve flagged the post for deletion.

How does it work with updates to PyCall or PyPlot where they are precompiled again? Does the ENV["PYTHON"] set in Julia still apply to future precompilations? What is the precedence between ENV["PYTHON"] in Julia and the environmental variables set in Windows?

Your clarification is appreciated.

The next time you build, if ENV["PYTHON"] is not set, it remembers which Python it used in the previous build. If an ENV["PYTHON"] is set, that takes precedence.

If you set ENV["PYTHON"] ="foo" in a Julia session, it overwrites any value inherited from the surrounding environment (e.g. the system setting) during the currrent session (but it doesn’t change the system setting) — this is how environment variables work.

julia> ENV["PYTHON"] ="foo"
"foo"

julia> Pkg.build("PyCall")
INFO: Building Conda
INFO: Building PyCall
INFO: No system-wide Python was found; got the following error:
could not spawn setenv(`foo -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('VERSION'))"`,String["USERDOMAIN_ROAMINGPROFILE=LAPTOP-TRHVI6R5", "HOMEPATH=\\Users\\Lucas", "ProgramData=C:\\ProgramData", "ProgramW6432=C:\\Program Files", "PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW", "SESSIONNAME=Console", "APPDATA=C:\\Users\\Lucas\\AppData\\Roaming", "PUBLIC=C:\\Users\\Public", "USERDOMAIN=LAPTOP-TRHVI6R5", "OS=Windows_NT", "PROCESSOR_REVISION=9e09", "TMP=C:\\Users\\Lucas\\AppData\\Local\\Temp", "ALLUSERSPROFILE=C:\\ProgramData", "Path=C:\\Users\\Lucas\\AppData\\Local\\Julia-0.6.0\\bin;C:\\Users\\Lucas\\AppData\\Local\\Julia-0.6.0\\bin;C:\\Python35\\;C:\\Python35\\Scripts\\;C:\\python36\\Scripts\\;C:\\python36\\;C:\\Program Files\\Microsoft MPI\\Bin\\;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Program Files\\MATLAB\\R2017a\\runtime\\win64;C:\\Program Files\\MATLAB\\R2017a\\bin;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files\\dotnet\\;C:\\Program Files\\Git\\cmd;C:\\Users\\Lucas\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Lucas\\Downloads\\g95\\bin;C:\\Users\\Lucas\\AppData\\Local\\atom\\bin", "COMPUTERNAME=LAPTOP-TRHVI6R5", "MSMPI_BIN=C:\\Program Files\\Microsoft MPI\\Bin\\", "USERNAME=Lucas", "CommonProgramFiles(x86)=C:\\Program Files (x86)\\Common Files", "CommonProgramFiles=C:\\Program Files\\Common Files", "G95_LIBRARY_PATH=C:\\Users\\Lucas\\Downloads\\g95\\lib\\gcc-lib\\i686-pc-mingw32\\4.1.2;C:\\Users\\Lucas\\Downloads\\g95\\lib", "USERPROFILE=C:\\Users\\Lucas", "PSModulePath=C:\\Program Files\\WindowsPowerShell\\Modules;C:\\windows\\system32\\WindowsPowerShell\\v1.0\\Modules", "PROCESSOR_LEVEL=6", "PhytonPath=C:\\Program Files\\Anaconda3\\", "=C:=C:\\Users\\Lucas\\.julia\\v0.6\\PyCall\\deps", "TEMP=C:\\Users\\Lucas\\AppData\\Local\\Temp", "SystemDrive=C:", "HOMEDRIVE=C:", "PYTHONIOENCODING=UTF-8", "PYTHON=foo", "LOCALAPPDATA=C:\\Users\\Lucas\\AppData\\Local", "PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 158 Stepping 9, GenuineIntel", "NUMBER_OF_PROCESSORS=8", "=::=::\\", "ComSpec=C:\\windows\\system32\\cmd.exe", "VS140COMNTOOLS=C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\", "SystemRoot=C:\\windows", "OneDrive=C:\\Users\\Lucas\\OneDrive", "MOZ_PLUGIN_PATH=C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\plugins\\", "ProgramFiles(x86)=C:\\Program Files (x86)", "LOGONSERVER=\\\\LAPTOP-TRHVI6R5", "windir=C:\\windows", "FPS_BROWSER_USER_PROFILE_STRING=Default", "CommonProgramW6432=C:\\Program Files\\Common Files", "ProgramFiles=C:\\Program Files", "FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer", "PROCESSOR_ARCHITECTURE=AMD64", "OPENBLAS_MAIN_FREE=1"]): no such file or directory (ENOENT)
using the Python distribution in the Conda package
INFO: PyCall is using C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\python.exe (Python 2.7.13) at C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\python.exe, libpython = C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\python27
INFO: C:\Users\Lucas\.julia\v0.6\PyCall\deps\deps.jl has been updated
INFO: C:\Users\Lucas\.julia\v0.6\PyCall\deps\PYTHON has been updated

julia> using PyPlot
INFO: Recompiling stale cache file C:\Users\Lucas\.julia\lib\v0.6\PyCall.ji for module PyCall.
INFO: Recompiling stale cache file C:\Users\Lucas\.julia\lib\v0.6\PyPlot.ji for module PyPlot.
sys:1: UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to u'Qt5Agg' by the following code:
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\__init__.py", line 14, in <module>
    line for line in traceback.format_stack()


ERROR: InitError: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)

The Python package matplotlib.pyplot could not be found by pyimport. Usually this means
that you did not install matplotlib.pyplot in the Python version being used by PyCall.

PyCall is currently configured to use the Julia-specific Python distribution
installed by the Conda.jl package.  To install the matplotlib.pyplot module, you can
use `pyimport_conda("matplotlib.pyplot", PKG)`, where PKG is the Anaconda
package the contains the module matplotlib.pyplot, or alternatively you can use the
Conda package directly (via `using Conda` followed by `Conda.add` etcetera).

Alternatively, if you want to use a different Python distribution on your
system, such as a system-wide Python (as opposed to the Julia-specific Python),
you can re-configure PyCall with that Python.   As explained in the PyCall
documentation, set ENV["PYTHON"] to the path/name of the python executable
you want to use, run Pkg.build("PyCall"), and re-launch Julia.

) <type 'exceptions.ImportError'>
ImportError(u'Matplotlib qt-based backends require an external PyQt4, PyQt5,\nPySide or PySide2 package to be installed, but it was not found.',)
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\pyplot.py", line 113, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\__init__.py", line 60, in pylab_setup
    [backend_name], 0)
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 16, in <module>
    from .backend_qt5 import (
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\backend_qt5.py", line 18, in <module>
    import matplotlib.backends.qt_editor.figureoptions as figureoptions
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\qt_editor\figureoptions.py", line 20, in <module>
    import matplotlib.backends.qt_editor.formlayout as formlayout
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\qt_editor\formlayout.py", line 56, in <module>
    from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
  File "C:\Users\Lucas\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\qt_compat.py", line 201, in <module>
    "Matplotlib qt-based backends require an external PyQt4, PyQt5,\n"

Stacktrace:
 [1] pyerr_check at C:\Users\Lucas\.julia\v0.6\PyCall\src\exception.jl:56 [inlined]
 [2] pyerr_check at C:\Users\Lucas\.julia\v0.6\PyCall\src\exception.jl:61 [inlined]
 [3] macro expansion at C:\Users\Lucas\.julia\v0.6\PyCall\src\exception.jl:81 [inlined]
 [4] pyimport(::String) at C:\Users\Lucas\.julia\v0.6\PyCall\src\PyCall.jl:374
 [5] __init__() at C:\Users\Lucas\.julia\v0.6\PyPlot\src\init.jl:189
 [6] _include_from_serialized(::String) at .\loading.jl:157
 [7] _require_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at .\loading.jl:200
 [8] _require(::Symbol) at .\loading.jl:457
 [9] require(::Symbol) at .\loading.jl:398
during initialization of module PyPlot

julia> plot(1:10)
ERROR: UndefVarError: plot not defined

julia> using PyPlot

julia> plot(1:10)
ERROR: ArgumentError: haskey of NULL PyObject
Stacktrace:
 [1] haskey(::PyCall.PyObject, ::String) at C:\Users\Lucas\.julia\v0.6\PyCall\src\PyCall.jl:268
 [2] #plot#85(::Array{Any,1}, ::Function, ::UnitRange{Int64}, ::Vararg{UnitRange{Int64},N} where N) at C:\Users\Lucas\.julia\v0.6\PyPlot\src\PyPlot.jl:169
 [3] plot(::UnitRange{Int64}, ::Vararg{UnitRange{Int64},N} where N) at C:\Users\Lucas\.julia\v0.6\PyPlot\src\PyPlot.jl:169

julia> ENV["PYTHON"]=""
""

julia> using PyPlot

julia> plot(1:10)
ERROR: ArgumentError: haskey of NULL PyObject
Stacktrace:
 [1] haskey(::PyCall.PyObject, ::String) at C:\Users\Lucas\.julia\v0.6\PyCall\src\PyCall.jl:268
 [2] #plot#85(::Array{Any,1}, ::Function, ::UnitRange{Int64}, ::Vararg{UnitRange{Int64},N} where N) at C:\Users\Lucas\.julia\v0.6\PyPlot\src\PyPlot.jl:169
 [3] plot(::UnitRange{Int64}, ::Vararg{UnitRange{Int64},N} where N) at C:\Users\Lucas\.julia\v0.6\PyPlot\src\PyPlot.jl:169

julia>

look my answer

I’m guessing you have a matplotlibrc configuration file (leftover from some previous installation attempt) that is causing it to default to the qt5agg backend, which is currently not working on Windows IIRC.

Try

ENV["MPLBACKEND"]="tkagg"
using PyPlot

to switch backends to Tk. If this works, fix your .matplotlibrc file to change it permanently.