0

I have created a simple-medium complexity script written in Python using Pycharm on my laptop and I wish to run this on my Raspberry Pi using Crontab. All of my past programming experience has been with C++ and C# in Windows and so I would normally just do a build of the project and it all gets compiled and linked into a single executable file.

For Python how do you "compile" the script so that it can be run on another PC? I use some external libraries (requests and ImgurClient) which I had to install using the Pycharm app. I guess I'm correct in thinking that these need to be taken across to the RaspPi too? My script is in two files and so do I need to copy both of these files across? Is there a way to build them into a single file to use easily?

This is my first script which I've written just from my knowledge of other languages and a bit of Googling. Just don't know how to proceed now that I have the actual script.

Tom Dippé
  • 47
  • 2
  • 10
  • 1
    No need to compile, all you need is a compatible version of Python on the target machine. After that it is as simple as copying the .py file over and running it. – Matt Olan Sep 14 '16 at 21:06
  • This may help you http://stackoverflow.com/questions/11409416/moving-a-python-script-to-another-computer – jh44tx Sep 14 '16 at 21:06
  • It's possible to package interpreter, script & deps into a single standalone executable even on Linux, but **you really don't want to**. This will make your program much, much slower to start up than it would have been if you'd just done things the conventional way -- that is, by copying the Python source (perhaps packaged as a Python egg or similar), installing an interpreter and any necessary library dependencies, and then using that interpreter to run the file. [The startup overhead might be tolerable on some larger machines, but on a Pi it'll be much, much, **much** too slow]. – Charles Duffy Sep 14 '16 at 21:13
  • ...that's because before that single executable will actually run, it needs to *unpack* the interpreter and everything else into separate files (typically in a temporary directory), so you're gaining no runtime speed advantage (code still being interpreted!), but eating a big runtime speed disadvantage (need to unpack everything at startup!) – Charles Duffy Sep 14 '16 at 21:17

6 Answers6

2

If you have Python installed on your Raspberry Pi, then from the shell, you just need to run:

# This installs pip (Python installer) as well as the requests library
sudo apt-get install python-pip

Once that is installed, run:

# To install the ImgurClient
pip install imgurpython

Then you can just run your script at the shell by typing:

python your_script_name.py

If you do not already have Python installed, just run the following command to install it before the others:

sudo apt-get install python
ode2k
  • 2,653
  • 13
  • 20
1

You can "compile" python files to .pyc but you would still need the python interpreter on the RaspPi to run them.

On a PC that does not have Python, you can create a standalone executable using py2exe, but the executable must then run on Windows.

You have to install a python interpreter on your raspberry Pi, or create an executable with py2exe which targets raspberry Pi (if py2exe exists on that platform), that would be on another raspberry Pi :)

Another alternative would be Cython, but with external libraries as complex as the ones you want to use that would be a really difficult route.

Python on Raspberry Pi

Creating python exes on Linux

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

Python does not need to compile as it is an interpretive language. You will be fine running it on another machine as long as you aren't making system calls. System calls will only become a problem if you are trying to do things like use windows commands on a linux machine.

Just copy those two files over to the pi and run like normal. Or from the terminal

python program.py
Steven Walton
  • 505
  • 6
  • 20
  • Do I not need to install the external libraries I'm using? I do 'import requests' etc and so the raspberry pi would need to know how to find this. – Tom Dippé Sep 14 '16 at 21:11
  • @TomDippé, yes, you need to install them, but you're running Debian; it has them (well, definitely `requests`) packaged. – Charles Duffy Sep 14 '16 at 21:19
  • @TomDippé if you are unsure about packages that are included you can always look in your python path. But Charles is correct in that most of the libraries will be there. The only things that might not be are extra libraries that you installed later, ie not default python libraries like numpy or scipy. Then use pip to install, apt, or something like anaconda. – Steven Walton Sep 14 '16 at 21:35
0

Use PyInstaller. In the terminal, to create a stand alone exe just use a command like this:

pyinstaller -F myscript.py
deedle
  • 105
  • 11
  • Not for a Raspberry Pi -- those don't run .exe files, and you *really* don't want the startup-time overhead of a PyInstaller unpack anyhow. – Charles Duffy Sep 14 '16 at 21:10
  • RaspPi is running Raspbian (based on Debian) and so can't run as an exe – Tom Dippé Sep 14 '16 at 21:12
  • @TomDippé, ...PyInstaller is not actually Windows-only -- it can create Linux executables as well -- but those Linux executables will be painfully slow to run (well, to start running) on a Pi. – Charles Duffy Sep 14 '16 at 21:20
0

As the other answers have said, you can just run your code on the Pi, because Python code is interpreted and not complied.

That being said, you will need to install any python packages, like the ImgurClient beforehand. If you did this with PyCharm on you PC, you will likely have to use pip on the Pi.

LoveMeSomeCode
  • 3,888
  • 8
  • 34
  • 48
0

I don't know if you are gonna be able to run the python script in your other environment, specially if the script uses external libraries (.whl) that you normally install with pip.

A good option to run the script on a clean environment is to use virtualenv:

https://virtualenv.pypa.io/en/stable/

"It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either)"

And then install only the necessary librarys to run your script. If you export this new environment, you will probably be able to run your script without any problems.

Renato J. F.
  • 106
  • 5