简体   繁体   English

安装Python命令行脚本

[英]Installing Python command line script

I am trying to understand how to install Python packages with command line scripts. 我试图了解如何使用命令行脚本安装Python软件包。 What I mean by this is installing a package with eg pip install package and then be able to run package while in any directory, ie, that package should be on my path. 我的意思是用pip install package ,然后可以在任何目录中运行package ,即该package应该在我的路径上。

I am following this guide , which creates a minimal working example, and you install the package with pip install . 我正在遵循本指南该指南创建了一个最小的工作示例,然后使用pip install .安装软件包pip install . .

There are two alternatives here, one with console_scripts in entry_points , and one with scripts and creating a bin folder and put an executable script in there. 这里有两种选择,一种是在entry_points带有console_scriptsentry_points一种是具有scripts并创建bin文件夹并将可执行脚本放入其中的脚本。 More detailed: 更详细:

First method: scripts 第一种方法: scripts

Add a directory bin , in the same directory as the funniest package. 在与funniest包相同的目录中添加目录bin So: 所以:

funniest/
    __init__.py
    text.py
bin/
    funniest-joke

The funniest-joke file is funniest-joke文件是

#!/usr/bin/env python

import funniest
print(funniest.joke())

In setup.py , add scripts=['bin/funniest-joke'] as an argument in the setup function call. setup.py ,将scripts=['bin/funniest-joke']setup函数调用中的参数。

Second method: console_scripts 第二种方法: console_scripts

Add a command_line.py file alongside text.py and __init__.py , which is text.py__init__.py旁边添加一个command_line.py文件,该文件是

import funniest

def main():
    print funniest.joke()

In setup.py , add this as an argument to the setup function call: setup.py ,将此作为参数添加到setup函数调用中:

entry_points={
    'console_scripts': [
        'funniest-joke = funniest.command_line:main'
    ],
},

The problem is that I can't get any of these to work. 问题是我无法使其中任何一个正常工作。 The packages install just fine, but I do not get an executable on my path, and it does not even seem to put anything in any /bin/ directory. 这些软件包可以很好地安装,但是我的路径上没有可执行文件,而且似乎也没有在/bin/目录中放置任何内容。 There should be a funniest-joke on my path, but there is not. 我的路上应该有个funniest-joke ,但是没有。

For information: I am using a miniconda installed Python, which is at ~/miniconda3/ , so my pip , which I use to install the packages, is at ~/miniconda3/bin , and Python is also the one at ~/miniconda3/bin . 有关信息:我使用的是安装在miniconda上的Python,位于~/miniconda3/ ,所以我用来安装软件包的pip位于~/miniconda3/bin ,Python也是~/miniconda3/bin

The package ( funniest ) is installed into /miniconda3/lib/python3.6/site-packages/funniest . 软件包( funniest )安装在/miniconda3/lib/python3.6/site-packages/funniest

My path contains the following directories. 我的路径包含以下目录。

/bin/
/usr/local/bin/python
/Users/USERNAME/miniconda3/bin
/usr/local/bin
/usr/bin

Is anyone able to help me? 有人可以帮助我吗?

When you enter in the terminal: 当您输入终端时:
which python 哪个蟒蛇

The example will show you the location of the default python you are using. 该示例将向您显示您正在使用的默认python的位置。
By what you have said your result will probably be. 根据您所说的,您的结果可能会是。
/Users/USERNAME/miniconda3/bin/python /用户/用户名/ miniconda3 / bin / python

That being said, if you enter: 话虽如此,如果您输入:
which <executable> 哪个<可执行文件>
for any executable, the terminal will show you where it is installed. 对于任何可执行文件,终端将向您显示其安装位置。
I hope that helps. 希望对您有所帮助。

you don't need to follow the path to install the package in python. 您无需按照以下路径在python中安装软件包。 But you need to follow this way 1. first install python on pc 2. simple Open command prompt then type pip install (---python package name----) 3. then press Enter button but make sure internet is available. 但是您需要遵循以下方式:1.首先在PC上安装python 2.简单的打开命令提示符,然后键入pip install(--- python package name ----)3.然后按Enter按钮,但请确保Internet可用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM