Detect venv in the project folder
We create our environment through scripts, this does the following :
Clones the required projects.
Clones project settings.
Creates the python virtual environment
Issue is how do I automatically get Pycharm to use the venv on the target machine, can I configure a path in the settings that is relative to the root ?
Or a standard path from the root, like ~/project/venv
We have the python system interpreter in a standard location on all machines.
?
Please sign in to leave a comment.
You can just create a new interpreter, select "Virtualenv > Existing environment", and point to the python executable inside a venv directory. PyCharm should detect and activate venv automatically for this project.
Or, do you want to script that as well?
I'd like to script it, we are working on macos. So ideally I'd liek to be able to piut a path into the settings like
~/env-project
Or have a relative path in the project settings like
./venv/bin/python
Which will then force the python interpreter to be called from that point.
Creating a new one works file until you come to create a new environment that has the same name as an existing environment, this then causes an issue wit h the second virtualenv unsure of how to solve this,
Not sure if it's possible to use relative paths for interpreter settings, probably not.
The interpreter settings are stored in "config\options\jdk.table.xml" (see here for where to find the config dir: https://intellij-support.jetbrains.com/hc/en-us/articles/206544519)
You might be able to change config directly using scripts, but this hasn't been tested and I can't guarantee it'll work.
Not sure why you would create a second interpreter with the same name. Having multiple duplicate interpreters may cause issues, but you can use one interpreter for multiple projects, so I don't see a reason to create duplicate interpreters, unless I misunderstood something about your case.
the python environment name seems to be defined by the folder that it is in.(name is in the ./idea folder in a project)
When creating a new environment we rename the older one with a timestamp so if required we can look back at it.
However the name of the virtualenv is in the .idea folder and the realy path that this is on seem to be in the user settings, this means that old environment is not longer poionting at it's virtualenv but the new enivonments vitualenv.
in general if you move a pycharm project on disk you need to setup the virtualenv again in any case.
We use templates that generate the venv folder so our whole project creation process is automatic except for assigning the interpreter. If Pycharm could just detect a local venv directory and make that the interpreter it would make 100's of power business users in our company happy. Is that possible without having to edit the jdk.table.xml which is fiddly since we have windows, mac and linux? Thanks.
@Alison Johnston
Where do you store that venv?
If your project doesn't have .idea folder yet and you put your venv inside the project, PyCharm will automatically choose it as your project interpreter when you first time open the project.
Thanks Sergey. Its in the root directory and thanks I did not realise that but yes we do have a .idea folder because there are other settings we want to share like source folder, requirements file name, runConfigurations.
@Sergey does that trick also work in IntelliJ and if yes, whats the exact name of the venv dir supposed to be? I have tied venv and .venv, which do not seem to work. :(
There is a known bug since the 2020.2.1 version, unfortunately :(
https://youtrack.jetbrains.com/issue/PY-44217
no show files of directory env create for poetry Sergey Karpov