Using spyder with virtualenv - python

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!

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

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.

Related

Installed Python 3, now my Anaconda environment is messed up

I apologize in advance for my poor vocabulary - I do not know much about environments, paths, and things of the sort. I am on macOS Catalina.
I created a program using Spyder from Anaconda. My program uses packages like pandas and numpy which are built into Anaconda. I used to be able to run the program in Terminal with the command: $ python3 app.py.
However, I recently downloaded Python 3.9.1 from https://www.python.org/downloads/mac-osx/. Ever since then, I have been unable to run the program in Terminal because of missing package errors like
import numpy as np
ModuleNotFoundError: No module named 'numpy'
I went to my applications folder, right clicked on Python 3.9.1, and clicked Move To Trash. This did not solve my problem. I reinstalled Anaconda (I did not uninstall it - just simply installed again), but that also did not solve my problem. I am able to run the program in Spyder (from Anaconda), but I wish to run it in Terminal again.
I believe the solution is simple, but I am not sure what to do. I have tried searching and reading but I am not familiar with the terminology. I think I just need to reset the environment, but I am not sure.
Anaconda is used for creating closed enviorments, so you don't need to thrash your computer with global packages.
Imagine you have 2 different projects. Project A works only on python 3.2 and Project B works on 3.8.
That's where anaconda comes in.Managing enviorments with conda
conda create -n PROJECTA python=3.2
conda create -n PROJECTB python=3.8
Now activate env you wish to work with. For macOS
source activate PROJECTA
Now you should see (PROJECTA) instead of (base)
Now inside this PROJECTA you can install modules you require like
pip install numpy
and when executing .py file
move to dir with your app.py file and
python3 app.py
this will be opened in conda enviorment you created and activated, using modules you installed in this env.
You can still edit py file and execute it through shell, but it will throw errors if you try to run it from IDE without linking project to respected conda enviorment.
If you are using PyCharm Configure a Conda vir env in PyCharm
You must create separate environments for every projects or it will get messed up.
conda create -n name_of_environment python=3.6
You must not delete the python folder into trash rather uninstall it Python 3.9.1 and remove its path from the directory.
In short if you run python from terminal and it is not supporting your Installed anaconda packages . You should use anaconda prompt instead of CMD.
Because your anaconda is not added to path rather then it picks up the python 3.9 you have installed from Python 3.9.1 from https://www.python.org/downloads/mac-osx/. ( This is the python with separate environment then anaconda so it wont detect anaconda packages ).
When you run the cmd and enter python it runs the python that you downloaded and installed rather then Anaconda
At the time of installing anaconda it gives option to add conda variables to path you can select those at installing stage / or add manually

Unable to run python script in virtualenv using bash script

I want to run a python script in in-built anaconda environment tensorflow_p36. To check if it is in virtual environment or not, I am using command pip -V.
My first attempt at bash script:
#!/bin/bash
source activate tensorflow_p36
python /home/ec2-user/abc/temp.py
pip -V
Note: tensorflow_p36 being an in-built environment, does not require to be called from specific /env/bin directory. It can be activated from any directory. I think it's a feature of Amazon Deep Learning AMIs.
My second attempt at bash script:
#!/bin/bash
pythonEnv="/home/ec2-user/anaconda3/envs/tensorflow_p36/"
source ${pythonEnv}bin/activate
${pythonEnv}bin/python /home/ec2-user/abc/temp.py
pip -V
Note: When I try to run source /home/ec2-user/anaconda3/envs/tensorflow_p36/bin/activate command in terminal, the environment isn't being activated.
Each time, I am getting the same result:
pip 9.0.1 from /home/ec2-user/anaconda3/lib/python3.6/site-packages (python 3.6)
Whereas, I should be getting:
pip 9.0.1 from /home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages (python 3.6)
Can someone please explain how do I activate virtual environment and run a python script from that environment? I need to use this particular environment because of the dependencies installed in it.
Extra info:
Not sure if it matters, but the tensorflow_p36 is a conda environment, not a virtualenv.
This works with virtualenv. Create environment:
virtualenv -p python 3.6 tensorflow_p36
Then change the script to:
#!/bin/bash
source $HOME/tensorflow_p36/bin/activate
python /home/ec2-user/abc/temp.py
I believe the confusion has to do with the fact that you are using anaconda and not virtualenv to create a python environment. These two tools work differently.
If you are using an EC2 instance, why not to install tensorflow_p36 globally anyway?

Switch between spyder for python 2 and 3

From what I have learnt in the documentation it states that you can easily switch between 2 python environments by just creating a new variable using command prompt
"conda create -n python34 python=3.4 anaconda" if i already have python 2.7 installed.
An environment named python 34 is created and we can activate it using "activate python 34" But all this happens like executing the above commands happens in my windows command prompt. I want to switch between python versions in spyder IDE, How to do this?
Spyder is launched from the environment that you're using.
So if you want to use python 3 in Spyder then you activate python34 (or whatever you named the environment with Python 3) then run spyder.
If you want to use python 2 in Spyder then you deactivate the python3 environment (or activate an environment in which you installed Python 2) then run spyder.
I do not believe that you can change environments once Spyder is launched.
N.B. you may need to install Spyder in each environment, depending on your set up, by first activating the environment then using conda install spyder.
Just go to preferences in spyder & then go to Python interpreter-> Use the following python interpreter: here, from browse files option, give path for your python2.exe file & then apply. Now your python2 doesn't have spyder-kernels module required to open console in spyder so install it by writing command in cmd python2 -m pip install spyder-kernels. Here python2 -m is used coz I have two versions of python installed
Just go to the directory where you have installed Spyder(use cd in command prompt), for me, it looks like "C:\Users\Rohan\Anaconda2" and type spyder in cmd. it will run your Spyder IDE.

