Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch python Idle from a virtual environment (virtualenv)

I have a package that I installed from a virtual environment. If I just launch the python interpreter, that package can be imported just fine. However, if I launch Idle, that package cannot be imported (since it's only available in one particular virtualenv and not global). How can I launch Idle from a virtualenv, so that all packages from the virtualenv would be available?

like image 259
Kevin Le - Khnle Avatar asked Feb 07 '11 17:02

Kevin Le - Khnle


People also ask

How do I run a Python code in a virtual environment?

To use the virtual environment you created to run Python scripts, simply invoke Python from the command line in the context where you activated it. For instance, to run a script, just run python myscript.py .


2 Answers

Short answer

  1. Start the virtual environment
  2. Run python -m idlelib.idle

From this answer.

Long answer

This answer assumes Python 3.

There are a few different virtual environment managers, each of which has a slightly different way of handling where python is installed and how it's run, as detailed in this answer.

This answer assumes the venv module is used, and that it was installed following the docs.

Note: Some Linux distributions package the venv module into a separate package: Ubuntu and Debian

If the virtual environment was installed in a folder called my_project-venv by running python -m venv my_project-venv from inside the folder my_project, the virtual environment will be inside a new folder created by the module:

my_project_dir       │       ┝━ my_project-venv 

On Windows, with Python 3.7.1, the files inside the my_project-venv folder will probably look like this:

my_project-venv       │       ┝━ Include       ┝━ Lib       ┝━ Scripts       │     ┝━ ...       │     ┝━ activate.bat       │     ┝━ Activate.ps1       │     ┝━ deactivate.bat       │     ┕━ ...       │       ┕━ pyvenv.cfg 

The virtual environment can be started by running either the activate.bat or Activate.ps1 script, depending on whether cmd or PowerShell is used:

:: Using cmd.exe cd my_project_dir .\my_project-venv\Scripts\activate.bat 
# Using PowerShell cd my_project_dir .\my_project-venv\Scripts\Activate.ps1 

Note: These scripts don't keep the shell open if run by double-clicking them. Start a shell, then run them by typing the above commands, with the folder names changed for your project

On most other operating systems, the virtual environment folder will look like this:

my_project-venv       │       ┝━ bin       │     ┝━ ...       │     ┝━ activate       │     ┝━ activate.csh       │     ┝━ activate.fish       │     ┕━ ...       │       ┝━ include       ┝━ lib       ┝━ lib64       ┕━ pyvenv.cfg 

Then, from any shell other than csh or fish, activate the environment by:

# Most operating systems cd my_project_dir . my_project-venv/bin/activate 

For csh and fish there are shell-specific scripts for activating the virtual environment (activate.csh and activate.fish, respectively) and they can be run like the activate script.

Once the virtual environment has been activated on all operating systems, running the following will start IDLE with access to the packages installed into the virtual environment:

python -m idlelib.idle 
like image 134
Matthew Willcockson Avatar answered Sep 17 '22 14:09

Matthew Willcockson


For Python 3.6+, please see Paul Wicking's answer below.

In Python prior to 3.6, IDLE is essentially

from idlelib.PyShell import main if __name__ == '__main__':   main() 

So you can launch it yourself unless you built the virtualenv without default packages.

like image 41
9000 Avatar answered Sep 18 '22 14:09

9000