Python学习日记(一)------------Using the Python Interpreter

1. Invoking the Interpreter

type 'python' command in DOS box.

To add the installation directory to user path, by typing set path = %path%;C:\python27. But if you restart the cmd window, you need to do this again.

2.Quiting the Interpreter

quit() or Control-Z on Windows, Control-D on Unix

3.Argument Passing

import sys

sys.argv[0]

4.Source Code Encoding

# -*- coding: encoding -*-

With that declaration, all characters in the source file will be treated as having the encoding encoding, and it will be possible to directly write Unicode string literals in the selected encoding.

5. The Interactive Startup File

When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. You can do this by setting an environment variable named PYTHONSTARTUP to the name of a file containing your start-up commands.

and if you want to use the start up file in a script, you must do this explicitly in the script

import os

filename = os.environ.get('PYTHONSTARTUP')

if filename and os.path.isfile(filename):

      execfile(filename)

 

6. The Customization Modules

Python provides two hooks to let you customize it: sitecustomize and usercustomize.

import site

site.getusersitepackages()

than create or modify file usercustomize.py int the directory.

 

ps:tty: 设备终端 Unix-style line ending ('\n'); Window line ending('\r\n')

.pyw, in this case , the console window that normally appears is suppressed.

 

### Python get-pip.py 下载和使用方法 #### 1. 下载 `get-pip.py` 脚本 可以通过多种方式获取 `get-pip.py` 文件: - **通过 `curl` 命令下载** 使用 `curl` 工具可以从网络上直接下载脚本文件到本地。执行以下命令完成下载: ```bash curl -sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py ``` 此处 `-sS` 参数表示静默模式但仍显示错误信息,而 `-L` 表示跟随重定向[^1]。 - **通过浏览器手动下载** 打开链接 [https://bootstrap.pypa.io/get-pip.py](https://bootstrap.pypa.io/get-pip.py),将页面中的内容保存为名为 `get-pip.py` 的文件[^4]。 - **针对旧版 Python(如 Python 2.7)** 如果需要为 Python 2 安装兼容的 `pip` 版本,则应使用专门支持 Python 2 的 URL 地址: ```bash curl -o get-pip.py https://bootstrap.pypa.io/pip/2.7/get-pip.py ``` #### 2. 运行 `get-pip.py` 脚本 下载完成后,需运行此脚本来安装或升级 `pip`: - **基本运行命令** 对于默认解释器(通常是系统中优先级最高的 Python 解释器),可以这样运行脚本: ```bash python get-pip.py ``` - **指定特定版本的 Python** 若要为目标版本的 Python 安装对应的 `pip`,则显式调用对应解释器。例如: - 针对 Python 3.x: ```bash python3 get-pip.py ``` - 针对 Python 2.7: ```bash sudo python2 get-pip.py ``` 成功安装后可通过以下命令验证版本号以及确认其关联的具体 Python 版本: ```bash pip --version ``` 或者对于 Python 3 用户可能还需要测试: ```bash pip3 --version ``` 注意,在某些环境中,如果存在多个 Python 版本共存的情况,建议始终明确指明使用的 Python 版本以避免冲突[^2]。 #### 3. 处理常见问题 - 若发现无法找到已安装的 `pip` 或者报错提示权限不足等问题时,尝试加上超级用户权限前缀 `sudo` 来重新操作。 - 当遇到因网络原因导致下载失败的情形下,可考虑更换镜像站点或其他可靠资源路径再试次。 --- ### 示例代码片段 以下是完整的流程演示: ```bash # Step A: Download the script using curl. curl -sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py # Step B: Execute it with your desired version of Python interpreter. python3 get-pip.py # Verification step after installation completes successfully. pip3 --version ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值