Python 包安装及常用命令【python 入门】

 背景:

        近期看到一个项目,做微信只能机器人,服务是使用python搭建的,于是拷贝下来自己打算跑一跑,部署一下,可是自己又没有python的经验,于是各种查资料学习,跟着敲一敲,顺便记录一下python的各种技能点。

1 python安装

我安装的是python 3.10.8,window环境:直接打开 python官网,下滑,找到对应的包,使用installer模式,下载后直接双击安装即可;安装时记得勾选 【add python xx to PATH】

安装完成后, 打开命令提示符窗口(方法是点击“开始”-“运行”-输入:“cmd”),敲入 Python 后,会出现下图,即表示安装成功。

参考:安装python详细步骤(超详细,保姆级,一步一图)_python安装-优快云博客

 2 pathon 常用命令

因为我这里是window环境,不了解其他mac,linux是否有差异,这里也不做叙述。

pip、pip3  

pip命令在python包索引(pypI)中查找包,解析以来项,并安装指定包版本,

要安装Python包,您只需键入要安装的包pip的名称和名称。

以下命令将安装最新版本的软件包:

pip install django
如果您需要特定版本,请运行以下命令:

#pip install package== version
pip install django==4.0.4
在处理协作项目时,您需要跟踪依赖关系,通常使用需求文件。
使用该r标志,您可以从文本文件中读取和安装软件包:

pip install -r requirements.txt
另一个常用的功能是freeze标志。它用于输出您在环境中安装的软件包版本列表。
您可以使用它将依赖项输出到需求文件:

pip freeze >> requirements.txt

3 导航命令

ls, dir

要列出目录(文件夹)的内容,您必须使用ls(Unix) 或dir(Windows) 命令。这可能是您第一次遇到CLI时学到的第一个命令。 

这是使用的语法:

ls # Shows the contents of the working directory
ls mydirectory
以下是本地文件系统中文件夹内容的示例:

ls test_python/
#classes_error.py radius.py test-Django
该命令有许多有用的标志。事实上,ls -al查看隐藏文件(以点开头的文件)
以及每个文件的模式、大小和日期通常被称为查看隐藏文件:

alias ls=“ls -al”
#Results
total 20
drwx------ 3 daniel daniel 4096 ene 16 19:13 .
drwxr-xr-x 36 daniel daniel 4096 may 17 22:18 …
-rw------- 1 daniel daniel 32 nov 17 2020 classes_error.py
-rw------- 1 daniel daniel 327 nov 10 2020 radius.py
drwx------ 4 daniel daniel 4096 ene 16 01:07 test-Django
对于Windows,您可以通过Git Bash使用ls,也可以使用内置dir命令:

dir

pwd 密码

pwd代表“打印工作目录”,它正是这样做的:为您提供您所在目录的完整路径: 

pwd
#/home/daniel/github/HTML-site/images
如果您曾经在终端中迷失过自己,那么这个命令就是救命稻草。

您可以在Windows中使用cd不带参数的命令实现相同的输出(请注意,Unix中的相同命令会将您带到主目录):

#Only on Windows
cd
#D:FoldersubFolder

cp 拷贝/复制

使用图形文件管理器复制文件很直观,但效率低下。使用此命令,您可以在系统上复制任何类型的文件: 

cp old_file.txt copy_old_file.txt
要复制目录的所有内容,您必须使用cp -r:

cp -r originaldirectory/ newdir
cp命令在Windows中是copy:

copy old_file.txt copy_old_file.txt /a

cat, type

要在终端中打印文本文件的内容而不使用编辑器打开文件,您可以在Unix和Windows上使用cat、more或:lesstype 

cat old_file.txt # Unix
type old_file.txt # Windows
#Content
Hi there I hope you’re enjoying the article …
as much as I’ve enjoyed writing it!
End of the sample.

mv, move  移动,相当于剪切-复制

mv命令将文件和目录从一个目录移动到另一个目录 – 基本上是剪切和粘贴 – 如果目标不存在,则重命名文件: 

#Rename files
mv source_file.txt renamed_file.txt

#File to another directory
mv renamed_file.txt newdir/
您还可以使用模式匹配来移动文件。例如,将所有.py文件移动到另一个文件夹:

mv *.py mypythondir/
Windows上的等效命令是move,其功能与上述几乎相同:

#Windows
move source_file.txt renamed_file.txt

rm, del   删除

