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

问题描述

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

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

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

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

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

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

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

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',    <==

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

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.

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

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

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

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.

推荐答案

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

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

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

With this approach, the egg is not in PYTHONPATH.

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

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

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

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

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

07-18 14:29
查看更多