jupyter ModuleNotFoundError: No module named matplotlib

I am currently trying to work basic python - jupyter projects.
I am stuck on following error during matplotlib:
screenshot on jupyter-error
ModuleNotFoundError: No module named 'matplotlib'
I tried to update, reinstall matplotlib aswell in conda and in pip but it still not working.
happy over every constructive feedback
In a Notebook's cell type and execute the code:
import sys
!{sys.executable} -m pip install --user matplotlib
and reload the kernel
(src: http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/ )
open terminal and change the directory to Scripts folder where python installed. Then type the following command and hit enter
pip install matplotlib
Hope this will solve the issue.
I was facing the exact issue. It turns out that it was using the system Python version despite me having activated my virtual environment.
This is what eventually worked.
If you are using a virtual environment which has a name say myvenv, first activate it using command:
source activate myvenv
Then install module ipykernel using the command:
pip install ipykernel
Finally run (change myvenv in code below to the name of your environment):
ipykernel install --user --name myvenv --display-name "Python (myvenv)"
Now restart the notebook and it should pick up the Python version on your virtual environment.
While #Frederic's top-voted solution is based on JakeVDP's blog post from 2017, it completely neglects the %pip magic command mentioned in the blog post. Since 2017, that has landed in mainline IPython and the easiest way to access the correct pip instance connected to your current IPython kernel and environment from within a Jupyter notebook is to do
%pip install matplotlib
Take a look at the list of currently available magic commands at IPython's docs.
generally speaking you should try to work within python virtual environments. and once you do that, you then need to tell JupyterLab about it. for example:
# create a virtual environment
# use the exact python you want to work with in this step
python3.9 -m venv myvenv
# 'activate' (or 'enter') it
source myvenv/bin/activate
# install the exact stuff you want to use in that environment
pip install matplotlib
# now tell JupyterLabs about the environment
python -m ipykernel install --user --name="myenv" --display-name="My project (myenv)"
# start it up
jupyter notebook mynotebook
# if you now look under 'Kernel->Change kernel', your 'myenv' should be there
# select it (restart kernel etc if needed) and you should be good
The issue with me was that jupyter was taking python3 for me, you can always check the version of python jupyter is running on by looking on the top right corner (attached screenshot).
When I was doing pip install it was installing the dependencies for python 2.7 which is installed on mac by default.
It got solved by doing:
> pip3 install matplotlib
Having the same issue, installing matplotlib before to create the virtualenv solved it for me. Then I created the virtual environment and installed matplotlib on it before to start jupyter notebook.
in jupter notebook type
print(sys.executable)
this gave me the following
/Users/myusername/opt/anaconda3/bin/python
open terminal, go into the folder
/Users/myusername/opt/anaconda3/bin/
type the following:
python3 -m pip install matplotlib
restart jupyter notebook (mine is vs code mac ox)
If module installed an you are still getting this error, you might need to run specific jupyter:
python -m jupyter notebook
and this is also works
sudo jupyter notebook --allow-root

Python modules (Jupyter) using wrong anaconda environment on Windows

My problem is that I am running multiple python environments with different dependencies on a Windows 10 box. The python themselves are working fine, however when I run Jupyter it does not use the correct python environment.
I have several python environments setup there were installed using
conda env create -n python=3.5
I installed Jupyter using pip (because the conda install seemed broken at the moment)
pip install jupyter
I am able to switch between them using activate in the windows cmd
>activate env1
/c/Anaconda3/envs/env1/python
>which python
>activate env2
>which python
/c/Anaconda3/envs/env2/python
When I run each python binary and check the sys.path it pertains to the correct install.
import sys
sys.path
['', 'C:\\Anaconda3\\envs\\env1', 'C:\\Anaconda3\\envs\\env1\\python35.zip', 'C:\\Anaconda3\\envs\\env1\\DLLs', 'C:\\Anaconda3\\envs\\env1\\lib', 'C:\\Anaconda3\\envs\\env1\\lib\\site-packages', 'C:\\Anaconda3\\envs\\env1\\lib\\site-packages\\cycler-0.10.0-py3.5.egg']
I do not have PYTHONPATH set and the only python entries in my path (other than whatever activate changes) are to the root Anaconda.
>echo %PATH%
... C:\Anaconda3\Library\bin; C:\Anaconda3\Scripts; C:\Anaconda3 ...
Things seem to work well if I am just using python.
However whenever I try to run jupyter it seems to be using env2 regardless. In my Jupyter notebook I run the import sys; sys.path and it always seems to point to env2.
I have verified that I am using the correct Jupyter, and I have attempted running Jupyter by calling python.
>activate env1
>which jupyter
/c/Anaconda3/envs/env1/Scripts/jupyter
>activate env2
>which jupyter
/c/Anaconda3/envs/env2/Scripts/jupyter
jupyter notebook # This shows env2 in sys.path
python -m jupyter notebook # This shows env2 in sys.path extended by the below entries
'c:\\anaconda3\\envs\\env2\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\username\\.ipython'
I have also tried manually installing the correct python in Jupyter and it does not change anything.
ipython kernel install --user
I have a bunch of dependencies installed in my env1 that my Jupyter notebooks require and I don't want to mess with my env2.
Does anyone have any idea why Jupyter is not using the correct python environment? Is there some python configuration I am not aware of? Is this some odd Windows specific issue?

Categories

Resources