talkpython / mastering-pycharm-course

Course demos and handouts for Talk Python's Effective PyCharm course
https://training.talkpython.fm/courses/explore_pycharm/mastering-pycharm-ide
GNU General Public License v2.0
1.12k stars 716 forks source link

PyCharm doesn't see virtual environment #42

Closed BruceBerk closed 3 years ago

BruceBerk commented 3 years ago

My copy of PyCharm (Professional 2020.02 on macOS Catalina) does not see the virtual environment on first opening a project. I am running Python version 3.8.5, installed in /usr/local/bin by HomeBrew.

I followed the instructions under Your Turn - Configure a complex project. Step 01 shows the Project in PyCharm Step 02 shows when I run the program PyCharm uses /usr/local/bin/python3.8 Step 03 shows the project interpreter screen

So, you may want to add to the video a section on what to do if the virtual environment is not the default interpreter: Step 04 - click the gear icon and choose Add... Step 05 - the Existing environment choice should show the virtual environment already filled in. Select Existing environment and click OK Step 06 - running the program now uses the correct Python from the virtual environment

Step 01 - Project Step 02 - Run Program Step 03 - Interpreter Before Step 04 - Add Interpreter Step 05 - Existing Interpreter defaults to  env Step 06 - Run program works correctly
mikeckennedy commented 3 years ago

Hi @BruceBerk

This is frustrating, sorry. For some reason, JetBrains goes back and forth detecting and then not detecting the venvs as the versions change. I've added this as a bug more than once for them. I'd recommend just using the latest and then adding the venv manually as the project default interpreter.

As far as I can see, it's not something you're doing wrong but a problem with PyCharm. It used to only do .env, then it started doing venv automatically, then it stopped both. Some versions would recommend the sorta-detected venv if you when to add one. Currently it seems to completely ignore this.

codecowboy commented 1 year ago

@mikeckennedy This still seems to be an issue in 2023 (version 2023.1 Pro) I had previously installed PyCharm before beginning the course on an M1Pro Mac (Ventura) and had developed a few small projects in a directory PyCharm chose for me (~/PyCharmProjects). Again, prior to the course, I had installed a newer version of python via brew as this seemed to be a smart move if I later wanted to create virtual environments using 3.11 instead of the stock 3.9. If I follow your video, I don't see the nice red venv in the directory structure when I open the project. I spent a good while trying to resolve it but couldn't so am now super frustrated and want to smash all of the things ;) As far as I remember, I have only ever let PyCharm create the virtual environments and they use the homebrew python as the base interpreter.

If I open one of the projects I created before starting the course the red venv is there. Any idea what is going on here? I'm wondering if pycharm will only correctly detect the venvs unnder the PycharmProjects directory? I think it is also creating a project folder there with a .idea subfolder but without the source files. Please let me know if I should open a new ticket.

mikeckennedy commented 1 year ago

Hi @codecowboy Sorry for the struggles. PyCharm definitely will detect venvs anywhere, not just in that folder location. I wonder if possibly you're opening the project at the wrong level? I ask because even if PyCharm won't detect the venv as a venv, it will always show it in the project view as a set of files. So if you don't see that there, the venv is either not created in the right location manually or pycharm is pointed at something like a subfolder in the project.

codecowboy commented 1 year ago

No worries. I think this was maybe caused by my trying to emulate your "mad drag and drop skillz" when opening first_project and setting the interpreter - something I never do with folders and input fields but hey, what could go wrong?

Well, this: somehow I overwrote the virtualenv location without realising it and so the venv directory was indeed being created elsewhere; I ended up with several copies of the project with multiple venv folders in different locations. That might well be what I want in some cases going forward but in this case I just wanted it to look like the video. Lesson learned: leave drag & drop to the pros :)

mikeckennedy commented 1 year ago

LOL, sounds good. :)