Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running python script without installed libraries

I have working Python script using scipy and numpy functions and I need to run it on the computer with installed Python but without modules scipy and numpy. How should I do that? Is .pyc the answer or should I do something more complex?

Notes:

  • I don't want to use py2exe. I am aware of it but it doesn't fit to the problem.
  • I have read, these questions (What is the difference between .py and .pyc files?, Python pyc files (main file not compiled?)) with obvious connection to this problem but since I am a physicist, not a programmer, I got totally lost.
like image 567
Victor Pira Avatar asked Jun 07 '15 08:06

Victor Pira


People also ask

Can I run Python script without Python installed?

py2exe is a Python extension which converts Python scripts (. py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed. It is the most common tool for doing so.

Can you use Python without libraries?

It is not possible. A pyc -file is nothing more than a python file compiled into byte-code. It does not contain any modules that this file imports!

Can you use Python without installing?

If you are on a system where you don't want to or cannot install a Python interpreter, but you'd like to practice your Python skills, or just want to quickly test an idea, online Python interpreters are a great choice for you. Using an online Python interpreter you can simply run Python code in your web browser.


3 Answers

It is not possible.

A pyc-file is nothing more than a python file compiled into byte-code. It does not contain any modules that this file imports!

Additionally, the numpy module is an extension written in C (and some Python). A substantial piece of it are shared libraries that are loaded into Python at runtime. You need those for numpy to work!

like image 181
Roland Smith Avatar answered Oct 05 '22 05:10

Roland Smith


Python first "compiles" a program into bytecode, and then throws this bytecode through an interpreter.

So if your code is all Python code, you would be able to one-time generate the bytecode and then have the Python runtime use this. In fact I've seen projects such as this, where the developer has just looked through the bytecode spec, and implemented a bytecode parsing engine. It's very lightweight, so it's useful for e.g. "Python on a chip" etc.

Problem comes with external libraries not entirely written in Python, (e.g. numpy, scipy).

Python provides a C-API, allowing you to create (using C/C++ code) objects that appear to it as Python objects. This is useful for speeding things up, interacting with hardware, making use of C/C++ libs.

like image 38
P i Avatar answered Oct 05 '22 06:10

P i


Take a look at Nuitka. If you'll be able to compile your code (not necessarily a possible or easy task), you'll get what you want.

like image 29
Elazar Avatar answered Oct 05 '22 06:10

Elazar