0

I have a virtual environment and I do not know where should I save my Python file ? it only works when I run it in /home/jojo/Enviroment/venv1 can I save it in other place ? Especially when I want to use PyCharm thank you

test me
  • 83
  • 1
  • 7

1 Answers1

1

virtualenv and your code base can very well be in different locations. I prefer things this way. Here are sample commands that I use.

Step 1: Activate the virtualenv.

[mayank@demo /dev]$ source /usr/local/pyenv3.4/bin/activate
(pyenv3.4)[mayank@demo dev]$ 

Notice the prefix "(pyenv3.4)" that indicates that virtualenv is now activated.

Step 2: Ensure that python executable in virtualenv is indeed pointed by "python"

(pyenv3.4)[mayank@demo dev]$ which python 
/usr/local/pyenv3.4/bin/python

Notice that python is indeed pointing to the one in virtualenv directory.

Step 3: Create a file elsewhere and execute it

echo "import sys; print(sys.executable)" > /tmp/print_python_exe.py
python /tmp/print_python_exe.py

Edit: As suggested by Paul, one might choose to keep the virtualenv directory inside the app directory. This style of structuring projects is also suggested here: http://docs.python-guide.org/en/latest/dev/virtualenvs/#basic-usage

Mayank Jaiswal
  • 12,338
  • 7
  • 39
  • 41
  • Thank YOU it worked, so if I use IDLE I have to run it through the terminal, right ? – test me Sep 08 '16 at 13:08
  • You mean "IDE"? Pycharm allows to create or use and existing virtualenv for your projects. https://www.jetbrains.com/help/pycharm/2016.1/adding-existing-virtual-environment.html. Is that what you are asking ? – Mayank Jaiswal Sep 08 '16 at 13:14
  • No I mean if I don't have PyCharm and I want to run Python in IDLE, do have to run it through the terminal ? – test me Sep 08 '16 at 14:22
  • Not sure. Never used IDLE. Let me redirect you here: http://stackoverflow.com/questions/4924068/how-to-launch-python-idle-from-a-virtual-environment-virtualenv. By the way, give a try to ipython. I use ipython extensively and know for the fact that it's powerful. – Mayank Jaiswal Sep 08 '16 at 14:56
  • The code and the virtualenv do not *have* to be in different locations. I store my virtualenvs inside my projects. In fact, this is the pattern documented in the Python Packaging documentation. – Paul Everitt Sep 09 '16 at 11:01
  • I agree. I should drop the word "must" from my answer. This answer pretty much captures the gist : http://stackoverflow.com/a/1785127/578989 – Mayank Jaiswal Sep 09 '16 at 12:03