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?
- Install the specified version of the Python interpreter
- Create a temporary virtual environment
- Activate the virtual environment
- Install in the virtual environment you want to try Python library
- Directly enter Python Shell mode
- Automatically import the library you want to try
# 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The top three top ten free market viewing software in the currency circle are OKX, Binance and gate.io. 1. OKX provides a simple interface and real-time data, supporting a variety of charts and market analysis. 2. Binance has powerful functions, accurate data, and is suitable for all kinds of traders. 3. gate.io is known for its stability and comprehensiveness, and is suitable for long-term and short-term investors.

The most suitable platforms for trading Meme coins include: 1. Binance, the world's largest, with high liquidity and low handling fees; 2. OkX, an efficient trading engine, supporting a variety of Meme coins; 3. XBIT, decentralized, supporting cross-chain trading; 4. Redim (Solana DEX), low cost, combined with Serum order book; 5. PancakeSwap (BSC DEX), low transaction fees and fast speed; 6. Orca (Solana DEX), user experience optimization; 7. Coinbase, high security, suitable for beginners; 8. Huobi, well-known in Asia, rich trading pairs; 9. DEXRabbit, intelligent

Cryptocurrency data platforms suitable for beginners include CoinMarketCap and non-small trumpet. 1. CoinMarketCap provides global real-time price, market value, and trading volume rankings for novice and basic analysis needs. 2. The non-small quotation provides a Chinese-friendly interface, suitable for Chinese users to quickly screen low-risk potential projects.

The reliable and easy-to-use virtual currency exchange apps are: 1. Binance, 2. OKX, 3. Gate.io, 4. Coinbase, 5. Kraken, 6. Huobi Global, 7. Bitfinex, 8. KuCoin, 9. Bittrex, 10. Poloniex. These platforms were selected as the best for their transaction volume, user experience and security, and all offer registration, verification, deposit, withdrawal and transaction operations.

When choosing a digital currency trading platform suitable for beginners, you need to consider security, ease of use, educational resources and cost transparency: 1. Priority is given to platforms that provide cold storage, two-factor verification and asset insurance; 2. Apps with a simple interface and clear operation are more suitable for beginners; 3. The platform should provide learning tools such as tutorials and market analysis; 4. Pay attention to hidden costs such as transaction fees and cash withdrawal fees.

The top ten digital virtual currency apps are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

Recommended apps for the top ten virtual currency viewing platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi, 5. Coinbase, 6. Kraken, 7. Bitfinex, 8. KuCoin, 9. Bybit, 10. Bitstamp, these platforms provide real-time market trends, technical analysis tools and user-friendly interfaces to help investors make effective market analysis and trading decisions.

The core advantage of gate.io (global version) is that the interface is minimalist, supports Chinese, and the fiat currency trading process is intuitive; Binance (simplified version) has the highest global trading volume, and the simple version model only retains spot trading; OKX (Hong Kong version) has the simple version of the interface is simple, supports Cantonese/Mandarin, and has a low threshold for derivative trading; Huobi Global Station (Hong Kong version) has the core advantage of being an old exchange, launches a meta-universe trading terminal; KuCoin (Chinese Community Edition) has the core advantage of supporting 800 currencies, and the interface adopts WeChat interaction; Kraken (Hong Kong version) has the core advantage of being an old American exchange, holding a Hong Kong SVF license, and the interface is simple; HashKey Exchange (Hong Kong licensed) has the core advantage of being a well-known licensed exchange in Hong Kong, supporting France
