简体   繁体   English

Python打包问题(Ppt):尝试运行使用命令行中的non.py文件的打包项目时出错

[英]Python packaging trouble(Ppt): Getting error when trying to run my packaged project that uses a non-.py file from the command line

After doing $ pip install path/to/MyProject-0.1.0.tar.gz I am able to successfully do import myproject from every directory on my computer using the python interpreter. 在执行$ pip install path/to/MyProject-0.1.0.tar.gz我能够使用python解释器成功地从我计算机上的每个目录import myproject

However, my goal is to be able to just run $ myproject from every directory. 但是,我的目标是能够从每个目录运行$ myproject When I run $ myproject I get this: 当我运行$ myproject我得到了这个:

IOError: [Errno 2] No such file or directory: '/home/username/.virtualenvs/vir_env/local/lib/python2.7/site-packages/myproject/myproject_style.cssc'

I looked in the /home/username/.virtualenvs/vir_env/local/lib/python2.7/site-packages/myproject/ dir and the myproject_style.css file is there. 我查看了/home/username/.virtualenvs/vir_env/local/lib/python2.7/site-packages/myproject/ dir, myproject_style.css文件就在那里。

Notice the error says cssc instead of css . 注意错误说cssc而不是css I don't know why I get this error because the program runs perfectly fine when I navigate to the myproject dir and do $ python __init__.py 我不知道为什么我会收到此错误,因为当我导航到myproject目录并执行$ python __init__.py时程序运行完全正常

Project overview: 项目概况:

.
├── MyProject.egg-info
│   ├── dependency_links.txt
│   ├── entry_points.txt
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── dist
│   └── MyProject-0.1.0.tar.gz
├── LICENSE.txt
├── MANIFEST.in
├── myproject
│   ├── __init__.py
│   └── myproject_style.css
├── README
└── setup.py

setup.py setup.py

from setuptools import setup

setup(
    name='MyProject',
    version='0.1.0',
    author='B',
    author_email='jrh@example.com',
    packages=['myproject', ],
    include_package_data=True,
    url='http://pypi.python.org/pypi/MyProject/',
    license='LICENSE.txt',
    package_data={'': ['myproject_style.css']},
    data_files=[('myproject',['myproject_style.css'])],
    entry_points={
    'console_scripts': ['myproject = myproject:main', ],},
    description='Shitz and giggles.',
    long_description=open('README').read(),
)

MANIFEST.in MANIFEST.in

include myproject/myproject_style.css

init .py init .py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
try:
    from pygments import highlight
    from pygments.lexers import PythonLexer
    from pygments.formatters import HtmlFormatter
except:
    print "\npygments is not installed yet. Install pygments or run 'pip install -r requirements.txt'.\n"
    sys.exit()

import argparse

import SimpleHTTPServer
import BaseHTTPServer
import webbrowser
import SocketServer
import os
import distutils.sysconfig as dc

import urllib
import cgi
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

class MyProjectHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        path = os.path.join(os.getcwdu(), self.path[1:])
        if os.path.exists(path) and path.endswith('.py'):
            with open(path) as file:
                cod = file.read()
                hl = highlight(cod, PythonLexer(), HtmlFormatter(noclasses=True, linenos='table', style='friendly')
                hl = '<body style="background:#f0f0f0">'+hl+'</body>'
                print hl
                self.send_response(200)
                self.end_headers()
                self.wfile.write(hl)
        else:
            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def list_directory(self, path):
        try:
            lis = os.listdir(path)
        except os.error:
            self.send_error(404, "No permission to list directory")
            return None
        lis.sort(key=lambda a: a.lower())
        f = StringIO()
        css_file = open(path_to_myproject_css).read()
        f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
        f.write("<html>\n<head>\n<title>Directory and python file listing for %s</title>\n" % path)
        f.write('<style type="text/css"> %s</style>\n' %css_file)
        f.write('</head>\n')
        f.write("<body>\n<div class=page-container>\n<h2>Myproject</h2>\n")
        f.write("<ul class=shortcut-list>\n")
        f.write("<li class=shortcut-list><a href=%s>Python standard library modules</a></li>" % path_standard_library)
        f.write("\n<li class=shortcut-list><a href=%s>User installed python modules</a></li>" % path_external_packages)
        try:
            with open(os.getenv("HOME")+'/.myprojectrc', "r") as prc: 
                prc_string = prc.read()
            f.write(prc_string)
        except:
            pass

        f.write("\n</ul>")  
        f.write("\n<h5>Directory and python file listing for %s</h5>" % path)
        f.write("\n<hr>\n<ul>\n")
        os.chdir("/")
        for name in lis:
            fullname = os.path.join(path, name) 
            displayname = name
            condition1 = name.endswith(u'.py') or os.path.isdir(fullname) or os.path.islink(fullname) 
            condition2 = name.endswith(u'.egg-info') or name.endswith(u'.egg')
            if condition1 and not condition2:    
                if os.path.isdir(fullname):
                    displayname = name + "/"
                if os.path.islink(fullname):
                    displayname = name + "@"
                f.write('<li><a href="%s">%s</a>\n'% (fullname, displayname))
        f.write("</ul>\n<hr>\n</div>\n</body>\n</html>\n")
        length = f.tell()
        f.seek(0)
        self.send_response(200)
        encoding = sys.getfilesystemencoding()
        self.send_header("Content-type", "text/html; charset=%s" % encoding)
        self.send_header("Content-Length", str(length))
        self.end_headers()
        return f  

def _add_argument_handler():
    if os.path.isdir(subcmd):
        print "\nYou added '%s' to the top directory links." % subcmd
        d_name = raw_input("\nSpecify the name of this link. Just pressing <enter> uses '%s' as the link name.\n> " % subcmd)
        print ""
        if len(d_name) == 0:
            d_name = subcmd
        else:
            pass
        with open(os.getenv("HOME")+'/.myprojectrc', "a") as prc:
            prc.write("<li class=shortcut-list><a href=%s>%s</a></li>\n" % (subcmd, d_name))
    else:
        print "\n'%s' is not an existing directory path.\n" % subcmd

def _remove_argument_handler():
    if not _link_name_checker(subcmd):
        print "\nthe top directory link name '%s' does not exist yet.\n" % subcmd
    else:
        _remove_line(subcmd)
        print "\nYou removed the top directory link name '%s'.\n" % subcmd

def _link_name_checker(subcmd):
    with open(os.getenv("HOME")+'/.myprojectrc', "r") as prc:
        lines = prc.readlines()
        for line in lines:
            suff = "%s</a></li>\n" % subcmd
            if line.endswith(suff):
                return True
            else:
                pass
        return False

def _remove_line(del_d_name):
    with open(os.getenv("HOME")+'/.myproject', "r") as prc:
        lines = prc.readlines()

    with open(os.getenv("HOME")+'/.myproject', "w") as prc:
        for line in lines:
            suff = "%s</a></li>\n" % del_d_name
            if not line.endswith(suff):
                prc.write(line)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("cmd", help="Execute a command", action="store", nargs='*')
    args = parser.parse_args()

    if args.cmd:
        cmd, subcmd = args.cmd
        if cmd == "add":
            _add_argument_handler()
        if cmd == "remove":
            _remove_argument_handler()
        else:
            print "\n'%s' is not a valid argument.\n" % cmd
        sys.exit()
    else:
        pass

    webbrowser.open("http://localhost:8090")

    os.chdir("/usr/lib/python2.7")

    print "\nPress <CTRL> + C to stop running myproject.\n"

    server = BaseHTTPServer.HTTPServer(('', 8090), MyProjectHTTPRequestHandler)
    try:
        server.serve_forever()
    except:
        print "\nBye bye!\n"

path_to_myproject = os.path.abspath(__file__)
path_to_myproject_css = path_to_myproject.replace("/__init__.py", "/myproject_style.css")
path_external_packages = dc.get_python_lib()
path_standard_library = dc.get_python_lib(standard_lib=True)

if __name__ == '__main__':
    main()

The problem is not that obvious but I think I know what's wrong. 问题不是那么明显,但我想我知道什么是错的。

This line of code: 这行代码:

path_to_myproject = os.path.abspath(__file__)

can return not only ".../__init__.py", but also ".../__init__.pyc" string 不仅可以返回“... / __ init__.py”,还可以返回“... / __ init __。pyc”字符串

Thus this line of code: 因此这行代码:

path_to_myproject_css = path_to_myproject.replace("/__init__.py", "/myproject_style.css")

will not only take __init__.py, but also __init__. 不仅会__init__.py,还会__init__。 pyc - a byte-compiled version of your py module. pyc - py模块的字节编译版本。 That's the most possible reason why you get cssc instead of css ; 这是你获得cssc而不是css的最可能原因; you just get "/__init__.py" replaced by "/myproject_style.css" in string ".../__init__.pyc". 你只需在字符串“... / __ init __。pyc”中用“/myproject_style.css”替换“/__init__.py”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从命令行运行时“找不到f2py” - “f2py not found” when trying to run from command line 从打包为zip文件的应用程序的命令行运行pdb? - Run pdb from command line on application packaged as a zip file? 当我尝试从python运行shell&#39;sed&#39;命令时出现错误 - Getting error when I am trying to run shell 'sed' command from python 如何从Windows命令行运行python文件(.py)而不必先键入python? - How to run a python file (.py) from the windows command-line without having to type python first? 尝试运行命令时收到错误,提示“ctx 是缺少的必需参数”。 [Python,discord.py] - Am receiving an error when trying to run a command saying 'ctx is a required argument that is missing'. [Python, discord.py] 尝试从命令行执行脚本时出现Python语法错误 - Python syntax error when trying to execute a script from command line 如何使用python file.py从命令行运行类? - How to run class from command line using python file.py? 当我运行 this.py 文件时,我收到一个不间断的错误 [Sel - I'm getting a non-stop error when I run this .py file [Sel 从命令行运行 python 脚本时未找到模块错误 - module not found error when python script is run from command line 从命令行运行时 Python sendgrid 库错误 - Python sendgrid library error when run from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM