推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pyengwoei
V2EX  ›  Python

Python 代码移植到服务器以后 模块都要重新安装一遍?

  •  1
     
  •   pyengwoei · Sep 26, 2016 · 6678 views
    This topic created in 3551 days ago, the information mentioned may be changed or developed.
    本地 Pycharm 开发 用 PIP 安装了几十个模块以后,移植到服务器这些模块要重新安装吗
    30 replies    2016-10-08 15:40:49 +08:00
    congeec
        1
    congeec  
       Sep 26, 2016
    需要,以后你还需要服务器代码的版本控制
    用 virtualenv 或 docker 吧
    pyengwoei
        2
    pyengwoei  
    OP
       Sep 26, 2016
    @congeec 好的谢谢了,因为我才接触 Python , 接受一个项目,但是这个项目以前是没有用 PIP 安装模块的,是直接通过: parent_dir = os.path.abspath(os.path.dirname(__file__))
    vendor_dir = os.path.join(parent_dir, 'vendor') 这样把模块弄在工程目录一起的
    ztyoung
        3
    ztyoung  
       Sep 26, 2016
    需要。
    可以开发的时候使用 virtualenv ,那样可以将模块一起移植过去。
    araraloren
        4
    araraloren  
       Sep 26, 2016
    ~~是迁移吧,移植不是这个意思
    pyengwoei
        5
    pyengwoei  
    OP
       Sep 26, 2016
    @ztyoung 意思是把 site-packages 里面的模块文件 全部拷贝到另外一台服务器的对应目录?
    felixonmars
        6
    felixonmars  
       Sep 26, 2016
    @pyengwoei 有用到系统库等外部资源的话,除非所有环境的系统版本都一致,否则建议重建 virtualenv 。
    pyengwoei
        7
    pyengwoei  
    OP
       Sep 26, 2016
    @felixonmars 只要是系统版本一样,就可以直接拷贝?
    felixonmars
        8
    felixonmars  
       Sep 26, 2016
    @pyengwoei 包括用到的包、包的来源。不能确定的话,从同一个模板用同样的步骤装包总是没错的……
    pyengwoei
        9
    pyengwoei  
    OP
       Sep 26, 2016
    @felixonmars 我是把 site-packages 这个目录里面所有的文件打包出来的
    bcllemon
        10
    bcllemon  
       Sep 26, 2016   ❤️ 1
    可以用 pipreqs 生成依赖文件,到服务上使用 virtualenv 创建虚拟环境,然后使用 pip install -r requirements.txt 进行批量安装
    pyengwoei
        11
    pyengwoei  
    OP
       Sep 26, 2016
    @bcllemon 这个看起来 不错
    pyengwoei
        12
    pyengwoei  
    OP
       Sep 26, 2016
    @bcllemon 还有一个小白问题,直接用 PIP 安装模块,和去 site-packages 目录里面直接拷贝需要的模块到另外一个环境 应该不行吧
    jy01264313
        13
    jy01264313  
       Sep 26, 2016
    写一个 requirements 文件吧,直接 pip install -r requirements
    bwangel
        14
    bwangel  
       Sep 26, 2016   ❤️ 1
    如果开发环境和服务器环境相同的话(例如我的都是 Ubuntu 16.04 ),那么本地使用 virtualenv , virtualenv 有一个--aloways-copy 选项,会把相关的C动态库什么的都复制过来,部署的时候直接把那个 virtualenv 复制上去就好了!

    如果如果开发环境和服务器环境不同的话(例如Mac和 CentOS ),那么有两种办法:

    1.本地先装好一个 Docker ,然后直接把 Docker 复制到服务器上去。(本地网络毕竟比服务器快,操作方便),或者写好 Docker file ,在服务器上 Build

    2. 在本地通过 pip freeze 生成一个 requirements ,在服务器上装相关 pip 包
    bwangel
        15
    bwangel  
       Sep 26, 2016
    对了, Pycharm 的项目设置里,可以设置 Python 解释器,如果使用 virtualenv 的话,记得设置上 virtualenv 中的 Python 。
    xvx
        16
    xvx  
       Sep 26, 2016 via Android   ❤️ 1
    我刚用 Pycharm ,这货好像就有类似 virtualenv 的功能吧,创建项目的时候可以选择的。
    qweweretrt515
        17
    qweweretrt515  
       Sep 26, 2016   ❤️ 1
    docker
    aiver
        18
    aiver  
       Sep 26, 2016   ❤️ 1
    pip freeze 生成 requirements 文件,然后服务器上 pip -r requirements 安装即可,但一般要大版本一致,如 2.7+版本或 3.3+版本;或, Docker 。
    alphadog619
        19
    alphadog619  
       Sep 26, 2016
    部署后 服务器会按照 requirements.txt 安装和开发环境一样版本的各个库
    pyengwoei
        20
    pyengwoei  
    OP
       Sep 26, 2016
    @bwangel 这下基本明白了 TY
    pyengwoei
        21
    pyengwoei  
    OP
       Sep 26, 2016
    @aiver ok 明白了 谢谢
    pyengwoei
        22
    pyengwoei  
    OP
       Sep 26, 2016
    好的,我找下
    kenfor
        23
    kenfor  
       Sep 26, 2016
    必须重新安装。 Linux 的话直接用 pip 安装就好了
    skywatcher
        24
    skywatcher  
       Sep 26, 2016
    最近也在考虑这个问题,虽然可以用 virtualenv 生成和导入 requirements.txt ,但是还是会出现对一些系统模块的依赖,到新的机器上又得全部 yum ( centos )重新安装一遍,还是麻烦;等 python 环境的打包、发布模块完成再来回答这个问题吧。
    bcllemon
        25
    bcllemon  
       Sep 27, 2016
    @pyengwoei site-packages 里面有些是和环境相关的,除非你本地开发环境和服务器环境 100%一样,否则很大可能出现各种未知错误.
    pyengwoei
        26
    pyengwoei  
    OP
       Sep 29, 2016
    @skywatcher 这模块是你自己开发?
    pyengwoei
        27
    pyengwoei  
    OP
       Sep 29, 2016
    @bcllemon 哎 看来对新手来说,老实安装 还是最低成本的了
    skywatcher
        28
    skywatcher  
       Sep 29, 2016
    @pyengwoei 在考虑中
    fhefh
        29
    fhefh  
       Oct 4, 2016
    mark~~
    linruiyi
        30
    linruiyi  
       Oct 8, 2016
    传统方式,写一个 requirements.txt 文件,然后一次性安装。可以尝试一下 docker 。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1115 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 18:18 · PVG 02:18 · LAX 11:18 · JFK 14:18
    ♥ Do have faith in what you're doing.