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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Python 3.6 modernized and refactored
idlelib
. This change included the renaming of several methods. Because of this,idlelib.PyShell
must now be accessed withidlelib.pyshell
. The following snippet is based on the accepted answer and should work for any Python version:For me launching something like this just works (Linux terminal):
(venv is path to your venv obviously)
Putting a few answers together and here is how I do this on Window with a fully functional batch file.
Make idle.bat in your virtualenv's Scripts directory. It will create (unless they exist) both links to tcl and tk (version 8.5 as of writing) and put them in you virtualenv's Lib directory then it fires up idle. Copy and paste this code exactly into an editor. Change the path names for your current virtualenv and Python install (mine is the standard for 2.7) then save it into Scripts/idle.bat.
Run the script with Powershell (RUN AS ADMIN!) to open idle.
IDLE is essentially
So you can launch it yourself unless you built the virtualenv without default packages.
I am using Ubuntu 15.04 operating system. I have installed some packages using virtualenv.
So, to run the files inside virtualenv including those packages I use the following commands in terminal
(Name of my virtual environment is venv):
After running the IDLE, you can open file using ctrl+o keyboard shortcut.
Short answer
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 project folder called
my_project_dir
by runningpython -m venv my_project-venv
from inside that folder, the virtual environment will be inside a new folder created by the module:On Windows, with Python 3.7.1, the files inside the
my_project-venv
folder will probably look like this:The virtual environment can be started by running either the
activate.bat
orActivate.ps1
script, depending on whethercmd
or PowerShell is used: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:
Then, from any shell other than
csh
or fish, activate the environment by:For
csh
andfish
there are shell-specific scripts for activating the virtual environment (activate.csh
andactivate.fish
, respectively) and they can be run like theactivate
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: