5

So I am new to Python. This may be a very foolish question, but I have no idea how to install packages as pytest?

It would be great if somebody could give instructions in order to achieve this.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
tazeunite00
  • 71
  • 1
  • 1
  • 2

2 Answers2

10

In general, installing a package in Python is pretty simple if you use pip which is already installed if you're using Python 2 >=2.7.9 or Python 3 >=3.4.

In order to install pytest:

Python 2: $ pip install pytest

Python 3: $ pip3 install pytest

However, it is a good practice to isolate Python environments by creating a virtual environment. In order to install pytest in an isolated virtual environment:

Python 2

$ pip install -U virtualenv
$ python -m virtualenv venv
$ source venv/bin/activate # in Windows -> $ venv\Scripts\activate.bat
$ pip install pytest

Python 3

$ pip3 install -U virtualenv
$ python3 -m virtualenv venv
$ source venv/bin/activate # in Windows -> $ venv\Scripts\activate.bat
$ pip install pytest

Python 3.6+

$ python3 -m venv venv
$ source venv/bin/activate # in Windows -> $ venv\Scripts\activate.bat
$ pip install pytest
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • Is it a trick to use _specific pytest_ default version? I'm under _Debian-Jessie-like distro_, so python 2.7 is default. If I creates a venv with `python3.6 -m venv .venv`, and install _pytest_ after activate this venv with `pip install pytest==3.7.1`, the version called by `pytest --version` is `This is pytest version 3.2.1 imported from /usr/local/lib/python2.7/dist-packages/pytest.pyc setuptools registered plugins` , if I want to use the newly installed version I have to call `python -m pytest --version`. If this need a dedicated question, I'll do. – freezed Aug 13 '18 at 17:17
  • @freezed, if your default version is Python 2, then you have first to install Python 3. Or you should use `pip install -U virtualenv` to install a virtual environment using python 2. What is the output you are getting after activating your virtual environment if you run `which python`, and what is the output you get when you run `which pip` (again after activating the virtual environment). – lmiguelvargasf Aug 13 '18 at 18:30
  • [Here](https://gist.github.com/freezed/58bd7e81d514e41dd8acf32bde7697ee) is paths of python in/outside venv. [Here](https://framapic.org/CiQs3jcdVNus/4kmCV4zSOmS7.png) is a screenshot of my troubles… but today, I cannot reproduce this behavior… pytest in venv corresponds to the venv python branch… I put this on side, I will see if it happens again. Thanks for answering. – freezed Aug 14 '18 at 06:24
2

Maybe you're looking for something like pip.

For example, if you want to install Cherrypy you must to run

    pip install cherrypy

or if you're using python3, and depending of your distro, the command is

    pip3 install cherrypy

If you're downloading a package from source, then you must to uncompress it and normally, depending the package, you must to run (as administrator or root)

    python setup.py install
Orlando
  • 1,509
  • 2
  • 19
  • 27