tkbx
tkbx

Reputation: 16305

Can't use wxPython in virtualenv?

I made a simple wxPython script that simply shows a window. When I run it in my normal python 2.7.3 with wxPython (import wx), it works fine. But when I run it in a virtualenv, I get the following:

Traceback (most recent call last):
  File "/Users/student/Desktop/text.py", line 3, in <module>
    class mainWindow(wx.Frame):
AttributeError: 'module' object has no attribute 'Frame'

Why is this? I have wx installed (./pip install wx in my virtualenv's bin folder)

Upvotes: 4

Views: 7499

Answers (5)

Himel Das
Himel Das

Reputation: 1218

This worked for me in Windows virtualenv...

First, go here to get the wheel binaries of wxpython that matches your system http://www.lfd.uci.edu/~gohlke/pythonlibs/#wxpython

Now in your virtualenv command prompt, make sure you have wheel

>pip install wheel

Then install wxpython common

>pip install wxPython_common-3.0.2.0-py2-none-any.whl

Finally install wxpython itself

>pip install wxPython-3.0.2.0-cp27-none-win_amd64.whl

Now in the virtualenv python interpreter, check to see if installation was successful by making and running an example

>python
...
>>> import wx
>>> app = wx.App(False)
>>> frame = wx.Frame(None, wx.ID_ANY, "Running from virtualenv!")
>>> frame.Show(True)
>>> app.MainLoop()

If a window appears and no error occurs, then you're all good :)

Upvotes: 1

cweston
cweston

Reputation: 11647

For others, here is what worked for me:

On Mac OSX I installed wxpython with Homebrew using:

brew install wxpython

Change into your virtualenv site-packages directory:

cd /venv/lib/python2.7/site-packages

then link the wx.pth

ln -s /usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/wx.pth wx.pth

and then link the wx-3.0-osx_cocoa directory:

ln -s /usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/wx-3.0-osx_cocoa wx-3.0-osx_cocoa

Upvotes: 3

Anibalismo
Anibalismo

Reputation: 103

(Im a python noob) I would coment here how I resolve to make wx work in a virtual env, tested on windows.

First you create your virtualenv (I did mine inside the project's directory)

virtualenv env

Then, go to the env\Lib\site-packages folder, and create a file there named wx.pth (name does not matter, only matters file extention)

Open the wx.pth file and edit it so it points to your wx-X.X-msw, where X.X is your wx version number. Mine is 3.0 (july 2014). it should be somethind like this:

C:\Python27\Lib\site-packages\wx-3.0-msw

activate your virtualenv. then open your python shell, and try running import wx; app = wx.App() if you do not get any nasty messages, then you should be all set.

Hope this helps!

Upvotes: 5

Nick T
Nick T

Reputation: 26747

wx on PyPI is a garbage module that has one trivial function. Unfortunately someone acquired the name for their "first python module" which has confused and irritated many people.

The string you give import and pip install can thus obviously be different, e.g. import PIL and pip install pillow, or nearly every Django plugin. Here, you want to pip install wxpython

On Ubuntu installing wxPython can be a bit of a pain, so I would suggest installing it using apt-get install python-wxgtk2.8 then (if your venv has --no-site-packages) symlinking the global library folder into the virtualenv's folder a la:

nick@cody:~/sandbox/lib/python2.7/site-packages
$ ls -lhtr wx*
... wx.pth -> /usr/lib/python2.7/dist-packages/wx.pth
... wx-2.8-gtk2-unicode -> /usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/

Upvotes: 2

reptilicus
reptilicus

Reputation: 10407

I that that these steps worked for me a while ago...

wxpython in a virtualenv

Upvotes: 2

Related Questions