Installation of Python, Spyder, Numpy, Sympy, Scipy, Pytest, Matplotlib via Anaconda (2023)
These notes are provided primarily for students of graduate schools IMPRS and DASHH, staff and students at the Max Planck Institute for the Structure and Dynamics of Matter and others at DESY, as well as students at the University of Southampton (United Kingdom).
The objective of these introductory notes is to help readers install Python on their own computers, and to support their learning of programming, computational science and data science, and subsequently their studies, particular in natural sciences, mathematics, engineering, and computer science.
In short, we suggest to use the Anaconda Python distribution.
By the nature of the information provided, the content is likely to become partially outdated over time. For reference: this mini-introduction was written in September 2016, where Anaconda 4.1 was available, and Python 3.5 was the default Python provided, and last revised in December 2022, where conda was version 22.9.0, and Python 3.9.13 is the default interpreter.
What is what: Python, Python packages, Spyder, Anaconda
Python
Python is a programming language in which we write computer programs. These programs are stored in text files that have the ending .py, for example hello.py which may contain:
Python is also a computer program (the technical term is »interpreter») which executes Python programs, such as hello.py. On windows, the Python interpreter is called python.exe and from a command window we could execute the hello.py program by typing:
On Linux and OS X operating systems, the Python interpreter program is called python, so we can run the program hello.py as:
(This also works on Windows as the operating system does not need the .exe extension.)
Python packages
For scientific computing and computational modelling, we need additional libraries (sometimes called packages) that are not part of the Python standard library. These allow us, for example, to create plots, operate on matricies, and use specialised numerical methods.
The packages we often need include:
- numpy (NUMeric Python): matrices and linear algebra
- pandas: Python data science tools (Series and Dataframes)
- scipy (SCIentific Python): many numerical routines
- matplotlib: (PLOTting LIBrary) creating plots of data
We also use in this training:
- sympy (SYMbolic Python): symbolic computation
- pytest (Python TESTing): a code testing framework
The packages numpy, scipy, pandas and matplotlib are essential components computational work with Python and widely used.
Sympy has a special role as it allows SYMbolic computation rather than numerical computation.
The pytest package and tool supports regression testing and test driven development — this is generally important, and particularly so in best practice software engineering for computational studies and research.
Spyder
Spyder (home page) is s a powerful interactive development environment for the Python language with advanced editing, interactive testing, debugging and introspection features. There is a separate blog entry providing a summary of key features of Spyder, which is also available as Spyder’s tutorial from inside Spyder (Help -> Spyder tutorial).
The name SPYDER derives from "Scientific PYthon Development EnviRonment" (SPYDER).
We will use it as the main environment to learn about Python, programming and computational science and engineering.
Useful features include
- provision of the IPython (Qt) console as an interactive prompt, which can display plots inline
- ability to execute snippets of code from the editor in the console
- continuous parsing of files in editor, and provision of visual warnings about potential errors
- step-by-step execution
- variable explorer
Anaconda
Anaconda is a Python distribution. Python distributions provide the Python interpreter, together with a list of Python packages and sometimes other related tools, such as editors. To be more precise, Anaconda is not limited to packaging Python packages, but initially emerged to cater for Python-based applications and packages.
The packages provided by the Anaconda Python distribution include all of those that we need, and for that reason we suggest to use Anaconda here.
A key part of the Anaconda Python distribution is Spyder, an interactive development environment for Python, including an editor.
Installation
In general, the installation of the Python interpreter (from source/binaries) is fairly straightforward, but installation of additional packages can be a bit tedious.
Instead of doing this manually, we suggest on this page to install the Anaconda Python distribution using these installation instructions, which provides the Python interpreter itself and all packages we need.
The Anaconda Python distribution is available for download for Windows, OS X and Linux operating systems (and free).
For Windows and OS X you are given a choice whether to download the graphical installer or the next based installer. If you don’t know what the terminal (OS X) or command prompt (Windows) is, then you are better advised to choose the graphical version.
If you are using Linux, you probably want what is called "x86" (unless you own a "Power8" or "Power9" machine, etc.)
If you have a Mac with M1 or M2 processor, select the M1 option. Either graphical or command line installer is fine.
Download the installer, start it, and follow instructions. Accept default values as suggested.
During the installation, you may have the option to install additional editing environments. You don’t need to install these for this course, but it should not do any harm either.
If you are using Linux and you are happy to use the package manager of your distribution — you will know who you are —, then you may be better advised to install the required packages indivdually rather than installing the whole Anaconda distribution.
Test your installation
Once you have installed Anaconda or the Python distribution of your choice, you can download a testing program and execute it.
Running the tests with Spyder
This can be done either by typing spyder in a terminal or inside the Anaconda Prompt, or by starting Spyder through the Anaconda Navigator.
Spyder should be installed when you install the anaconda distribution. However, if spyder has not been installed (observed once on a Mac M2 in December 2022), then use the command conda install spyder to install it.
The current version of Spyder is 5.3 (at the time of writing).
Spyder may ask you if you want to install kite . This is not necessary for the course.
Open the file in Spyder via File -> Open.
The execute the file via Run -> Run.
If you get a pop up window, you can accept the default settings and click on the run button.
You should see output similar to this in the lower right window of spyder (you may also see a plot appearing):
If the test program produces these outputs, there is a very good chance that Python and the six listed packages are installed correctly.
Running the tests from the console
- Windows: type cmd in the search box
- Mac OS X: Start the Terminal application that is located in the Utilities folder in Applications
- Linux: start one of the shells you have available, or an xterm or so.
Download the testing file to your machine.
Change directory into the folder you have downloaded the file to, and type:
If all the tests pass, you should see output similar to this:
Missing packages
If you install Python in other ways than through the Anaconda distribution and, for example, you have only installed the numpy, scipy, pandas and matplotlib package, the program’s output might be:
Updating packages in the Anaconda installation
To update, for example, spyder and python, follow these steps:
Update the conda program (this manages the updating) by typing the following command into the console:
Confirm updates if asked to do so. More than one package may be listed to be updated.
Update individual packages, for example spyder:
This introductory page from the Anaconda team may contain useful material to get started with the Anaconda.
Related tutorials
If you prefer a video run through of an anaconda installation, check Steve Holden’s post from June 2015
Further reading
To lean more about Anaconda, try the documents and introductory tutorials offered at https://docs.anaconda.com/anaconda/ .
Posted by Hans Fangohr Sat 07 January 2023 Python Python, Productivity, Education
Русские Блоги
Недавно я столкнулся с проблемой: пакет python установлен в командном окне CMD, установленный пакет python не может быть найден в редакторе spyder в anaconda. После проверки информации в Интернете выясняется, что рабочий путь пакета python в cmd такой же, как и у anaconda Путь установки — это каталог того же уровня, поэтому spyder в anaconda не может обнаружить пакет. Посредством запроса данных и реализации было наконец найдено следующее решение:
Шаг 1: Найдите командное окно anaconda, нажмите Пуск—> Все программы—> anaconda—> приглашение anaconda, и вы попадете на следующую страницу,
pip install pydotplus нажмите Enter, чтобы начать установку, и получите следующую страницу
Это показывает, что установка прошла успешно.
Затем откройте spyder и импортируйте только что установленный пакет, как показано ниже:
Working with packages and environments in Spyder
While relatively straightforward once you’re familiar with it, the interaction between Spyder and other packages and environments can sometimes be confusing for first-time users. Improvements to Spyder have made this process much easier (and there is more to come!), but we’d like to clarify how that relationship works.
We’ll start by helping you to debug a common problem encountered in Python when dealing with packages and environments, i.e. when you can’t import a module even if you’re sure that you’ve installed it. Next, we’ll guide you through setting up your Spyder environment(s) to improve your workflow. If you’re looking for a way to use Spyder with different environments (e.g. one for simple data analysis, one for machine learning, one for developing an app, etc.) you can go directly to the final section.
The most common problem: Using newly-installed packages inside Spyder
After installing a package (let’s call it foo ) outside Spyder, users may encounter an error trying to import it inside the IDE:
This happens because foo was installed (with either conda or pip ) in a different conda or venv/virtualenv environment than the one in which Spyder is currently running.
To confirm this is the problem, you need to:
Activate the environment (e.g. myenv ) in which you installed the package foo (e.g. with conda activate myenv for conda, source myenv/bin/activate or workon myenv for virtualenv/venv, etc.).
Start a Python interpreter there by running the command python .
Run the following command inside the Python interpreter:
Start Spyder and run the same command shown in Step 3 in a Console.
If the resulting paths are the same, then Spyder and the package are in the same environment, and import foo shouldn’t produce an error (or else there is likely an unrelated issue with your installation).
If the resulting paths are different, then you have three choices:
- Activate the environment in which Spyder is installed and install your package on it (see next section). If you try to install future packages in another environment (like myenv ), you’ll get the same ModuleNotFoundError .
- Install Spyder into the existing myenv environment, or any other you’d like to work in, and run it from there (see following section). This is a little simpler than the third option and has the same effect, but more overhead and is less flexible.
- Install just the spyder-kernels package into the myenv environment, and set your Python interpreter path in Spyder’s Preferences to point to myenv ‘s Python executable (see the final section. This requires one more initial step, but requires less maintenance in the long run and avoids duplicate Spyder installs.
Installing packages into the same environment as Spyder
Spyder is a Python package just like any other you may be used to, and so you can import any package within its Console or Editor as you could from a regular Python or IPython terminal launched in Spyder’s environment:
- If Spyder is installed via our standalone installers (as we recommend on Windows and macOS), this will be Spyder’s built-in environment, which contains many popular scientific package, but cannot be modified, to avoid breaking Spyder itself.
- If Spyder is installed with Anaconda (as we recommend on Linux) and launched via a shortcut, from Anaconda Navigator or from Anaconda Prompt without modifying anything, this will be the default base Anaconda environment.
- If Spyder is installed via pip (experts only) and not into a virtualenv / venv , this will usually be whatever Python installation pip itself belongs to.
- If you use a system package manager ( apt-get , dnf , emerge , etc) to install Spyder, this will typically be your system Python and its library of packages.
- If you installed Spyder into a specific environment ( conda-env or venv ), or it came with a pre-configured one (like those for Keras or TensorFlow) and launched it from there, it will only have access to packages from that environment.
Therefore, if you’d like to use a package with your existing Spyder install (e.g. importing it into your scripts, packages or a Spyder IPython console), the simplest way to do so is to install the package into the same environment in which you installed Spyder, typically by the same means you installed Spyder ( conda , pip , package manager, etc). However, this is not possible if you’ve used a standalone installer, and if you’re installing packages with pip , conda-forge , Github, or custom channels, working on multiple major projects at once, using prebuilt environments, or otherwise have more sophisticated needs, you’ll likely want to use one or more separate environments for your packages. If so, the next section explains how.
Working with other environments and Python installations
If you have an existing, pre-configured environment (such as for Keras or TensorFlow), are managing multiple environments (such as for development or testing purposes), or even would like to work within a totally separate Python installation as that in which Spyder is installed (such as a standalone installer Spyder with a separate Anaconda installation, or vice-versa), you can install the modular spyder-kernels package into any Python environment ( conda environment, virtualenv/venv , system Python, WinPython, etc) in which you wish to work, and then change the Python interpreter used by Spyder on its IPython consoles to point to the Python executable of that environment.
This takes a small amount of preparation and configuration, but is much «lighter» and quicker than a full Spyder installation into that environment, avoids dependency conflicts, and opens up new workflow possibilities.
To achieve this, follow these steps:
Activate the environment (e.g. myenv ) in which you’d like to work (e.g. with conda activate myenv for conda, source myenv/bin/activate or workon myenv for virtualenv/venv, etc)
Install the spyder-kernels package there, with the command:
conda install spyder-kernels if using conda/Anaconda,
pip install spyder-kernels if using pip/virtualenv.
After installing via either method, run the following command inside the same environment:
and copy the path returned by that command (it should end in python , pythonw , python.exe or pythonw.exe , depending on your operating system).
Deactivate that environment, activate the one in which Spyder is installed (if you’ve installed it in its own environment) and start Spyder as you normally would.
After Spyder has started, navigate to Preferences > Python Interpreter > Use the following interpreter and paste the path from Step 3 into the text box.
Start a new IPython console. All packages installed in your myenv environment should be available there. If conda is used, the name of the current environment and its Python version should be displayed in Spyder’s status bar, and hovering over it should display the path of the selected interpreter.
Notes:
- In order for the Variable Explorer to be able display the built-in editors for specific data types (Numpy array, Pandas Series/DataFrame, etc) the corresponding optional Spyder dependencies (Numpy, Pandas, etc) need to be installed in Spyder’s environment, not just the IPython console working env. Furthermore, this is currently also required for custom classes provided by third-party packages displayed in the Object Explorer, though future Spyder versions may remove this latter limitation.
- While Spyder should work fine without it, ensuring the Python minor version ( 3.6 , 3.7 , 3.8 , etc) in Spyder’s environment matches that in your working environment, if practicable, will minimize the chance of any issues.
Connect with Spyder through our social media channels and stay up to date with current developments!
How to instal Python packages for Spyder
I am using the IDE called Spyder for learning Python. I would like to know in how to go about in installing Python packages for Spyder?
6 Answers 6
step 1. First open Spyder and click Tools —> Open command prompt.
I am running Spyder 4.2.4 and for me following solution turned out to be working:
- open tools-> preferences -> python interpreter
- click ‘use the following python interpreter’
- point the location to local python installation, in my case : C:\Users\MYUSER\AppData\Local\Programs\Python\Python37\python.exe
- Click OK and restart the kernel.
Now the pip started to work and I was able to import any package I previously installed on the cmd/python CLI.
Spyder is a package too, you can install packages using pip or conda, and spyder will access them using your python path in environment. Spyder is not a package manager like conda,, but an IDE like jupyter notebook and VS Code.
For the latest versions of Spyder use this console at right bottom
Note: Once you hit enter it may take some time to install and you can’t see the progress until it finishes.