如何在pycharm中全局管理sys.path

编程入门 行业动态 更新时间:2024-10-27 22:25:15
本文介绍了如何在pycharm中全局管理sys.path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一堆单元测试,它们不是从 /python2.7/site-packages/ 而是从一个完全不同的目录导入东西.所以我要做的是在每个测试文件中用 sys.path.append 做一些猴子补丁,让我的 Python 看到我需要的东西.

I have a bunch of unittests that import stuff not from /python2.7/site-packages/ but from a completely different directory. So what I do is do some monkey-patching in each test file with sys.path.append to make my Python see what I need.

有没有一种方法可以为整个 Pycharm 项目全局附加 sys.path 而不用打扰每个文件?

Is there a way to append sys.path globally for the whole Pycharm project without bothering with each and every file?

更新:设置默认工作目录没有帮助.例如,如果我将工作目录设置为 /Users/1111/_projects/_empty_dir/ 像这样:

UPDATE: setting default working directory doesn't help. For example, if I set the working directory to /Users/1111/_projects/_empty_dir/ like so:

然后运行这个

import sys for p in sys.path: print p

我看到的是这个

/Users/1111/.virtualenvs/blesk/bin/python /Users/1111/_projects/_testing_pycharm/importer.py /Users/1111/_projects/_testing_pycharm /Users/1111/.virtualenvs/blesk/lib/python2.7/site-packages/six-1.9.0-py2.7.egg /Users/1111/.virtualenvs/blesk/lib/python2.7/site-packages/parse-1.6.6-py2.7.egg /Users/1111/_projects/_testing_pycharm /Users/1111/.virtualenvs/blesk/lib/python27.zip /Users/1111/.virtualenvs/blesk/lib/python2.7 /Users/1111/.virtualenvs/blesk/lib/python2.7/plat-darwin /Users/1111/.virtualenvs/blesk/lib/python2.7/plat-mac /Users/1111/.virtualenvs/blesk/lib/python2.7/plat-mac/lib-scriptpackages /Users/1111/.virtualenvs/blesk/lib/python2.7/lib-tk /Users/1111/.virtualenvs/blesk/lib/python2.7/lib-old /Users/1111/.virtualenvs/blesk/lib/python2.7/lib-dynload /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /Users/1111/.virtualenvs/blesk/lib/python2.7/site-packages Process finished with exit code 0

并且 sys.path 上没有 /Users/1111/_projects/_empty_dir/.

推荐答案

这是一个相当简单的方法,请遵循以下路径:

This is one fairly easy, follow this path:

  • 设置
  • 项目:
  • 项目解释器
  • 选择您要为其更新 sys.path 的解释器
  • 找到右下角的图标,'显示所选解释器的路径'
  • 将文件夹添加到路径列表中.
  • 更多推荐

    如何在pycharm中全局管理sys.path

    本文发布于:2023-11-23 21:20:34,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1622898.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:全局   如何在   path   pycharm   sys

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!