6

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:

user2514157
  • 545
  • 6
  • 24
Victor Pira
  • 1,132
  • 1
  • 10
  • 28
  • why can't you install those modules in new environment? – Sarit Adhikari Jun 07 '15 at 08:52
  • Of course I could, but I am interested in possibilities how to don't do that. – Victor Pira Jun 07 '15 at 08:56
  • It's not going to work without those libraries. This has nothing to do with `.pyc` files. – mata Jun 07 '15 at 08:56
  • How can you run a script of which dependencies are missing? Even you skip the failure of importing such libraries and proceed, finally you'll end up being unable to do some commands. – TaoPR Jun 07 '15 at 09:01
  • It is not always possible to install software libraries on a computer (for example, if an employer does not give their employees administrative privileges). Hence the desire to package all necessary code such that it can be run on the python interpreter without relying on external dependencies. – user2514157 Apr 22 '20 at 03:17

3 Answers3

3

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!

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
1

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.

P i
  • 29,020
  • 36
  • 159
  • 267
  • 2
    External libraries written in Python are also a problem, because the bytecode of your application doesn't include them. – Roland Smith Jun 07 '15 at 14:01
1

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.

Elazar
  • 20,415
  • 4
  • 46
  • 67