HBPythonOperationEnv - 101camp/playground GitHub Wiki

背景

  • 越来越多.ipynb 形式,有怼员直觉的想尝试一切运行都在 Jupyter 中, 将 代码文本/系统环境/运行时环境/交互环境/... 各种 Python 可以运行的环境当成同一类对象了.

意义

了解 python 运行环境, 并知晓在开发和运行时分别选用什么环境, 可快速推进开发效率.

现象

  • 现象1
    • atl4dama 大妈在 ipynb 中开发完了整个脚本. 其中包括读取文件, 并操作.
    • 我git pl 仓库后, 打开大妈的 .ipynb文件, 直接 shift + enter, 诶? 报错显示文件不存在
  • 现象2
    • 直接复制大妈的系列管道命令: ༄ find ../../raw/zoomquiet/weekly/*.csv | xargs -I{} python zq7SET4.py {} > atl2SET4dama_all.csv
    • 报错说文件夹不存在?
  • 现象3
    • 命令行直接运行含有import matplotlib.pyplot as plt内容的 .py 文本, 报错 命令行不能显示图片.

python 运行环境

软件运行环境的层次

当我们从零开始一个软件项目的时候,会考虑的问题有:

  • 需要什么样的服务器?

    • 系统:Win or Linux
    • 配置:CPU 16核 8核?内存 16G?
    • 虚拟还是实体小机
    • 用途是什么?用于跑应用还是放数据库等等
  • 需要什么样的权限配置?

    • 一般不给Root用户
  • 需要什么样的网络配置?

    • 是否需要访问外网,需要访问哪些地址
    • 是否需要外网访问,开放哪些端口
  • 需要什么样的(服务器架构)拓扑结构

    • 是否需要进行负载均衡
    • 和其他服务器的联通关系
  • 服务器上需要安装的应用或者依赖包?

这是我们普遍理解的运行环境。需要注意的是,以上的问题一般只有在生产环境申请服务或者部署应用的时候才会问的这么细,如果是开发环境申请服务器,基本上只要搞清楚网络配置,剩下的都交给开发自己搞。因为开发人员会有各种自己需要的东西的安装,甚至做一些技术的尝试和创新等等。

以上的部分应该作为广义的软件运行环境下的比较偏向操作系统、网络环境的层次

Python 程序运行在哪儿

在前面的运行环境的问题都搞定后,才是到安装python。当我们在shell 里输入 python XXX.py 的时候,首先shell 会去环境变量对应的地址去寻找这一条命令如何执行,找到了python的可执行程序后,才知道如何理解这一条命令。之后才会根据python 的方式执行py脚本。其实执行的时候也会先通过解释器把py脚本做一轮解释,之后才继续执行。

python和操作系统的关系,在写脚本的时候感受不深,不过15.1. os — Miscellaneous operating system interfaces这里就会有感觉,通过os的包的函数,获取到操作系统层面的一些信息,甚至执行相关的操作。如果利用shell交互,则会在操作系统和python程序间通过shell进行传递。

Python 运行环境及类别

  • 1.命令行:用文本编辑器写好 .py 为扩展名的文本文件,打开命令行窗口,把当前目录切换到 .py 文件所在目录,然后用 python 解释器执行。terminal cli python .py
xxx(path)$ python xxx.py
    1. 交互环境运行: 使用 python 自带的 IDLE(terminal cli python)
$ python
>>> 
>>> 
...
...
    1. 集成开发环境(IDE)
    • IPython: 基于CPython之上的一个交互式解释器, Cpython 是官方版本.
$ cd xxx(path)
$ jupyter notebook

or

$ jupyter notebook
in1: cd
ot1: xxx(path)

同样需要在当前脚本所在的路径下, 才可以执行当前脚本.

选择哪种环境(操作)

  • 不同的运行时环境都分别解决了什么问题?
  • 开发和运行环境是相同的嘛?
  • 当前, 哪种运行环境最适合我们的日常自怼?

仍以 DebugUself/du4proto at atl4dama 为例, 回答以上问题.

  • 开发: 在 3.集成开发环境(IDE)IPython 中进行尝试, 因为所见即所得, 大量减少输入 print() 的时间.
  • 形成连续代码后, 另存为 .py, 保存在固定文件夹, 并撰写调用文档.
  • 运行: 1. 命令行 cd 进 .py脚本所在文件夹, $ python .py
                  /> .md(doc/)
    .ipynb(ipynb/) -> .py(try/) -> moudle(src/) -> readme.md
         ^                             |
         +-----------------------------+    
  • 以上本地的 .ipynb 和 .py 脚本 会推送到远程仓库, 生成的最终 .py脚本 沉淀在 src/ 文件夹里面. 期间过程中的 .csv 或 .html 文件不应上传到远程仓库. 原因及过程在这里: HbUsageRepositoryFile · DebugUself/du4proto Wiki

  • 另: 2.交互式环境 可以使用 os 模块, 与系统交互时, 直接使用.

附录

名词解释

symlink: In terminal, you would often see path like /usr/local/bin/, which is a symlink, similar to a shortcut path in Windows. You can not see the path in folders, because it is a virtual path, and once you remove the files in the real folder,the symlink would disappear. 相当于Window系统下的快捷方式

shell:Comparative to core, which means funciton of interacting with users. At the command line, environmental variables are defined for the current shell and become inherited by any running command or process. 在Hitchhiker's Guide中,提到shell (also called a terminal or console)是同一种东西,这里就是指你看到的终端或命令行。

IDE: With tight-knit components with similar user interfaces. Reduce the configuration necessary to piece together multiple development utilities. Speed learning a new programming language and its associatited libraries. One IDE for Python is Ipython: parallel appliactions to be developed, executed, debugged and monitored interactively.

Jupyter Notebook: The notebook component in Ipython is spinned-off and became a new program called Jupyter, with other language such as R, Ruby supported.

IDLE: Intended to be a simple IDE and suitable for beginners, especially in an educational environment.

pyenv: python版本管理的工具

pip: A package manager that facilitates installation, upgrade, and uninstallation of python packages. It also works with virtual python environments. 安装、升级、卸载python包的管理工具。如何找到你装过的package在哪里? pip show --files SomePackage,location一栏可以看到文件路径。

pipenv: A dependency manager for Python projects,recommended as it’s a higher-level tool that simplifies dependency management for common use cases. Pipenv manages dependencies on a per-project. Pipenv is a project that aims to bring the best of all packaging worlds to the Python world. It harnesses Pipfile, pip, and virtualenv into one single toolchain. It features very pretty terminal colours. Pipenv的优势,可以看官方介绍

Dependency: Application dependencies are the libraries other than your project code that are required to create and run your application. They are installed separately from system-level packages to prevent library version conflicts.

Virtualenv: A tool to create isolated Python environments. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

PyPi(Python Package Index):A central location where Python libraries are stored,类似有官方索引的packages

homebrew: A package manager,用Homebrew安装Python的好处:easier to keep your Python installation up to date

参考

增补

Changeling

⚠️ **GitHub.com Fallback** ⚠️