您可以使用rm命令删除文件和目录。 

要删除文件而不是目录,您可以使用:

rm file_to_remove.txt
如果要删除空目录,可以使用递归 ( -r) 标志:

rm -r dir_to_remove/
要删除包含内容的目录,您可以使用force ( -f) 和递归标志:

rm -rf dir_with_content/
在类似的形式中,您可以在Windows上找到del。更加谨慎,因为此命令没有上面看到的阻止标志:

del mywindowsdir

小心这个命令。一旦你删除了某些东西,就很难恢复它。

exit 退出

完成Python编程后,您应该能够退出shell会话。在大多数情况下,这也会关闭您正在使用的终端:

exit
请注意,此命令适用于Windows和Unix。

4 命令行编辑

Vim/Neovim

Vim及其后代Neovim是基于键盘的文本编辑器,主要用于命令行。

Vim预装在Linux和macOS上。实际上,它是您在与服务器交互时会遇到的最多的编辑器。在Windows上,您需要使用Vim页面中的可执行安装程序来安装它。

现在,您只需在命令行中输入Vim的名称即可享受Vim的强大功能:

vim
这将触发一个基于文本的界面,其中包含多个键盘组合,用于您在Python中编码时可能需要的每个操作。

Vim有一个陡峭的学习曲线,但是一旦你掌握了它,你就不会很快转向其他东西

Info 要退出Vim,请按“Escape”并键入:q。

5 开发工具

 virtualenv/venv

虚拟环境是Python开发中使用的一项关键技术。使用它们,您可以将跨不同项目使用的包隔离到一个轻量级文件夹中,通常命名为.venv.

使用Python 3.3或更高版本,您可以使用内置的venv模块来创建虚拟环境:

#.venv being the name of the virtual environment
python -m venv .venv
virtualenv是一个外部项目,与内置选项相比,它更快、更具可扩展性。要创建虚拟环境,首先安装virtualenv包:

#Installs virtualenv
pip install --user virtualenv
#Creates a .venv virtual environment
virtualenv .venv
接下来,您需要激活虚拟环境。在Windows上,根据您使用的是cmd还是PowerShell(推荐)运行以下命令之一:

:: PowerShell
.venvScriptsActivate.ps1

:: Cmd
.venvScriptsactivate.bat
在Linux或macOS上:

source .venv/bin/activate

Git

Docker

Docker使将Python应用程序打包和发布为轻量级、可移植、自给自足的容器变得更加容易。它有助于开发和部署,允许所有协作者使用相同的设置。

要使用Docker,您必须严格遵循获取Docker页面上为您的操作系统显示的安装过程。

要列出可用的Docker命令,请运行以下命令:

docker help

Grep

HTTPie

HTTPie是一个命令行HTTP客户端,可以更轻松地与Web服务交互。例如,您可以使用它来测试您的Python API,或与第三方网站进行交互。

这个CLI工具几乎在每个包管理器中都可用,如HTTPie的官方文档所示。但是,它也可以作为Python包提供,因此您可以使用pip安装它。

pip install httpie
以下是查询远程 API 的方式——在本例中为GitHub API:

http GET https://api.github.com/users
HTTP/1.1 200 OK
Accept-Ranges: bytes
Access-Control-Allow-Origin: *

ping

ping是几乎所有操作系统上默认可用的CLI命令。它的工作原理是向IP地址发送数据包并测试传输数据和接收响应所需的时间,然后以毫秒为单位显示结果

该命令主要用于验证两台机器之间的连接,即你的机器和你在web服务器上的Python应用程序

2.3.4.5 章节参看:全网最全的Python常见命令大全,建议收藏,以防备用_python基本42个命令-优快云博客

