Home > Article > Backend Development > Lazy man's artifact! A creative Python command line tool
When we hear some people say that the xx library is very easy to use, we always can't help but want to try it ourselves.
There are some libraries. The reason why they are easy to use is that some libraries have been sealed at a higher level. If you install this library, it will also install n multiple dependent libraries. For example, streamlit introduced in the previous article For example, there are as many as 90 dependent packages?
It’s better than Baidu’s Family Bucket…
It’s precisely because I’m afraid of contaminating my global Python environment that I usually use venv to create it when trying out new packages. Virtual environment, then install it, and then clean up the virtual environment.
Have you noticed that the whole process is actually quite troublesome.
Just last night, I was browsing on Github and found a library that can solve my long-standing troubles.
This library is called trypackage, a library created for trial libraries.
What does the try xx command do when you use it?
# 1. How to install
You can install it with one command
python3 -m pip install trypackage
You can see that trypackage only relies on a click command line library, which is still very lightweight
In fact, this is not the case. try will create a virtual environment based on virtualenv, so you also need to install virtualenv and virtualenvwrapper
python3 -m pip install virtualenv virtualenvwrapper
# 2. Basic use
Use try requests, try will create a virtual environment, and then install requests in the virtual environment. After the installation is completed, it will automatically enter the Python Shell mode and automatically import the package you want to try
The most considerate thing is that after the trial, exit the Python Shell and the virtual environment will be automatically cleaned up.
# 3. More usage
Specify the python version
My environment is not installed Python 3.8, through the -p parameter, you can specify Python 3.8. The installation process is too fast, so fast that I doubt whether this is really installing the Python 3.8 interpreter.
You can also directly specify the local Python version, for example
try requests -p /usr/bin/python3.7.1
Specify the running mode
Try has many direct running modes
1. When you do not specify it, the python shell mode is used by default
# 二者等价 try requests try requests --shell python
2. Use ipython There are two ways to specify
# 二者等价 try requests --ipython try requests --shell ipython
3. Use ptpython or ptipython
Both modes have code prompts With the auto-completion function
4, using bpython mode
Bpython’s code prompts and completion are more efficient than those of ptpython and ptipython. It's stronger, but I haven't tried the other differences yet.
Open in the editor
try requests --editor
Specify Github warehouse package
# 语法示例 try <user>/<repo> # 安装 Github 上的 Kenneth/requests 上的 master 版本 try kennethreitz/requests
Specify an existing virtual environment
try requests --virtualenv ~/.try/sandbox
Persistent virtual environment
By default, the virtual environment will be automatically cleaned up after you exit. If you have special needs, you can specify the --keep parameter to persist the virtual environment
Specify the directory of the virtual environment
默认情况下,try 会将虚拟环境创建在一个默认的目录中,这个目录比较深,不容易记住,你可以指定 --tmpdir 参数,告诉 try 要将虚拟环境创建在这里,这个参数对于想要持久化虚拟环境的人会很有用。
try requests --tmpdir ~/.try
# 4. 配置文件
try 虽然提供命令行入参的方式来识别用户选项,但对于一些用户来说,更希望能一次性修改 try 的默认选项,而不用每次都指定多个参数。
这时候,可以在你的 APP 目录下新增一个 config.ini 文件,内容模板如下
[env] virtualenv=~/.try/sandbox python=3.8 shell=ipython keep=false always_use_editor=false tmpdir=~/.try
所谓的 APP 目录,在不同的系统中是不一样的,可以使用如下代码来查看
>>> import click >>> click.get_app_dir("try") '/Users/iswbm/Library/Application Support/try' >>>
有了 config.ini,再次 try ,就会发现效果与配置一样的预期一致。
# 5. 总结一下
Try 是一个非常实用的命令行工具,功能可以说完全击中我的痛点,能把这样一个库做出来,真的是创意十足,再一次印证 Python 是懒人第一语言 。。
The above is the detailed content of Lazy man's artifact! A creative Python command line tool. For more information, please follow other related articles on the PHP Chinese website!