技术标签: python
例如:
pipenv
好一点?Pipenv是一种工具,旨在将所有包装领域(捆扎机,作曲家,npm,货物,纱线等)中的最佳产品引入Python世界。Windows是我们世界上的头等公民。
它会自动为您的项目创建和管理virtualenv,并Pipfile
在您安装/卸载软件包时从您的软件包中添加/删除软件包。它还会生成非常重要的Pipfile.lock
,用于生成确定性构建。
Pipenv的主要目的是为应用程序的用户和开发人员提供一种简单的方法来设置工作环境。有关库和应用程序之间的区别以及使用setup.py
vsPipfile
定义依赖项的区别,请参见☤Pipfile vs setup.py。
Pipenv试图解决的问题是多方面的:
pip
和virtualenv
分开。他们一起工作。requirements.txt
文件可能会出现问题,因此Pipenv使用Pipfile
和Pipfile.lock
将抽象依赖声明与上次测试的组合分开。$ pipenv graph
.env
文件来简化开发工作流程。建议使用pip3
, mac还是服务器上的Linux一般都会有python2.x
版本。在这种场景大多情况下,pip
指向python2.x
,并非python3.x
pip3 install --upgrade pip
# 推荐使用pip来安装
pip3 install pipenv
其他几种安装方式
# If you’re on MacOS, you can install Pipenv easily with Homebrew:
brew install pipenv
# Or, if you’re using Fedora 28:
sudo dnf install pipenv
# if you're using centos
sudo yum install -y pipenv
反正安装都一样,无论是使用其他的包管理工具还是pip,都可以
# python3 环境创建
pipenv --python 3.x
pipenv --three
pipenv install
# 创建完成后,虚拟环境的pip。并不是你真实环境的pip版本,如果有需要,需要升级一下pip 的版本
python3 -m pip install --upgrade pip
值得一提的是,首先必须得安装了相对应的Python 版本才能创建虚拟环境。可能点萌萌哒。举个栗子,例如我的电脑环境中只有python3.7,而我想创建一个python2.7 这样是不能创建的
Pip3安装的pipenv 只能python3 只能使用
pipenv install packageName
# 安装多个包,中间以空格分隔即可
pipenv install packageName-1 packageName-2 packageNama-3
pipenv uninstall packageName
pipenv shell
- 此时终端会在最前面显示
(xxx)
, xxx一般为项目文件名。证明退出成功- 并不需要关心虚拟环境的具体位置,只需要在当前目录下。有
Pipfile
即可
在其他包的虚拟环境中退出可能是使用deactivate
,在conda 中使用conda deactivate
,而在pipenv 中直接使用
exit
# 即可退出
如果使用deactivate,然后在使用
pipenv shell
,造成本终端页面进入虚拟环境失败。请参考勘误2
在此项目目录下只需一下命令
pipenv --rm
我相信你也和我一样遇见过或尽力过下载第三方包失效包很慢,或者干脆出现timeout
导致第三方包下载失败的情况,那么接下来让我们来使用国内的镜像源进行安装第三方包。速度嗖嗖嗖~
# 以requests 为栗子
pipenv install requests --pypi-mirror https://pypi.tuna.tsinghua.edu.cn/simple/
使用pipenv创建虚拟环境之后会在项目目录下生成Pipfile
,的文件。
# Pipfile
[[source]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
verify_ssl = true
name = "pypi"
[packages]
requests = "*"
[dev-packages]
[requires]
python_version = "3.8"
我们可以使用命令来清晰地呈现出当前安装的 Python 包版本及之间的依赖关系,命令如下:
pipenv graph
~ ProjectNote % pipenv graph
lxml4.6.2
requests2.25.1
- certifi [required: >=2017.4.17, installed: 2020.12.5]
- chardet [required: >=3.0.2,<5, installed: 4.0.0]
- idna [required: >=2.5,️, installed: 2.10]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.2]
有时候可能 Pipfile.lock 文件不存在或被删除了,这时候我们可以使用如下命令生成:
pipenv lock
以上就是pipenv基础使用了,但这还不够哦。接下来让我们深入探究一下
Usage: pipenv [OPTIONS] COMMAND [ARGS]...
Options:
--where Output project home information.
--venv Output virtualenv information.
--py Output Python interpreter information.
--envs Output Environment Variable options.
--rm Remove the virtualenv.
--bare Minimal output.
--completion Output completion (to be executed by the
shell).
--man Display manpage.
--support Output diagnostic information for use in
GitHub issues.
--site-packages / --no-site-packages
Enable site-packages for the virtualenv.
[env var: PIPENV_SITE_PACKAGES]
--python TEXT Specify which version of Python virtualenv
should use.
--three / --two Use Python 3/2 when creating virtualenv.
--clear Clears caches (pipenv, pip, and pip-tools).
[env var: PIPENV_CLEAR]
-v, --verbose Verbose mode.
--pypi-mirror TEXT Specify a PyPI mirror.
--version Show the version and exit.
-h, --help Show this message and exit.
Usage Examples:
Create a new project using Python 3.7, specifically:
$ pipenv --python 3.7
Remove project virtualenv (inferred from current directory):
$ pipenv --rm
Install all dependencies for a project (including dev):
$ pipenv install --dev
Create a lockfile containing pre-releases:
$ pipenv lock --pre
Show a graph of your installed dependencies:
$ pipenv graph
Check your installed dependencies for security vulnerabilities:
$ pipenv check
Install a local setup.py into your virtual environment/Pipfile:
$ pipenv install -e .
Use a lower-level pip command:
$ pipenv run pip freeze
Commands:
check Checks for PyUp Safety security vulnerabilities and against PEP
508 markers provided in Pipfile.
clean Uninstalls all packages not specified in Pipfile.lock.
graph Displays currently-installed dependency graph information.
install Installs provided packages and adds them to Pipfile, or (if no
packages are given), installs all packages from Pipfile.
lock Generates Pipfile.lock.
open View a given module in your editor.
run Spawns a command installed into the virtualenv.
scripts Lists scripts in current environment config.
shell Spawns a shell within the virtualenv.
sync Installs all packages specified in Pipfile.lock.
uninstall Uninstalls a provided package and removes it from Pipfile.
update Runs lock, then sync.
场景如下:
假设正式环境中为一个干净的仓库,有且仅有初始的包
虚拟环境中有所需要的第三方包。
如何实现在真实环境中使用虚拟环境中的第三方包并运行
pipenv run python xxx.py
有时候可能 Pipfile.lock 文件不存在或被删除了,这时候我们可以使用以下命令生成:
pipenv lock
部署的时候只需要将此执行一下命令即可安装所有的依赖包,它是依靠Pipfile.lock的
pipenv sync
只说不做假把式,只做不说傻把式。搞了这么多,来个case实现与项目接轨吧。这里为以docker部署为栗子
首先在项目中我们也使用虚拟环境去开发,当然也建议这样去做。没错,我是在教你做事。嘻嘻~
此时的项目应该差不多如下,此时两个为必须存在一个是Pipfile
, 一个是项目文件(文件夹)。deploy
,Dockerfile
为后实现
书写Dockerfile,拿docker部署没有Dockerfile是不阔能滴
Dockerfile实现如下,可以按需修改
FROM python:3.8
COPY . /code
WORKDIR /code
RUN sh deploy.sh
CMD ["pipenv", "run", "python3", "testfile.py"]
Deploy.sh如下
其实deploy 其中的内容可以直接放到dockerfile中,我自己喜欢这样。更加清晰一点。啊,如此清晰的逻辑与结构,无敌~。我又在教你做事,大哥别杀我
# 保持pip版本为最新版,及安装pipenv
python3 -m pip install --upgrade pip && pip3 install pipenv
# 创建虚拟环境
pipenv --python 3.8
# 安装环境依赖(第三方包)
pipenv update
这里需要注意的是,我建议你使用
pipenv update
,更加保险。什么, 你问我为什么不用sync?
既然你诚心诚意的发问了,那我就大发慈悲的告诉你吧。哈哈~
pipenv update
相当于执行了pipenv lock
和pipenv sync
两个命令如果用
pipenv sync
,而此时的你如果没有Pipfile.lock
,那岂不是很尬
Docker build 执行结果如下
Sending build context to Docker daemon 99.33kB
Step 1/5 : FROM python:3.8
---> d1bfb3dd9268
Step 2/5 : WORKDIR /code
---> Running in 74cda17b1483
Removing intermediate container 74cda17b1483
---> ecfd46d28538
Step 3/5 : COPY . /code
---> 8a89f329a4f9
Step 4/5 : RUN pip install pipenv && sh deploy.sh
---> Running in cea95051d481
Collecting pipenv
Downloading pipenv-2020.11.15-py2.py3-none-any.whl (3.9 MB)
Requirement already satisfied: setuptools>=36.2.1 in /usr/local/lib/python3.8/site-packages (from pipenv) (51.0.0)
Requirement already satisfied: pip>=18.0 in /usr/local/lib/python3.8/site-packages (from pipenv) (20.3.3)
Collecting virtualenv-clone>=0.2.5
Downloading virtualenv_clone-0.5.4-py2.py3-none-any.whl (6.6 kB)
Collecting certifi
Downloading certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
Collecting virtualenv
Downloading virtualenv-20.2.2-py2.py3-none-any.whl (5.7 MB)
Collecting appdirs<2,>=1.4.3
Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting distlib<1,>=0.3.1
Downloading distlib-0.3.1-py2.py3-none-any.whl (335 kB)
Collecting filelock<4,>=3.0.0
Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting six<2,>=1.9.0
Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: six, filelock, distlib, appdirs, virtualenv-clone, virtualenv, certifi, pipenv
Successfully installed appdirs-1.4.4 certifi-2020.12.5 distlib-0.3.1 filelock-3.0.12 pipenv-2020.11.15 six-1.15.0 virtualenv-20.2.2 virtualenv-clone-0.5.4
Creating a virtualenv for this project...
Pipfile: /code/Pipfile
Using /usr/local/bin/python3.8 (3.8.6) to create virtualenv...
⠦ Creating virtual environment...created virtual environment CPython3.8.6.final.0-64 in 1079ms
creator CPython3Posix(dest=/root/.local/share/virtualenvs/code-_Py8Si6I, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==20.3.1, setuptools==51.0.0, wheel==0.36.1
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/code-_Py8Si6I
Installing dependencies from Pipfile.lock (d2a522)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
All dependencies are now up-to-date!
Removing intermediate container cea95051d481
---> 5bc79a1c17b6
Step 5/5 : CMD ["pipenv", "run", "python3", "testfile.py"]
---> Running in d86af926715c
Removing intermediate container d86af926715c
---> a64b6bc63353
Successfully built a64b6bc63353
Successfully tagged test:1
(ProjectNote) stringle-004@zhixiankeji-004s-MacBook-Pro ProjectNote % docker run test:1
<Response [200]>
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 clabg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao1 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.bacom name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tlogin class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (windowocation.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> <tml>
<Element html at 0x7fbfe776d100>
Docker 构建注意事项:
# Pipfile文件
[requires]
python_version = "3.8"
# dockerfile
FROM python:3.8
这两个后面跟的版本号必须一致,否则将会构建失败
使用pipenv update
而不是pipenv sync
,为什么,我就不告诉你了。看上面就好
还记得我之前在服务器上,无论怎么安装就是安装不上,无论是其他包还是pipenv
.后来硬是找不到什么原因。直到。。。
# 查看pip 版本
pip3 -V
# or
pip3 --version
输出结果类似如下
~ % pip3 -V pip 20.3.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
如果pip 的版本还不是20版本的,需要进行升级
# 推荐
python3 -m pip install --upgrade pip [--user]
# 或者
python3 -m pip install -U pip [--user]
# 不推荐
pip3 install --upgrade pip
pip3 install -U pip
其中[–user] 为可选参数,最好加上。一般情况下不加也没事
-m
:run library module as a script (terminates option list) 将库模块作为脚本运行(终止选项列表)
之前使用其他的python虚拟环境工具的时候,使用deactivate
,退出虚拟环境。而pipenv
, 并不是这样
使用deactivate
,退出虚拟环境后又使用`pipenv shell, 进入虚拟环境,结果显示…
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.
# 先exit 一下即可,然后使用
pipenv shell
# 即可重新进入虚拟环境
创建虚拟环境需注意
pipenv 并不支持嵌套的虚拟环境,默认使用此文件的父级目录中的pipfile
比如:在projectfile中有文件src-1,pipfile, src-2,如果进入src-1 中执行
pipenv install
orpipenv --python 3.8
orpipenv tree
,中任何一个命令都会优先使用projectfile/pipfile
文章浏览阅读2.5w次,点赞6次,收藏50次。官方解释是,docker 容器是机器上的沙盒进程,它与主机上的所有其他进程隔离。所以容器只是操作系统中被隔离开来的一个进程,所谓的容器化,其实也只是对操作系统进行欺骗的一种语法糖。_docker菜鸟教程
文章浏览阅读5.7k次,点赞3次,收藏14次。该如何避免的,今天小编给大家推荐两个下载Windows系统官方软件的资源网站,可以杜绝软件捆绑等行为。该站提供了丰富的Windows官方技术资源,比较重要的有MSDN技术资源文档库、官方工具和资源、应用程序、开发人员工具(Visual Studio 、SQLServer等等)、系统镜像、设计人员工具等。总的来说,这两个都是非常优秀的Windows系统镜像资源站,提供了丰富的Windows系统镜像资源,并且保证了资源的纯净和安全性,有需要的朋友可以去了解一下。这个非常实用的资源网站的创建者是国内的一个网友。_msdn我告诉你
文章浏览阅读1.2k次。vue2封装对话框el-dialog组件_
文章浏览阅读4.7k次,点赞5次,收藏6次。MFC 文本框换行 标签: it mfc 文本框1.将Multiline属性设置为True2.换行是使用"\r\n" (宽字符串为L"\r\n")3.如果需要编辑并且按Enter键换行,还要将 Want Return 设置为 True4.如果需要垂直滚动条的话将Vertical Scroll属性设置为True,需要水平滚动条的话将Horizontal Scroll属性设_c++ mfc同一框内输入二行怎么换行
文章浏览阅读832次。检查Linux是否是否开启所需端口,默认为6379,若未打开,将其开启:以root用户执行iptables -I INPUT -p tcp --dport 6379 -j ACCEPT如果还是未能解决,修改redis.conf,修改主机地址:bind 192.168.85.**;然后使用该配置文件,重新启动Redis服务./redis-server redis.conf..._redis-server doesn't support auth command or ismisconfigured. try
文章浏览阅读4.9k次。济大数电实验报告_数据选择器及其应用
文章浏览阅读236次。1研究内容消费在生产中占据十分重要的地位,是生产的最终目的和动力,是保持省内经济稳定快速发展的核心要素。预测河南省社会消费品零售总额,是进行宏观经济调控和消费体制改变创新的基础,是河南省内人民对美好的全面和谐社会的追求的要求,保持河南省经济稳定和可持续发展具有重要意义。本文建立灰色预测模型,利用MATLAB软件,预测出2019年~2023年河南省社会消费品零售总额预测值分别为21881...._灰色预测模型用什么软件
文章浏览阅读1.2k次。12.4-在Qt中使用Log4Qt输出Log文件,看这一篇就足够了一、为啥要使用第三方Log库,而不用平台自带的Log库二、Log4j系列库的功能介绍与基本概念三、Log4Qt库的基本介绍四、将Log4qt组装成为一个单独模块五、使用配置文件的方式配置Log4Qt六、使用代码的方式配置Log4Qt七、在Qt工程中引入Log4Qt库模块的方法八、获取示例中的源代码一、为啥要使用第三方Log库,而不用平台自带的Log库首先要说明的是,在平时开发和调试中开发平台自带的“打印输出”已经足够了。但_log4qt
文章浏览阅读786次。全局观思维模型,一个教我们由点到线,由线到面,再由面到体,不断的放大格局去思考问题的思维模型。_计算机中对于全局观的
文章浏览阅读330次。一、CountDownLatch介绍CountDownLatch采用减法计算;是一个同步辅助工具类和CyclicBarrier类功能类似,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成。二、CountDownLatch俩种应用场景: 场景一:所有线程在等待开始信号(startSignal.await()),主流程发出开始信号通知,既执行startSignal.countDown()方法后;所有线程才开始执行;每个线程执行完发出做完信号,既执行do..._countdownluach于cyclicbarrier的用法
文章浏览阅读508次。Prometheus 算是一个全能型选手,原生支持容器监控,当然监控传统应用也不是吃干饭的,所以就是容器和非容器他都支持,所有的监控系统都具备这个流程,_-自动化监控系统prometheus&grafana实战
文章浏览阅读4.7k次。输入关键字,可以通过键盘的搜索按钮完成搜索功能。_react search