# Control mopidy by python code

**URL:** <https://discourse.mopidy.com/t/control-mopidy-by-python-code/1590>\
**Category:** Misc\
**Created:** [April 18, 2017, 6:01pm UTC](https://discourse.mopidy.com/t/control-mopidy-by-python-code/1590 "2017-04-18T18:01:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patresss](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Patresss](https://discourse.mopidy.com/u/Patresss)\
**Post date:** [April 18, 2017, 6:01pm UTC](https://discourse.mopidy.com/t/control-mopidy-by-python-code/1590/1 "2017-04-18T18:01:07Z")

</div>

Hi, how can I control mopidy by my python code? I would be grateful if someone give me full example with simply commend like stop music, next song.

---

<div class="post-metadata">

**Author:** ![Ecological](https://avatars.discourse-cdn.com/v4/letter/e/c5a1d2/32.png) [@Ecological](https://discourse.mopidy.com/u/Ecological)\
**Post date:** [April 18, 2017, 7:07pm UTC](https://discourse.mopidy.com/t/control-mopidy-by-python-code/1590/2 "2017-04-18T19:07:48Z")

</div>

You will have to use the api. There are a couple ways. One is to send RPC cmds to the websocket. The other is to build a custom frontend.

[https://docs.mopidy.com/en/latest/api/](https://docs.mopidy.com/en/latest/api/)

---

<div class="post-metadata">

**Author:** ![kingosticks](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@kingosticks](https://discourse.mopidy.com/u/kingosticks)\
**Post date:** [April 18, 2017, 7:28pm UTC](https://discourse.mopidy.com/t/control-mopidy-by-python-code/1590/3 "2017-04-18T19:28:44Z")

</div>

The easiest way is using the python mpd library. Their documentation has examples.

---

<div class="post-metadata">

**Author:** ![jcass77](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/jcass77/32/721_2.png) [@jcass77](https://discourse.mopidy.com/u/jcass77)\
**Post date:** [April 20, 2017, 9:36am UTC](https://discourse.mopidy.com/t/control-mopidy-by-python-code/1590/4 "2017-04-20T09:36:25Z")

</div>

Maybe also take a look at [https://github.com/ismailof/mopidy-json-client](https://github.com/ismailof/mopidy-json-client). Not sure how active it is or how far it progressed.

---

<div class="post-metadata">

**Author:** ![ismailof](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/ismailof/32/750_2.png) [@ismailof](https://discourse.mopidy.com/u/ismailof)\
**Post date:** [May 5, 2017, 1:05pm UTC](https://discourse.mopidy.com/t/control-mopidy-by-python-code/1590/5 "2017-05-05T13:05:54Z")

</div>

Hi! As @jcass77 says, you can use `mopidy-json-client` to control mopidy from python (it uses the websocket interface).

To install it run:  
`sudo pip install https://github.com/ismailof/mopidy-json-client/archive/master.zip`

Sorry if there is a big lack of documentation. I’ll hopefully update it soon. A sample program would be something like:

> from mopidy-json-client import MopidyClient

> mopidy = MopidyClient()

> mopidy.playback.play()

Available commands are all exposed by the websocket API ([mopidy.core — Core API — Mopidy 3.4.2 documentation](http://mopidy.readthedocs.io/en/latest/api/core/))
