V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
craiiz
V2EX  ›  Python

大家帮我看看这个功能该怎么实现

  •  
  •   craiiz · 2020-04-06 01:50:21 +08:00 · 2399 次点击
    这是一个创建于 1471 天前的主题,其中的信息可能已经有所发展或是发生改变。

    tuxiang

    如上图所示

    我想实现:双击 command.command 文件自动运行 manage.py,需要在 command 文件里怎么写?

    当前情况:在 command 文件里只写了 python manage.py

    分析:需要 command 文件运行的时候能够获取当前正在运行的 command 文件的路径;类似于 python 的 sys.path[0] 或者 os.path.basename[file]的效果。 补充之后的 command 文件内容应该类似于:

    """

    cd 当前 command 文件路径;
    python manage.py

    """

    但是目前 google 许久,没有找到类似的终端命令 求大神相助。。。。。。

    10 条回复    2020-05-15 11:07:03 +08:00
    Trim21
        1
    Trim21  
       2020-04-06 02:01:00 +08:00
    command 应该是由 shell 来解释的吧?应该看看 zsh 的语法
    Trim21
        2
    Trim21  
       2020-04-06 02:05:42 +08:00
    搜了下,zsh 里面大概是这样
    python "${0:a:h}/main.py"
    craiiz
        3
    craiiz  
    OP
       2020-04-06 02:11:05 +08:00
    感谢 @Trim21
    已解决,需要在前面加上一句:cd $(dirname $BASH_SOURCE)
    also24
        4
    also24  
       2020-04-06 02:12:47 +08:00 via Android
    没太理解楼主的目的,直接使用:

    python ./manage.py

    会有什么问题出现嘛?
    craiiz
        5
    craiiz  
    OP
       2020-04-06 02:15:17 +08:00
    微博的图那么快就挂了???
    craiiz
        6
    craiiz  
    OP
       2020-04-06 02:18:42 +08:00
    @also24 没想到微博的图那么快就挂了。但这样会不能运行,会提示:
    python: can't open file './manage.py': [Errno 2] No such file or directory
    logout
    Saving session...
    ...copying shared history...
    ...saving history...truncating history files...
    ...completed.
    craiiz
        7
    craiiz  
    OP
       2020-04-06 02:22:28 +08:00
    微博图挂了,为保证主题质量,之后有人遇到同样问题还可解决。现重述问题:


    场景:command 。command 和 manage 。py 在同一个文件夹

    我想实现:双击 command 。command 文件自动运行 manage 。py,需要在 command 文件里怎么写?

    当前情况:在 command 文件里只写了 python manage 。py 。

    分析:需要 command 文件运行的时候能够获取当前正在运行的 command 文件的路径;类似于 python 的 sys 。path[0] 或者 os 。path 。basename[file]的效果。 补充之后的 command 文件内容应该类似于:

    """

    cd 当前 command 文件路径;
    python manage 。py

    """

    但是目前 google 许久,没有找到类似的终端命令 求大神相助。。。。。。
    Trim21
        8
    Trim21  
       2020-04-06 02:28:20 +08:00
    @also24 #4 大概因为 pwd 不是这个文件夹,所以找不到这个 manage.py
    oott123
        9
    oott123  
       2020-04-06 09:21:55 +08:00 via Android
    先写一个 bash 内容是 cd xx; python manager.py 再在 command 里指向这个 bash

    当然你可以查查 command 有没有改 working directory 的功能,应该也有
    craiiz
        10
    craiiz  
    OP
       2020-05-15 11:07:03 +08:00
    已解决,谢谢各位大神
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3398 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:29 · PVG 20:29 · LAX 05:29 · JFK 08:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.