How to run Python egg files directly without installing them?

蓝咒 提交于 2019-12-03 03:05:02

问题


Is it possible to run Python egg files directly as you can run jar files with Java?

For example, with Java you might dos something like:

$ java -jar jar-file

回答1:


A python egg is a "a single-file importable distribution format". Which is typically a python package.

You can import the package in the egg as long as you know it's name and it's in your path.

You can execute a package using the "-m" option and the package name.

However, python packages generally do not do anything when executed, and you may get an error. The -c option can be used to run code. (See http://docs.python.org/using/cmdline.html for details on command line options)

> python -m sphinx
sphinx is a package and cannot be directly executed


> python -c "import <package in an egg>; <function>();"



> python -c "import sphinx; print sphinx.package_dir"
C:\Python26\lib\site-packages\sphinx-0.6.1-py2.6.egg\sphinx



回答2:


As of Python 2.6, you can use python some.egg and it will be executed if it includes a module named __main__.

For earlier versions of Python, you can use PYTHONPATH=some.egg python -m some module, and somemodule from the egg will be run as the main module. (Note: if you're on Windows, you'd need to do a separate SET PYTHONPATH=some.egg.)




回答3:


For example, if you want to import the suds module which is available as .egg file:

egg_path='/home/shahid/suds_2.4.egg'

sys.path.append(egg_path)

import suds
#... rest of code



回答4:


Python Egg file direct execution steps

Suppose if you have egg file and driver file to run through below command.

PYTHONPATH=eggfilename.egg python driverfile.py

above command for without install egg file with python code.



来源:https://stackoverflow.com/questions/1264447/how-to-run-python-egg-files-directly-without-installing-them

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!