要在导入中分配.src前缀的src布局?在PyCharm终端中激活venv以进行开发安装 [英] Src layout to dispense .src prefix in imports? Activate venv in PyCharm terminal for development installs
问题描述
我想了解在"src/布局" 以一种在导入中使用src.
前缀进行分配的方式?
我已经阅读了大多数 PyPA 和 使用此目录/文件结构: 当我使用开发模式使用 使用此 我要分配的简单 我正在使用venv,令我惊讶的是,卵符号链接未写入venv Python控制台指示已找到
问题描述的结果是必须激活PyCharm终端内部的venv a>. 以下是您可能会遇到的情况的描述. (问题并没有立即显现出来,因为与终端不同,诸如调试,运行等功能以无缝方式集成了venv.) 应注意: 在from src.mylibrary.hello_word import hello_function # <- This works.
from mylibrary.hello_word import hello_function # <- How to get this working?
hello_function()
C:\MyProject
│
│ setup.py
│
└───src
│
├──mylibrary
│ hello_word.py
│ module_two.py
│ __init__.py
│
pip install -e .
安装,将egg目录添加到上述树中: │ (...)
│
└──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.
.
def hello_function():
print("hello world")
__init__.py
保留为空.sitepackages
,而是写入C:\Users\Name\AppData\Roaming\Python\Python38\site-packages
... mylibrary
软件包:
>>> from setuptools import find_packages
>>> find_packages(where='src')
['mylibrary']
提供了 pip 和 setuptools 试图做什么的线索.
决定性的 pip 消息基于您site-packages
的写入权限,但是,如果在终端上激活venv,则无需更改任何默认权限./p>
如果您使用1个venv,将涉及3个不同的site-packages
(注意路径).
您可能会尝试的3种选择:
选项1.以admin身份运行 PyCharm ,从终端执行以下命令即可:
C:\MyProject>pip install -v -e .
Non-user install because site-packages writeable
(...)
Creating c:\program files\python38\lib\site-packages\mylibrary.egg-link (link to src)
这将安装到基本Python安装中的site-packages
(注意路径).您可能想要避免的事情,因为它会污染您的基本安装.
选项2.以用户身份运行 PyCharm . 无需在终端上激活venv.
C:\MyProject>pip install -v -e .
Defaulting to user installation because normal site-packages is not writeable
(...)
Creating c:\users\name\appdata\roaming\python\python38\site-packages\mylibrary.egg-link (link to src)
这将安装到venv之外和Python基础安装之外的site-packages
(注意路径).您可能要避免的事情,因为 PyCharm 完成后将无法识别开发安装.
注意:终端中的消息(...)site-packages不可写".指的是Python基本安装中的site-packages
.但是,即使不将venv明确激活,即使将权限设置为可写,开发安装也不会写入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_venv\lib\site-packages\mylibrary.egg-link (link to src)
这里您确实在venv中写了site-packages
,这很可能是您想要的.
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?
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.
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
│
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
With this 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'),
)
The simple hello_world.py
module that I want to dispense having to write src.
when importing.
def hello_function():
print("hello world")
The __init__.py
is left empty.
I'm using a venv, to my surprise the egg symlink isn't written to the venv sitepackages
but to C:\Users\Name\AppData\Roaming\Python\Python38\site-packages
...
Python console indicates mylibrary
package is found:
>>> from setuptools import find_packages
>>> find_packages(where='src')
['mylibrary']
The problem described results from having to activate the venv inside PyCharm's terminal.
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.)
It should be noted:
Using the verbose flag
-v
while installing in development mode gives clues to what pip and setuptools are trying to do.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.If you are using 1 venv, there will be 3 different
site-packages
involved (mind the paths).
The 3 options you are likely to try:
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 files\python38\lib\site-packages\mylibrary.egg-link (link to src)
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.
Option 2. Run PyCharm as user. Without activating venv on the terminal.
C:\MyProject>pip install -v -e .
Defaulting to user installation because normal site-packages is not writeable
(...)
Creating c:\users\name\appdata\roaming\python\python38\site-packages\mylibrary.egg-link (link to src)
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.
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
.
Option 3. Run PyCharm as user. Activating venv on the terminal.
(MyProject_venv) C:\MyProject>pip install -v -e .
Non-user install because user site-packages disabled
(...)
Creating c:\myproject_venv\lib\site-packages\mylibrary.egg-link (link to src)
Here you did write to site-packages
in your venv, which is likely what you want.
这篇关于要在导入中分配.src前缀的src布局?在PyCharm终端中激活venv以进行开发安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!