Hi, I installed osmnx using pip3 install ***** , and I tested it out with a simple python code. It works out as expected. Then, I’d like to be able to import osmnx. After searching from the internet, I need to use the PyCall package. So I tried the following.
using PyCall
@pyimport(osmnx)
I got an error message, saying that the module osmnx is not installed/found in the current python version the PyCall is configured to yet. So I go to the mac terminal, run which python, and it gives an executable path of the current python. Then, I followed what the PyCall documentation mentioned, by changing/setting the environment as the following.
ENV["PYTHON"] = " a path to my executable"
And then
Pkg.build("PyCall")
This time, the python in PyCall is configured to the correct python3, but still it does not work. Plus, based on the PyCall documentation, the python version used in PyCall is automatically configured to the python3 in the system by default.
julia> pyimport("osmnx")
ERROR: PyError (PyImport_ImportModule
The Python package osmnx could not be found by pyimport. Usually this means
that you did not install osmnx in the Python version being used by PyCall.
PyCall is currently configured to use the Python version at:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the osmnx module.
One alternative is to re-configure PyCall to use a different Python
version on your system: set ENV["PYTHON"] to the path/name of the python
executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.
Another alternative is to configure PyCall to use a Julia-specific Python
distribution via the Conda.jl package (which installs a private Anaconda
Python distribution), which has the advantage that packages can be installed
and kept up-to-date via Julia. As explained in the PyCall documentation,
set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then,
To install the osmnx module, you can use `pyimport_conda("osmnx", PKG)`,
where PKG is the Anaconda package the contains the module osmnx,
or alternatively you can use the Conda package directly (via
`using Conda` followed by `Conda.add` etcetera).
) <class 'ImportError'>
ImportError("Python is not installed as a framework. The Mac OS X backend will not beable to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/osmnx/__init__.py", line 9, in <module>
from .buildings import *
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/osmnx/buildings.py", line 9, in <module>
import matplotlib.pyplot as plt
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2374, in <module>
switch_backend(rcParams["backend"])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
backend_mod = importlib.import_module(backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/backends/backend_macosx.py", line 14, in <module>
from matplotlib.backends import _macosx
Stacktrace:
[1] pyimport(::String) at /Users/nanal/.julia/packages/PyCall/0jMpb/src/PyCall.jl:486
[2] top-level scope at none:0
It is finding the osmnx module, it is just not succeeding in loading it. The relevant portion of the error message is:
Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X.
Basically, it is trying to load a matplotlib backend that only works with the python executable or with other programs linked in a certain way, and not with programs like Julia that dynamically link libpython.
The solution is to use a different matplotlib backend. You can set the backend by the MPLBACKEND environment variable, e.g. set ENV["MPLBACKEND"]="tkagg" (or you could try "qt5agg"). You can set this permanently by editing your .matplotlibrc configuration file.
If you use Matplotlib via the PyPlot.jl package, then it automatically tries to use a supported backend. However, when you import a module like osmnx that loads matplotlib itself, it ends up using the default matplotlib backend.
(A moral of this story is to always post the complete error message when you ask for help.)