Undefined Variable
Undefined Variable

Reputation: 4267

Using spyder with virtualenv

I am a newbie to Python and wrapping my head around some basic concepts. I come from PHP background. Following is a definition/breakdown of the problem I am facing:

I installed anaconda, which had a whole bunch of libraries and tools installed into my system. This is kind of my "master python environment"

Then I created and loaded a virtualenv. In this virtualenv I loaded a few packages I wanted like pip install simplekml, pip install ipython

Now I fired open spyder and in the iPython console I tried to import simplekml and it gave me an import error. I read about this issue online and it said within spyder I need to point to the python in my virtualenv (using tools > preferences > python interpreter) or I should do a pip install spyder from my virtualenv and use that version.

I tried both. I installed spyder in my virtualenv and then in the iPython console when I import simplekml I get the error:

ModuleNotFoundError: No module named 'simplekml'

If I go to the terminal and open iPython and type the same then it works fine. How can I have that terminal loaded to spyder?

I have been struggling with this for hours so any help you provide is greatly appreciated!

Upvotes: 8

Views: 13804

Answers (2)

Egon Kirchof
Egon Kirchof

Reputation: 59

You need a spyder.ini file different for each virtual environment. In this file, spyder saves the python interpreter to use, among other things. I didn't find a command line option to specify which spyder.ini to use so I created a small bash script:

enter code here`echo "Starting spyder with my spyder.ini..." cp ~/.config/spyder-py3/spyder.ini temp.spyder.ini cp /spyder.ini ~/.config/spyder-py3/spyder.ini echo "Starting spyder..." /usr/bin/spyder3 --new-instance -p echo "Spyder closed. Restoring original spyder.ini" cp ~/.config/spyder-py3/spyder.ini /spyder.ini cp temp.spyder.ini ~/.config/spyder-py3/spyder.ini rm temp.spyder.ini
I save it in the bin folder of my virtual environments as spyder3 so when I call spyder3 it will first replace the sypder.ini for the one I want and restore the original one when I close spyder. The -p part is optional.

Upvotes: 0

Hami
Hami

Reputation: 714

Building on what @carlos-cordoba said in his comment. If you have anaconda installed I suggest you create an anaconda environment as so:

conda create --name pyflakes spyder simplekml ipython

This will create an environment pyflakes with spyder, simpleklm and ipython installed.

Then you just have to activate the environment with source activate pyflakes or activate pyflakes if you are on windows and run spyder from there.

For more information on anaconda environments, see the documentation.

EDIT: Add virtualenv example.

To user virtualenv this should work:

$ pip install virtualenv
$ cd my_project_folder
$ virtualenv my_project
$ source my_project/bin/activate
$ pip install spyder simpleklm ipython

Source

Upvotes: 1

Related Questions