python在服务器上部署应用程序

时间:2014-09-09 09:42:47

标签: python linux

我已经开发了这个python应用程序,它通过转到目录

来工作
cd Truman-2.5/truman/

并执行

./truman -c config/tru.ini

➜  Truman-2.5  tree
.
├── CHANGES
├── LICENSE
├── README
├── docs
├── files.txt
├── requirements.txt
├── scripts
├── setup.py
├── tests
│   └── __init__.py
└── truman
    ├── __init__.py
    ├── config
    │   └── tru.ini
    ├── vcore
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── addVM.py
    │   ├── addVM.pyc
    └── truman

我还创建了一个设置文件

setup.py

import os
from setuptools import setup

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name = "truman",
    version = "2.5",
    author = "KSHK",
    author_email = "kshk@kshk.com",
    description = ("Truman Automation Tool"),
    license = "BSD",
    keywords = "example documentation tutorial",
    url = "https://github.com/truman/trumman.git",
    packages=['truman.vcore','tests'],
    long_description=read('README'),
    scripts=["truman/truman"],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
)

现在,我想部署这个可执行文件" truman"在/ usr / local / bin中,所以我在setup.py中使用了scripts参数,但是它在/ usr / local / bin中创建了这个可执行文件

#!/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
# EASY-INSTALL-SCRIPT: 'truman==2.5','truman'
__requires__ = 'truman==2.5'
import pkg_resources
pkg_resources.run_script('truman==2.5', 'human')

从/ usr / local / bin下的代码部署python可执行文件的最佳方法是什么,并确保它从您的应用程序导入包和模块?

更新

将entry_points添加到setup.py

import os
from setuptools import setup

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name = "truman",
    version = "2.5",
    author = "KSHK",
    author_email = "kshk@kshk.com",
    description = ("truman Automation Tool"),
    license = "BSD",
    keywords = "example documentation tutorial",
    url = "https://git/git",
    packages=['truman'],
    long_description=read('README'),
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
    entry_points = {
    'console_scripts': [
        'truman = truman:main_func',
    ],
    }
)

然而执行truman会出现此错误:

➜  truman-2.5  /usr/local/bin/truman -c truman/config/tru.ini
Traceback (most recent call last):
  File "/usr/local/bin/truman", line 8, in <module>
    load_entry_point('truman==2.5', 'console_scripts', 'truman')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 318, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2221, in load_entry_point
    return ep.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 1959, in load
    raise ImportError("%r has no %r attribute" % (entry,attr))
ImportError: <module 'truman' from '/Library/Python/2.7/site-packages/truman-2.5-py2.7.egg/truman/__init__.pyc'> has no 'main_func' attribute

truman脚本基本上调用vcore模块并将值传递给addVM

#!/usr/bin/python
__author__ = 'krishnaa'
import os
os.sys.path.append(os.path.dirname(os.path.abspath('.')))

import optparse
from vcore.addVM import addVM

def main():
    parser = optparse.OptionParser()
    parser.add_option('-c', '--config',
                      dest="config_file",
                      default='vcloudE.ini'
                        )
    options, remainder = parser.parse_args()
    addVM(options)

if __name__ == "__main__":
    main()

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你可以使用entry_points,你可以指出应用程序的主要功能。入口点将生成与上面类似的脚本,但没关系。这个脚本只执行你的main_function。

setup(
    entry_points = {
        'console_scripts': [
            'truman = truman.truman:main_func',
        ],
    }
)