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

前端 未结 1 1938
無奈伤痛
無奈伤痛 2020-12-04 02:08

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

相关标签:
1条回答
  • 2020-12-04 02:50

    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.

    0 讨论(0)
提交回复
热议问题