如何在pycharm中全局管理sys.path [英] how to manage sys.path globally in pycharm

查看:50
本文介绍了如何在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:

  1. 设置
  2. 项目:<您的项目名称>
  3. 项目解释器
  4. 选择您要为其更新 sys.path 的解释器
  5. 找到右下角的图标,'显示所选解释器的路径'
  6. 将文件夹添加到路径列表中.

这篇关于如何在pycharm中全局管理sys.path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