I use IPython and the %bookmark
magic command but the limitation is that I need to exit the IPython shell to activate a virtual environment and then relaunch IPython.
Is there a way, built into IPython, a third-party package, or a known hack to let me activate the environment without exiting the IPython shell?
Actual workflow
# within ipython shell
exit
# bash shell
cd path/of/my/project
poetry shell # or pipenv shell
ipython
Intended workflow
%cd -b demographics
# activate venv
import package # from that env
2 Answers
This is a good question, but unfortunately, the answer might not be what you are hoping for. If I understood your question correctly – this is not possible. Because virtual shell activation is essentially running child process that setups PATH for another Python interpreter and then PYTHONPATH environment variable for this. A child process can never modify its parent process in any operating system. You cannot, for example, change the Python interpreter you already have running because replacing a running process in-memory is super complicated and never done in practice.
If this is a workflow issue and you definitely want to do this from within IPython, I suggest you write an one-liner copy paste command that would do something along these lines:
- Launches a new IPython using the virtualenv e.g.
(source venv/bin/activate && ipython)
- Terminates the old, existing IPython session that has been launched from the virtualenv (though not sure how you can exit IPython within IPython prompt)
There is likely a better solution for his workflow question. Maybe there is a workflow that is similar as %%bookmark
, but for the whole Python environments and processes, like creating shell scripts.
1
“not sure how you can exit IPython within IPython prompt” Not sure what you mean here, but
exit()
orquit()
closes the IPython shell. But they have to be called from within the IPython shell. So it’s quite impossible to integrate with a script or a shell command.
You can workaround and essentially activate the virtualenv from python. This hack is dependent on the location of activate_this.py
I’ve tested it on virtualenv version 20.2.1, might not work with different versions.
Not recommended for Production
Try this:
import virtualenv
import os
import sys
# Provide the location of your venv
venv_loc = r'C:worktest-venv'
# Setup a new virtual env, if not setup already.
# Skip this line if you want to use an existing virtual env
virtualenv.run.cli_run([venv_loc])
# Find activator script. This is only tested in virtualenv version 20.2.1
if str(os.name).lower() == "nt":
venv_activator = os.path.join(venv_loc, 'Scripts', 'activate_this.py')
elif str(os.name).lower() == "posix":
venv_activator = os.path.join(venv_loc, 'bin', 'activate_this.py')
else:
print("unsupported OS")
sys.exit(1)
# Activate virtual env
exec(open(venv_activator).read(), {'__file__': venv_activator})
# Now you can import your package.
1
Have you tested running this from within an IPython shell that was launched from an already activated virtual env?