This is Python version 3.1.5 ============================ Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Python Software Foundation. All rights reserved. Python 3.x is a new version of the language, which is incompatible with the 2.x line of releases. The language is mostly the same, but many details, especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Build Instructions ------------------ On Unix, Linux, BSD, OSX, and Cygwin: ./configure make make test sudo make install This will install Python as python3. You can pass many options to the configure script; run "./configure --help" to find out more. On OSX and Cygwin, the executable is called python.exe; elsewhere it's just python. On Mac OS X, if you have configured Python with --enable-framework, you should use "make frameworkinstall" to do the installation. Note that this installs the Python executable in a place that is not normally on your PATH, you may want to set up a symlink in /usr/local/bin. On Windows, see PCbuild/readme.txt. If you wish, you can create a subdirectory and invoke configure from there. For example: mkdir debug cd debug ../configure --with-pydebug make make test (This will fail if you *also* built at the top-level directory. You should do a "make clean" at the toplevel first.) What's New ---------- We try to have a comprehensive overview of the changes in the "What's New in Python 3.1" document, found at http://docs.python.org/3.1/whatsnew/3.1.html For a more detailed change log, read Misc/NEWS (though this file, too, is incomplete, and also doesn't list anything merged in from the 2.7 release under development). If you want to install multiple versions of Python see the section below entitled "Installing multiple versions". Documentation ------------- Documentation for Python 3.1 is online, updated twice a day: http://docs.python.org/3.1/ All documentation is also available online at the Python web site (http://docs.python.org/, see below). It is available online for occasional reference, or can be downloaded in many formats for faster access. The documentation is downloadable in HTML, PostScript, PDF, LaTeX (through 2.5), and reStructuredText (2.6+) formats; the LaTeX and reStructuredText versions are primarily for documentation authors, translators, and people with special formatting requirements. Converting From Python 2.x to 3.x --------------------------------- Python starting with 2.6 will contain features to help locating code that needs to be changed, such as optional warnings when deprecated features are used, and backported versions of certain key Python 3.x features. A source-to-source translation tool, "2to3", can take care of the mundane task of converting large amounts of source code. It is not a complete solution but is complemented by the deprecation warnings in 2.6. See http://docs.python.org/py3k/library/2to3.html for more information. Testing ------- To test the interpreter, type "make test" in the top-level directory. This runs the test set twice (once with no compiled files, once with the compiled files left by the previous test run). The test set produces some output. You can generally ignore the messages about skipped tests due to optional features which can't be imported. If a message is printed about a failed test or a traceback or core dump is produced, something is wrong. On some Linux systems (those that are not yet using glibc 6), test_strftime fails due to a non-standard implementation of strftime() in the C library. Please ignore this, or upgrade to glibc version 6. By default, tests are prevented from overusing resources like disk space and memory. To enable these tests, run "make testall". IMPORTANT: If the tests fail and you decide to mail a bug report, *don't* include the output of "make test". It is useless. Run the failing test manually, as follows: ./python Lib/test/regrtest.py -v test_whatever (substituting the top of the source tree for '.' if you built in a different directory). This runs the test in verbose mode. Installing multiple versions ---------------------------- On Unix and Mac systems if you intend to install multiple versions of Python using the same installation prefix (--prefix argument to the configure script) you must take care that your primary python executable is not overwritten by the installation of a different version. All files and directories installed using "make altinstall" contain the major and minor version and can thus live side-by-side. "make install" also creates ${prefix}/bin/python3 which refers to ${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the same prefix you must decide which version (if any) is your "primary" version. Install that version using "make install". Install all other versions using "make altinstall". For example, if you want to install Python 2.5, 2.6 and 3.0 with 2.6 being the primary version, you would execute "make install" in your 2.6 build directory and "make altinstall" in the others. Issue Tracker and Mailing List ------------------------------ We're soliciting bug reports about all aspects of the language. Fixes are also welcome, preferable in unified diff format. Please use the issue tracker: http://bugs.python.org/ If you're not sure whether you're dealing with a bug or a feature, use the mailing list: python-dev@python.org To subscribe to the list, use the mailman form: http://mail.python.org/mailman/listinfo/python-dev/ Proposals for enhancement ------------------------- If you have a proposal to change Python, you may want to send an email to the comp.lang.python or python-ideas mailing lists for inital feedback. A Python Enhancement Proposal (PEP) may be submitted if your idea gains ground. All current PEPs, as well as guidelines for submitting a new PEP, are listed at http://www.python.org/dev/peps/. Release Schedule ---------------- See PEP 375 for release details: http://www.python.org/dev/peps/pep-0375/ Copyright and License Information --------------------------------- Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Python Software Foundation. All rights reserved. Copyright (c) 2000 BeOpen.com. All rights reserved. Copyright (c) 1995-2001 Corporation for National Research Initiatives. All rights reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum. All rights reserved. See the file "LICENSE" for information on the history of this software, terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES. This Python distribution contains *no* GNU General Public License (GPL) code, so it may be used in proprietary projects. There are interfaces to some GNU code but these are entirely optional. All trademarks referenced herein are property of their respective holders.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值