简体   繁体   中英

PyCharm: Refresh virtual environment for a project

The problem

I have a few projects in PyCharm, each with its own virtual environment.

Quite often, I add packages to these virtual environments using the CLI, mainly with python setup.py develop from another local package.

The new package appear in Preferences -> Project -> Project Interpreter and the calls to the newly-installed packages runs, but the imports still appear as erroneous, which drives me nuts:

在此处输入图像描述

What have I tried

  • Changing the virtual environment to a different one, than switching back, hoping it would reload. Did not work.
  • Restarting PyCharm. Works, but it's fairly slow, so I don't consider it a solution.

How do I refresh the virtual environment

For PyCharm 2018.3.4, I solved this by verifying that the correct source locations are specified under File/Settings/Project::Project Structure. All of the valid source directories should be marked as "Sources" and the venv directory should marked as Excluded, at least that's what worked for me.

I had similar issue mainly with local packages except I used pip install -e . and anaconda environment. This worked for me:

  1. Use Ctrl + Shift + A shortcut
  2. Type " Rescan Available Python Modules and Packages "

Instead of the first step you can use Help -> Find Action and then type " Rescan ... ".

The only advantage of this solution is you don't have to restart PyCharm which is really annoying. But it does "Rescan", so does not help with speed much.

I have no enough reputation to comment on @Nerxis post. To complement his answer, I couldn't find " Rescan Available Python Modules and Packages " on Help > Find Action but it was on the Preferences search bar. You can define the key binding in Preferences > Keymap > Plugins > Python , in my case there was no key binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM