在 PyCharm 中控制 PYTHONPATH 排序以首先搜索源文件夹 [英] Controlling PYTHONPATH ordering in PyCharm to make the Source Folders are searched first

查看:25
本文介绍了在 PyCharm 中控制 PYTHONPATH 排序以首先搜索源文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 projectname 包,并使用 PyCharm 调试其中的代码.我还使用 venv 为包设置 Python 环境.我遵循以下标准包结构.

<预><代码>.├── 姓名│ ├── __init__.py│ ├── arith.py│ └── arith.py├── 仓│ └── app.py├──构建│ ├── bdist.macosx-10.11-intel│ └── lib│ └── 名字│ ├── __init__.py│ └── arith.py├──区│ └── projectname-0.1-py2.7.egg├── 文档├──需求.txt├── setup.py└── 测试├── __init__.py└── arith_tests.py

然后,我将项目导入到 PyCharm 中.在 Project:sekelton 中,我将 NAME/tests 标记为源文件夹,将 build/dist 标记为排除文件夹.

我还运行 python setup.py install 来构建生成的 egg 文件并将其安装到 venv 的 site-package 目录中.

问题是安装在 site-package 中的 egg 文件首先被调用,因为 PYTHONPATH 显示 from import sys;打印 sys.path:

['/python/structure/projects/skeleton/bin','python/structure/projects/venv/lib/python2.7/site-packages/projectname-0.1-py2.7.egg', <== 首先搜索egg文件'python/结构/项目/骨架/名称', <==

这很烦人,因为我无法使用 PyCharm 调试代码,当我修改代码时,我不得不再次运行 python setup.py install 来更新 egg 文件.我可以通过从 Project Interpreter 设置中删除 egg 文件来规避这个问题,但我认为更改顺序应该是更好的选择.

如何更改 PyCharm 中的 PYTHONPATH 顺序,以便首先搜索本地源文件夹?

编辑

当我尝试从 Project Interpreter 设置中删除包时,PyCharm 显示错误消息,但这是误报,因为 PyCharm 成功删除了 egg 文件,并更新了 easy-install.pth.

解决方案

使用 pip install -e NAME,将使 projectname.egg-link 指向NAME 目录,并更新 easy-install.pth.我认为这是比 python setup.py 更好的方法,并且解决了 PYTHONPATH 问题.

骨架>点安装 -e .获取文件:///python/structure/projects/skeleton要求已经满足(使用 --upgrade 升级):nose in/python/structure/projects/venv/lib/python2.7/site-packages(来自项目名称==0.1)安装收集的包:projectname为 projectname 运行 setup.py develop安装成功 projectname

使用这种方法,鸡蛋不在 PYTHONPATH 中.

['/python/structure/projects/skeleton/bin','/python/structure/projects/skeleton/NAME',

更好的是,我们可以使用 pip uninstall projectname 删除包.

pip 卸载项目名卸载 projectname-0.1:python/structure/projects/venv/lib/python2.7/site-packages/projectname.egg-link继续(是/否)?是成功卸载 projectname-0.1

我从 从本地安装 Python 包得到提示带有pip的文件系统文件夹

I create a projectname package, and use PyCharm to debug the code in it. I also use venv for setting up Python environment for the package. I follow the standard package structure as follows.

.
├── NAME
│   ├── __init__.py
│   ├── arith.py
│   └── arith.py
├── bin
│   └── app.py
├── build
│   ├── bdist.macosx-10.11-intel
│   └── lib
│       └── NAME
│           ├── __init__.py
│           └── arith.py
├── dist
│   └── projectname-0.1-py2.7.egg
├── docs
├── requirements.txt
├── setup.py
└── tests
    ├── __init__.py
    └── arith_tests.py

Then, I imported the project into PyCharm. In Project:sekelton, I marked NAME/tests as source folders, and build/dist as excluded folders.

I also run python setup.py install to build and install the generated egg file into the venv's site-package directory.

The issue is that the egg file installed in site-package is called first as the PYTHONPATH shows from import sys; print sys.path:

['/python/structure/projects/skeleton/bin',
 'python/structure/projects/venv/lib/python2.7/site-packages/projectname-0.1-py2.7.egg',  <== egg file is searched first
 'python/structure/projects/skeleton/NAME',    <==  

This is pretty annoying as I can't debug the code with PyCharm, and when I modified the code, I had to run python setup.py install again to update the egg file. I may be able to circumvent this issue by removing the egg file from the Project Interpreter setup, but I think changing the order should be the better option.

How can I change the PYTHONPATH order in PyCharm so that the local Source Folders are searched first?

EDIT

The PyCharm shows an error message when I tried to remove the package from the Project Interpreter setup, but it's false positives, as PyCharm successfully removes the egg file, and updates the easy-install.pth.

解决方案

Using pip install -e NAME, will make the projectname.egg-link that points to the NAME directory, and update the easy-install.pth. I think this is better approach than python setup.py, and solves the PYTHONPATH problem.

skeleton> pip install -e .
Obtaining file:///python/structure/projects/skeleton
Requirement already satisfied (use --upgrade to upgrade): nose in /python/structure/projects/venv/lib/python2.7/site-packages (from projectname==0.1)
Installing collected packages: projectname
  Running setup.py develop for projectname
Successfully installed projectname

With this approach, the egg is not in PYTHONPATH.

['/python/structure/projects/skeleton/bin',     
'/python/structure/projects/skeleton/NAME',

Even better, we can remove the package with pip uninstall projectname.

pip uninstall projectname
Uninstalling projectname-0.1:

python/structure/projects/venv/lib/python2.7/site-packages/projectname.egg-link
Proceed (y/n)? y
    Successfully uninstalled projectname-0.1    

I got hints from Installing Python packages from local file system folder with pip

这篇关于在 PyCharm 中控制 PYTHONPATH 排序以首先搜索源文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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