# How to use osmnx python package in julia?

**URL:** <https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541>\
**Category:** General Usage\
**Created:** [February 7, 2019, 11:41am UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541 "2019-02-07T11:41:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [February 7, 2019, 11:41am UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/1 "2019-02-07T11:41:34Z")

</div>

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.

```julia
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.

```julia
ENV["PYTHON"] = " a path to my executable"

```

And then

```julia
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.

So I really need help indeed. Thank you!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2019, 12:25pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/2 "2019-02-07T12:25:14Z")

</div>

> [@bsnyh](#):
>
> Plus, based on the PyCall documentation, the python version used in PyCall is automatically configured to the python3 in the system by default.

No, on Mac and Windows it installs its own Anaconda Python by default.

If you look at `PyCall.python`, it will tell you what `python` executable it was configured with. Does it match your expectations?

More explicitly, if you re-launch Julia and do

```julia
ENV["PYTHON"] = Sys.which("python3")
import Pkg
Pkg.build("PyCall")
using PyCall
PyCall.python

```

is it the python that you want? What happens if you do `pyimport("osmnx")` then?

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [February 7, 2019, 12:42pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/3 "2019-02-07T12:42:15Z")

</div>

OK. Here is what have happened.

```julia
julia> ENV["PYTHON"]=Sys.which("python3")
"/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

julia> import Pkg

julia> Pkg.build("PyCall")
  Building Conda ─→ `~/.julia/packages/Conda/CpuvI/deps/build.log`
  Building PyCall → `~/.julia/packages/PyCall/0jMpb/deps/build.log`

julia> using PyCall

julia> PyCall.python
"/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

```

On the other hand, on Mac terminal,

```julia
$ which python3
"/Library/Frameworks/Python.framework/Versions/3.7/bin/python3"
$ python3
>>> import osmnx
>>> quit()
$ which python3.7
"/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"
$ python3.7
>>> import osmnx
>>>quit()

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2019, 2:16pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/4 "2019-02-07T14:16:23Z")

</div>

And what is the output of `pyimport("osmnx")` after the configuration I suggested?

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [February 7, 2019, 2:27pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/5 "2019-02-07T14:27:10Z")

</div>

```julia
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

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2019, 2:44pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/6 "2019-02-07T14:44:53Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [February 7, 2019, 2:49pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/7 "2019-02-07T14:49:08Z")

</div>

Thank you. You mean editing the `.matplotlibrc` configuration file by adding `ENV["MPLBACKEND"] = "tkagg"`?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2019, 2:51pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/8 "2019-02-07T14:51:27Z")

</div>

> [@bsnyh](#):
>
> You mean editing the `.matplotlibrc` configuration file by adding `ENV["MPLBACKEND"] = "tkagg"` ?

No. `ENV["MPLBACKEND"] = "tkagg"` is Julia syntax for setting an environment variable. You can do this before calling `pyimport`.

In the `.matplotlibrc` file you would [add something like](https://matplotlib.org/users/customizing.html) `backend : TkAgg`.

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [February 7, 2019, 3:10pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/9 "2019-02-07T15:10:36Z")

</div>

Yes. It works. Julia can finally understand osmnx. But I have the other error message.

```julia
julia> @pyimport osmnx
julia> osmnx.graph_from_place('New York, USA') 
ERROR: syntax: invalid character literal

```

---

<div class="post-metadata">

**Author:** ![konkam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/konkam/32/6598_2.png) [@konkam](https://discourse.julialang.org/u/konkam)\
**Post date:** [February 7, 2019, 3:15pm UTC](https://discourse.julialang.org/t/how-to-use-osmnx-python-package-in-julia/20541/10 "2019-02-07T15:15:06Z")

</div>

You probably need double quotes `" "`
