Creating a Portable Python (local install) for Linux

后端 未结 7 1129
醉酒成梦
醉酒成梦 2021-02-07 15:53

I\'m looking to create the following:

A portable version of python that can be run on any system (with any previous version of python or no python installed) and have it

相关标签:
7条回答
  • 2021-02-07 16:36

    I don't know how this is even possible. If it were, they woudn't need to distribute binary packages of python for different platforms. You can't simply distribute python that will run on any platform. It has to be built from source for that arch. Virtualenv will expect you to tell it which system python to use (using links).

    This pretty much goes for almost any binary package that links against system libs. Again, if it were possible, we wouldn't need any platform specific binary distributions.

    You can, however, achieve part of what you want. That is, running python on another machine that doesn't have python installed as long as its the same arch. This is the same concept behind freezing, or py2exe/py2app/pyinstaller. An interpreter is bundled into a standalone environment. So the app can run on any similar platform.

    Edit

    I just realized that while your question speaks about "system" agnostically, your title contains the reference "linux". There are different flavors of linux, so in order for it to work you would have to build it fat for multiple archs and also completely contain the standalone links. You might try building a package with pyinstaller and using that to include in your project.

    You can try just building python from source, in your virtualenv:

    $ ./configure --prefix=/path/to/virtualenv && make && make install
    

    If you still have problems with the links to libs, you can also investigate building it statically

    0 讨论(0)
  • 2021-02-07 16:37

    I faced the same problem, so I created PortableVirtualenv. Your Question is just the definition of it.

    I use it as a base for commercial multiplatform app I develop. (But PortableVirtualenv is public domain - use it freely.)

    If needed, you can pip-install any package and zip the whole directory to distribute also packages you need.

    0 讨论(0)
  • 2021-02-07 16:38

    I'm not sure that working solely in Python is the way to go here. You might have better luck with Puppet of Chef, which are configuration tools that can be used to create a local environment. There is plenty of code out there to install virtualenv and python on just about any Linux plus OSX (probably not Windows though).

    Your workflow would be to install chef or Puppet (your choice), run a script to install the Python you want, then enter a virtualenv and pip install any packages you might need.

    Sorry this isn't as easy as virtualenv alone, but it is much more robust.

    0 讨论(0)
  • 2021-02-07 16:38

    You can try this: http://downloads.activestate.com/ActivePython/ Look in the menu: Downloads >> ActivePython

    0 讨论(0)
  • 2021-02-07 16:41

    One nice option is to make a "snap" portable linux application. They have a python mode which lets you specify you specify exactly what modules you need. From https://snapcraft.io/first-snap#python :

    Snaps let you distribute a dependency-isolated Python app in an app store experience for end users.

    Another option is to containerize your application with something like docker. Then instead of executing your script directly, the user is actually running a small OS with just your application and its dependencies. https://www.infoq.com/articles/docker-executable-images/ has more about executable containers.

    Container images can also be used for short lived processes: a containerized executable meant to be run on your computer. These containers execute a single task, are short lived and can generally be removed after use. We call these executable images. Examples are compilers (Golang) or build tools (Maven), presentation software (I love to hack a simple presentation in Markdown format and let a RevealJS Docker image serve that) and browsers (a fresh contained browser to follow that fishy link). A real evangelist for executable images is Docker's own Jessie Frazelle. To get some great inspiration be sure to read her blog about them or check out this presentation at DockerCon 2015.

    0 讨论(0)
  • 2021-02-07 16:44

    I just tested this and it works great.

    Get the copy of python you want to install and untar it and cd to the untarred folder first.

    Also get a copy of setuptools and untar that.

    /opt/portapy used below is of course just the name I came up with for this post, it could be any path and the full path should be tarred up and the same path should be used on any systems you put this on due to absolute path linking.

    mkdir /opt/portapy
    cd <python source dir>
    ./configure --prefix=/opt/portapy && make && make install
    cd <setuptools source dir>
    /opt/portapy/bin/python ./setup.py install
    

    Make the virtual env folder inside the portapy folder.

    mkdir /opt/portapy/virtenv
    /opt/portapy/bin/virtualenv /opt/portapy/virtenv
    cd /opt/portapy/virtenv
    source bin/activate
    

    Done. You are ready to install all of your libraries here and have the option of creating multiple virtual envs this way.

    You can then tar up the whole /opt/portapy folder and transport it to any Linux system of the same arch, within reason I suspect.

    I compiled 2.7.5 ond centOS 5.8 64bit and moved the folder to a Cent6.9 system and it runs perfectly.

    0 讨论(0)
提交回复
热议问题