Src Layout to Dispense .Src Prefix in Import? Activate Venv in Pycharm Terminal for Development Installs

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

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.

How do I activate a virtualenv inside PyCharm's terminal?

Edit:

According to https://www.jetbrains.com/pycharm/whatsnew/#v2016-3-venv-in-terminal, PyCharm 2016.3 (released Nov 2016) has virutalenv support for terminals out of the box

Auto virtualenv is supported for bash, zsh, fish, and Windows cmd. You
can customize your shell preference in Settings (Preferences) | Tools
| Terminal | check Activate virtaulenv

you also need to make sure to have the path of virtual environment path included in the content root folder of your project structure. You can go to settings (preference) | project | Project Structure | if your environment is not included in the project directory.



***Old Method:***

Create a file .pycharmrc in your home folder with the following contents

source ~/.bashrc
source ~/pycharmvenv/bin/activate

Use your virtualenv path as the last parameter.

Then set the shell Preferences->Project Settings->Shell path to

/bin/bash --rcfile ~/.pycharmrc

Can't import local module? PyCharm

For PyCharm to recognize a directory as a module, you need to mark it as a Sources Root. To do that, right click it, hover over "Mark Directory as" and click on "Sources Root".

setup.py - how to set a sub-folder as the main package directory?

You can use what's called a src-layout (since src/ is more typically used as a top-level directory for packages. See https://setuptools.readthedocs.io/en/latest/setuptools.html#using-a-src-layout

If using setup.cfg you can write this like:

[options]
package_dir=
=src
packages=find:

[options.packages.find]
where=src

or equivalently, using an old-style setup.py:

from setuptools import find_packages
setup(
...
package_dir={'': 'src'}
packages=find_packages(where='src')
...
)

packages



Related Topics



Leave a reply



Submit