Src layout to dispense .src prefix in imports? Activate venv in PyCharm terminal for development installs

隐身守侯 提交于 2020-06-29 03:13:17

问题


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']

回答1:


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.



来源:https://stackoverflow.com/questions/62498127/src-layout-to-dispense-src-prefix-in-imports-activate-venv-in-pycharm-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!