在导入中分配 .src 前缀的 src 布局?在 PyCharm 终端中激活 venv 以进行开发安装 [英] Src layout to dispense .src prefix in imports? Activate venv in PyCharm terminal for development installs

查看:30
本文介绍了在导入中分配 .src 前缀的 src 布局?在 PyCharm 终端中激活 venv 以进行开发安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解在 "src/layout" 以某种方式在导入中使用 src. 前缀?

I want to understand what is considered the correct minimalist way to use setuptools with a "src/ layout" in a way that dispenses using src. prefix in imports?

我已经阅读了大部分 PyPAsetuptools 文档(及其许多用例),但我不明白什么是执行此示例的正确方法.

I have read most of the PyPA and setuptools documentation (and its many use cases), but I can't understand what is considered the correct way of doing this example.

下面的布局再现了我想要实现的目标.我无法理解如何在 mylibrary 包的所有模块中使第二个导入而不是第一个导入工作:

The below layout reproduces what I want to achieve. I can't understand how to get the second import to work instead of the first across all modules of the mylibrary package:

from src.mylibrary.hello_word import hello_function # <- This works.
from mylibrary.hello_word import hello_function  # <- How to get this working?

hello_function()

使用这个目录/文件结构:

Using this directory/file structure:

C:MyProject
│
│   setup.py
│
└───src
    │
    ├──mylibrary
    │      hello_word.py
    │      module_two.py
    │      __init__.py
    │

当我使用开发模式时 使用 pip install -e 安装. 将egg 目录添加到上面的树中:

When I use development mode install with pip install -e . the egg directory is added to the above tree:

    │ (...)
    │ 
    └──mylibrary.egg-info
           dependency_links.txt
           PKG-INFO
           SOURCES.txt
           top_level.txt

使用这个setup.py:

from setuptools import setup, find_packages, find_namespace_packages

setup(
    name='mylibrary',
    version='0.1',
    package_dir={'': 'src'},
    # packages=find_namespace_packages(where='src'),  # <- I suppose this isn't the deciding factor.
    packages=find_packages(where='src'),
)

简单的 hello_world.py 模块,我想在导入时不必编写 src. .

The simple hello_world.py module that I want to dispense having to write src. when importing.

def hello_function():
    print("hello world")

__init__.py 为空.

我正在使用 venv,令我惊讶的是,鸡蛋符号链接并未写入 venv sitepackages 而是写入 C:UsersNameAppDataRoamingPythonPython38site-packages...

I'm using a venv, to my surprise the egg symlink isn't written to the venv sitepackages but to C:UsersNameAppDataRoamingPythonPython38site-packages...

Python 控制台指示找到 mylibrary 包:

Python console indicates mylibrary package is found:

>>> from setuptools import find_packages
>>> find_packages(where='src')
['mylibrary']

推荐答案

描述的问题是由于必须激活 PyCharm 终端内的 venv.

The problem described results from having to activate the venv inside PyCharm's terminal.

您可能会遇到的场景描述如下.(问题不是很明显,因为与终端不同,调试、运行等功能以无缝方式集成了 venv.)

A description of the scenarios you'll likely encounter follows. (The problem isn't immediately obvious because unlike the terminal, functionalities like debugging, running, etc, integrate the venv in a seamless way.)

需要注意的是:

  • Using the verbose flag -v while installing in development mode gives clues to what pip and setuptools are trying to do.

决定性的 pip 消息基于您的 site-packages 的写入权限,但是如果激活,您将不必更改任何默认权限您在终端上的 venv.

The decisive pip messages are based on write permissions of your site-packages, however you won't have to change any of the default permission if activating your venv on the terminal.

如果您使用 1 个 venv,将涉及 3 个不同的 site-packages(注意路径).

If you are using 1 venv, there will be 3 different site-packages involved (mind the paths).

您可能会尝试的 3 个选项:

The 3 options you are likely to try:

选项 1. 以管理员身份运行 PyCharm,从终端执行以下命令:

Option 1. Run PyCharm as admin, executing the following from the terminal gives:

C:MyProject>pip install -v -e .

Non-user install because site-packages writeable
(...)
Creating c:program filespython38libsite-packagesmylibrary.egg-link (link to src)

这会安装到基础 Python 安装中的 site-packages(注意路径).您可能想要避免的东西,因为它会污染您的基础安装.

This installs to site-packages (mind the path) in your base Python installation. Something you likely want to avoid, because it pollutes your base installation.

选项 2. 以用户身份运行 PyCharm.无需在终端上激活 venv.

C:MyProject>pip install -v -e .

Defaulting to user installation because normal site-packages is not writeable
(...)
Creating c:users
ameappdata
oamingpythonpython38site-packagesmylibrary.egg-link (link to src)

这会安装到您的 venv 和 Python 基础安装之外的 site-packages(注意路径).您可能想要避免的事情,因为 PyCharm 在完成后将无法识别开发安装.

This installs to site-packages (mind the path) outside your venv, and outside your Python base installation. Something you likely want to avoid, because PyCharm won't recognize the development installation after it's done.

注意:终端中的消息(...) site-packages is not writeable"指的是 Python 基础安装中的 site-packages.但是,如果没有显式激活 venv,即使您将权限设置为可写,开发安装也不会写入您的 venv site-packages.

NOTE: The message in the terminal "(...) site-packages is not writeable" refers to the site-packages in your Python base instalation. But, without explicitly activating the venv, even if you set the permissions to writeable, the development instalation won't write to your venv site-packages.

选项 3. 以用户身份运行 PyCharm.在终端上激活 venv.

(MyProject_venv) C:MyProject>pip install -v -e .

Non-user install because user site-packages disabled
(...)
Creating c:myproject_venvlibsite-packagesmylibrary.egg-link (link to src)

这里您确实在您的 venv 中写入了 site-packages,这可能是您想要的.

Here you did write to site-packages in your venv, which is likely what you want.

这篇关于在导入中分配 .src 前缀的 src 布局?在 PyCharm 终端中激活 venv 以进行开发安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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