推荐学习书目
› 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
xielongen
V2EX  ›  Python

pycharm 如何禁止使用 setup 安装的包而引用项目中的代码运行

  •  
  •   xielongen · Jul 17, 2017 · 4368 views
    This topic created in 3400 days ago, the information mentioned may be changed or developed.

    使用 python setup 安装了自己开发的包,但是接下来 import 到的都是安装的包,无法使用项目中修改代码后的包。希望安装的包给其它人用,而自己开发时屏蔽掉。求解决这个问题的最佳实践

    28 replies  •  2017-07-21 07:36:52 +08:00
    whileFalse
    whileFalse
    Jul 17, 2017
    1
    import json

    import ".json"

    我记得是这样
    whileFalse
    whileFalse
    Jul 17, 2017
    2
    try:
    import '.mypackage'
    except:
    import mypackage

    我是这个意思
    xielongen
    xielongen
    OP
    Jul 17, 2017
    3
    我不希望修改代码,这样需要修改很多地方
    lights
    lights
    Jul 17, 2017 via iPhone
    4
    好像有一个 project setting ?可以设置当前项目的 sdk 之类的,应该能解决楼主的问题吧
    mkeith
    mkeith
    Jul 17, 2017❤️1
    5
    virtualenv 使用不同的环境呢
    xielongen
    xielongen
    OP
    Jul 17, 2017
    6
    @mkeith 这样需要搞两个 env,还有同步的维护问题
    xielongen
    xielongen
    OP
    Jul 17, 2017
    7
    @lights sdk, 那不是 java 的东西吗
    walleL
    walleL
    Jul 17, 2017❤️1
    9
    1. 使用 pip install -e 安装
    2. 不安装,在项目目录下做一个链接
    xielongen
    xielongen
    OP
    Jul 17, 2017
    10
    @walleL 我希望安装,但是在 pycharm 中不使用
    edsion996
    edsion996
    Jul 17, 2017
    11
    楼主是来钓鱼的么,我怎么感觉已经把所有可行的方案都给否定了。。。
    tempdban
    tempdban
    Jul 17, 2017
    12
    import sys
    sys.path.insert( 1, "your path" )
    2929899530pgy
    2929899530pgy
    Jul 17, 2017
    13
    厉害,支持
    mkeith
    mkeith
    Jul 17, 2017
    14
    两个 env 要同步什么啊,直接 pip install -r requirement.txt
    xielongen
    xielongen
    OP
    Jul 17, 2017
    15
    @zjb861107 我大型项目经验较少,也大概能猜到一些解决方案,但是 pythoner 不就是得追求简洁可靠的解决方案嘛!
    xielongen
    xielongen
    OP
    Jul 17, 2017
    16
    @tempdban 这个也是要修改很多的代码
    xielongen
    xielongen
    OP
    Jul 17, 2017
    17
    @zjb861107 肯定还有其它的方案,例如设置 usercustomize
    tempdban
    tempdban
    Jul 17, 2017
    18
    @xielongen 你真的试过么?
    xielongen
    xielongen
    OP
    Jul 19, 2017
    20
    @tempdban 你知道如何在 pycharm 中全局添加这个 path 吗
    tempdban
    tempdban
    Jul 19, 2017 via Android
    21
    @xielongen 哇,兄弟,百度
    xielongen
    xielongen
    OP
    Jul 20, 2017
    22
    @tempdban 不只是需要添加,还需要覆盖掉 site-packages 中安装的包,这个涉及到包搜索优先级的问题
    tempdban
    tempdban
    Jul 20, 2017 via Android
    23
    @xielongen 所以我让你插在 sys.path 的最前边啊
    xielongen
    xielongen
    OP
    Jul 20, 2017
    24
    @tempdban pycharm2017.1 专业版 添加之后在最后面,且无法调整
    tempdban
    tempdban
    Jul 20, 2017 via Android
    25
    @xielongen 哥我给你写的两行代码是干啥的
    xielongen
    xielongen
    OP
    Jul 20, 2017
    26
    我上面的回复中声明过不愿意使用这种 dirty 的方式
    xielongen
    xielongen
    OP
    Jul 20, 2017
    27
    @tempdban 我上面的回复中声明过不愿意使用这种 dirty 的方式, 每个文件添加添加这样冗余的代码,看着太恶心了
    tempdban
    tempdban
    Jul 21, 2017 via Android
    28
    @xielongen 兄弟,sys.path 是全部变量 你在程序的最开始改过一次之后,其他的文件读到的也是这个,甚至于多进程由于用的是 fork (),用 multi processing 库产生的子进程的 sys.path 也是修改过的。
    所以我一再问你试过么
    光靠想的就把楼上所有人方案推翻
    中文说的好好的非要夹个英文单词
    能不能静下心来好好研究?程序员这行饭这么好吃?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   705 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 22:01 · PVG 06:01 · LAX 14:01 · JFK 17:01
    ♥ Do have faith in what you're doing.