9

I'm a newbie with PyCharm and Mac OS. I created a python project in PyCharm with VirtualEnv, and I didn't select the option of 'Inherit global site packages'. After working on this project for a while, I realized I need to inherit some packages that are installed globally.

There is a workaround here, which creates a shared Virtual Environment and uses it: Create shared virtual env. However, I wonder whether there is any way to make changes to current project, rather than creating a new project or creating a shared virtual environment?


A separate question: assuming that I have a project that inherits global packages. If after the project's creation, I installed other packages globally, will those packages available in this project?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
user2830451
  • 2,126
  • 5
  • 25
  • 31
  • I see one option is to create new virtual environment with inherit global site packages option and select make available to all projects incase if you want to share the env between different projects. OR select installed python as a interpreter directly instead of the virtual env. – Reddysekhar Gaduputi Oct 22 '18 at 04:49
  • To answer to your other question, yes if inherit global packages setting is done then all the globally installed packages (even installed after virtual env creation) will be available to project. – Reddysekhar Gaduputi Oct 22 '18 at 04:50
  • Thank you for your reply. However, I actually know the new virtual environment trick and add the link above; but still wonder whether it's possible to not create a new virtual env or select a new interpreter? The second option actually does not work for me, because the current project without globally inherit uses the virtual env which I've installed a lot of project-specific packages. I don't want to install those packages globally. – user2830451 Oct 22 '18 at 20:56

1 Answers1

21

I figured this out when clicking around.

  1. In Pycharm, go to File -> Settings... -> Project -> Project Interpreter, click on the gear icon next to the menu: enter image description here

  2. Then choose "Show all...", and select the virtual environment you want to change, click this icon to add global packages' path into this venv's path enter image description here

then click ok or apply to make it work. Usually, your global packages' path is "/path-to-python/Lib/site-packages", for my desktop is: enter image description here

Now you can use all your global packages within the venv. For any system other than Windows, this should work fine as well.

But you won't see any global packages in pycharm's package viewer for this venv, which totally makes sense, because pycharm can only manager the venv for you, not the global one.


For your separate question: Yes. Because you only add global path into your venv, not like copying all package files into it. After adding the global path, all global packages should work inside your venv at any time.

ChamCham
  • 484
  • 1
  • 8
  • 19
Brandon
  • 708
  • 6
  • 13
  • 2
    The [documentation](https://virtualenv.pypa.io/en/stable/userguide/#the-system-site-packages-option) says to remove the file `venv/python3.7/no-global-site-packages.txt`, or on Windows you can modify `venv/pyenv.cfg` and set `include-system-site-packages = true`. – Peter Wood Oct 17 '19 at 08:27
  • @PeterWood: great comment. Should be a separate answer. – z33k Mar 19 '20 at 12:54