Using IntellijIdea within an existing virtualenv
Asked Answered
P

3

61

I want to use Intellij Idea within my existing python django project which was built within a virtual environment in ubuntu. How do I configure Intellij Idea to use the libraries of the virtual environment?

Peripatetic answered 2/1, 2014 at 5:51 Comment(0)
P
81

For PyCharm 2018

As per documentation from Pycharm:

  1. In the Project Interpreter page, click ] and select Add.
  2. In the left-hand pane of the Add Python Interpreter dialog box, select Virtualenv Environment. The following actions depend on whether the virtual environment existed before.

  3. If Existing environment is selected:

    1. Specify the required interpreter: use the drop-down list, or click Select an interpreter and find one in your file system.
    2. Select the check-box Make available to all projects, if needed.
  4. Click OK to complete the task.


For Pycharm 2016 and later

To add an existing virtual environment to the list of available interpreters

  1. In the Project Interpreter page, click enter image description here.
  2. In the drop-down list, choose Add local.

add environment option

  1. In the Select Python Interpreter dialog box that opens, choose the desired Python executable, located inside the virtual environment folder, and click OK.

Go to this link for more information.


For Older versions:

Well I solved the above problem. I have added virtualenv folder's python to project sdk. The virtualenv directory's python( for example venv/bin/python2.7) needs to be added to Intellij Idea project path. Example: need to go to file>project structure (intellij Idea) enter image description here

press new in Project SDK, and add new path to virtualenv's python directory like this:enter image description here

Go to Modules>Dependencies and set your module sdk to Python SDK which is marked on this picture: enter image description here

Click on Django (option marked in next the image) and set Django project root, Settings,Manage Script like this:enter image description here

Now press ok and final look of the Project settings: enter image description here

Now need to run the project.

Peripatetic answered 2/1, 2014 at 9:15 Comment(4)
this page describes how to add an existing pythong virtualenv jetbrains.com/help/pycharm/2017.1/… however, on OSX, it opens a file dialog, and it doesn't allow to navigate to "hidden" dirs, such as the typical ~/.virtualenvs any idea?Domash
Please correct me if I misunderstood: actually they don't show the hidden directory in the file browser, but if you type the directory name, then type / it will take you inside the hidden folder. It worked for me in 2016 versions. Haven't tested in 2017 edition. Directly copy pasting the path of the python file ie /home/user/proj/.env/bin/python in the file browser also works. @DavidPortabellaPeripatetic
it works, thx! when the file dialog appears, typing / will prompt for the path. here it also explains that you can use Command-Shift-. to show hidden files, and Command-Shift-G as an alternative to typing /. intellij-support.jetbrains.com/hc/en-us/community/posts/… osxdaily.com/2011/03/01/…Domash
For IntelliJ, when using Existing Environment option, make sure you have to select [...]/venv/bin/python (not simply [...]/venv/)Lamed
C
2

The above answer is based on older version of IntelliJ. For new, look here: https://www.jetbrains.com/help/pycharm/2016.1/adding-existing-virtual-environment.html

Cowman answered 4/10, 2016 at 19:41 Comment(0)
E
0

Additional permutation:

  • the project is java/maven
  • one only needs to specify a venv context such that exec'd python commands are run in the context of a specific venv?

Can one add an additional "SDK" (vague term in this context) as a "library" dependency and set the "scope=runtime"?

Python 3.12 as a library dependency

Does this "activate" the python 3.12 prior to any run/debug executions for this project?

This appears to work, but I'm just not feeling super-confident about it at the moment.

Elvaelvah answered 14/7 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.