How to import Python files in your Pepper apps?

by Sebastien
(Cagnon)

Use the cross to import files or folders

Use the cross to import files or folders

When using Choregraphe to create your Pepper apps, you can put Python code in the boxes.


However, if the code gets longer, boxes become hard to edit. They are also hard to version with your source versioning system like Git or SVN.

Plus, if you find a library online you can use in your app, it's easier to just download the library folder/files than copying them in boxes.

So how do you import Python files in your Pepper apps using Choregraphe?

You can check the video below if you feel you don't want to read:



To explain what happens:
1- When you play or install an application on your robot, the folder shown in project content is uploaded on the robot. So that's where your Python file will be.

2- When your behavior/activity is playing, you can get the behavior's directory by using ALFrameManager.getBehaviorPath.

3- now you should find the relative path between the Behavior path and your python file. In the video example, it was simply '..'.

4- we add that folder to the Python path when we start, and erase it when we quit.

The code for adding and removing the folder to your Python path is as below. The "File Name" refers to the parameter of the box, which in the video is '/..'.

class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)

def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
self.folderName = None


def onInput_onStart(self):
self.folderName = self.framemanager.getBehaviorPath(
self.behaviorId)
+ self.getParameter("File name")
import sys
if self.folderName not in sys.path:
sys.path.insert(0, self.folderName)
self.onStopped()

def onUnload(self):
import sys
if self.folderName and self.folderName in sys.path:
sys.path.remove(self.folderName)


As long as this box is started and not unloaded, you can import your module from anywhere.

A few things to be careful about:
1- don't ever import stuff outside of the class definition. In Naoqi 2.X There is only 1 Python session shared by all the apps on the robot. When an app is started, it is loaded into that 1 Python session. So whatever you import there is imported for everyone, everywhere.

2- when developing and editing your Python library, don't forget to use reload(myModule), otherwise you might just reuse an older version of your module (same as 1: the module was already imported by previous time you started the app).

3- if you create your app entirely in a Python file, don't forget to create a link to exit the app... Simple but otherwise your app will run forever.

Finally, if this tutorial helped you, please share it around using the Facebook or Twitter buttons below.

Comments for How to import Python files in your Pepper apps?

Average Rating starstarstarstarstar

Click here to add your own comments

May 23, 2016
Rating
starstarstarstarstar
savecart NEW
by: Anonymous

nice blog http://www.savecart.net/store/air-vistara-coupons-codes

Nov 03, 2015
Rating
starstarstarstarstar
Nice NEW
by: Anonymous

I would like to share this post to my friends.
HostGator Offers

Oct 25, 2015
Rating
starstarstarstarstar
how to generate free roblox robux NEW
by: best place to get free roblox on robux

I was really very excited after getting the best place to get free roblox on robux which will allow you also in creating your own virtual world in roblox and enjoying it.

Oct 16, 2015
Rating
starstarstarstarstar
Inspired NEW
by: Simon McCallister

I was trying this but could not imply fully. Thanks to you for sharing. Now I can do it easily . Keep posting more like this. sposamoda

Oct 11, 2015
Rating
starstarstarstarstar
amz NEW
by: amz

These days bloggers publish just about chat and web stuff which is actually irritating. free amazon gift card


Oct 01, 2015
Rating
starstarstarstarstar
Really this is very nice NEW
by: Anonymous

Hi,thanks for share it.
bing ads coupon

Sep 14, 2015
Rating
starstarstarstarstar
thnx NEW
by: Garrett M. Hasson

Much obliged, the paper is undoubtedly wholesome! The penman is a professional in this subject. As opposed to the investigations I browsed on the matter, the one provides sophisticated viewpoints. This web-site time after time delivers a ton of entertaining papers on the red hot topics. My mother and I surf them on a day-to-day basis.

harvardsummerprograms.com

Sep 13, 2015
Rating
starstarstarstarstar
Nice Post NEW
by: Abhishek

That was an really impressive post and i like that.
HostGator Deals

Aug 27, 2015
Rating
starstarstarstarstar
dragon city hack NEW
by: RamLaal

This webpage help in play dragon city game. this webpage unlock all the resources of dragon city like cheat dragon city easily.

Aug 18, 2015
Rating
starstarstarstarstar
Hi! NEW
by: Pearly Hakes

If you need some help with your essay writing. You can order papers on our website;)

Aug 07, 2015
Rating
starstarstarstarstar
Like it NEW
by: Lyla Cabana

Whereas your essay for college does not comply with the original parameters, collegers are permitted to ask for money back.

Jun 13, 2015
Rating
starstarstarstarstar
Good post NEW
by: Shannon Hegmann

Hi! Thanks for your great articles!
May I share one of your articles on my site about halloween costumes?

After publishing I will link for you as an author! Thanks!

Jun 11, 2015
Rating
starstarstarstarstar
Good post NEW
by: Trace Schultz

I see that you are a professional in this niche, so, may I ask you do a favor for me?
Could you help me place some ads of my Costummer on your site?
It will be great if I could suggest some guest posts or banner ads, if it's possible - answer me here, please!
p.s. I will pay for your help good money, I really love your site. Thank you!

Mar 10, 2015
Rating
starstarstarstarstar
Java & Pepper/Nao NEW
by: Sebastien (aboutrobot)

Hello P

For Java, there is an SDK that you can download (but I think you need to own a robot before that...).

Python & C++ can be used directly on the robot (as embedded programs or applications).
However, there is no Java Runtime Machine in the robots, so you can only run Java programs from your PC. That means you create a Java program on your PC that will make Remote Procedure Calls to the robot's Naoqi APIs over the network (Wifi).

Dec 08, 2014
Rating
starstarstarstarstar
Java Programming NEW
by: Anonymous

Excellent tutorial. I need to learn python. My question is can I use Java language?

Thank you.
P.

Click here to add your own comments

Join in and write your own page! It's easy to do. How? Simply click here to return to Pepper Robot Programming Tutorials.

Search The site