1

I attempt to create a virtual environment (and store my Python scripts) at directory /Users/MyName/Desktop/TestFolder. Specifically, I'd like to use numpy 1.8.0 in the virtual environment.

My default project interpreter in PyCharm is ~/anaconda/bin/python. How do I do this in PyCharm?

Nicholas
  • 2,560
  • 2
  • 31
  • 58
  • A virtual environment is not intended to store your scripts but rather to add specific packages in an isolated container. Is that what you are trying to do? – bones.felipe May 20 '17 at 21:41
  • Do you mean my scripts only need to point to the path of virtual environment? But isn't it more *independent* if the Python scripts and the virtual environment (i.e., the packages the project depends on) are stored in a same directory/folder so that one can easily export and ship them? Why would anyone try to separate them? @bones.felipe – Nicholas May 20 '17 at 23:12

1 Answers1

2
  1. Use virtualenv to create isolated envinronment:

python3 -m venv ~/VirtualPython ~/VirtualPython/bin/pip3 install numpy # and whatever You want to have as dependencies

  1. Make PyCharm be using that virtualenv: https://www.jetbrains.com/help/pycharm/2017.1/creating-virtual-environment.html

Use ~/VirtualPython/bin/python3 as interpreter

More about virtualenv: https://docs.python.org/3/library/venv.html

  1. Feel no fear using Your new venv. Really, there is always a fear like "should I do some magick before virtualenv scripts is really virtual"? Answer is "No". When You run ~/VirtualPython/bin/python3 my_script.py it's always will be executed by virtualenv
Arenim
  • 4,097
  • 3
  • 21
  • 31
  • Did you ever use Anaconda above? I think when one needs to use several packages, wouldn't it be better if he can use a copy of the whole Anaconda as a separate virtual environment and then only adds additional packages or change certain packages' versions if needed? In terms of *virtualenv*, it seems one has to manually add a list of packages he needs to use. – Nicholas May 20 '17 at 23:17
  • yep. Anaconda is (fully legitimate venv) envinronment itself. – Arenim May 21 '17 at 09:27
  • BUT. Adding special packages INSIDE anaconda for me is BAD idea: You may broke sanity of third-party distro and will be unable to restore. More: You mix YOUR changes and Anaconda's changes. Imagine: Anaconda updates, you'll have to re-create all yours "version changes" This is why it's better to use anaconda just like another upstream source for packages and control Your working version yourself – Arenim May 21 '17 at 09:28
  • Btw, anaconda has syntax sugar for this, more is here: http://stackoverflow.com/questions/16727171/installing-anaconda-into-a-virtual-environment?rq=1 – Arenim May 21 '17 at 09:30
  • I think I have figured this out myself, just go to Preferences>Project: Interpreter>Create Conda Env (from the drop-down menu). Specify the location of this conda environment then (for instance, at the same directory you'd like to create Python scripts), and then done. BTW, it's also possible to add additional package for some version using the + sign once you have the conda virtual environment installed. – Nicholas May 21 '17 at 15:34