How do I use pytest with virtualenv? - python

I installed pytest into a virtual environment (using virtualenv) and am running it from that virtual environment, but it is not using the packages that I installed in that virtual environment. Instead, it is using the main system packages. (Using python -m unittest discover, I can actually run my tests with the right python and packages, but I want to use the py.test framework.)
Is it possible that py.test is actually not running the pytest inside the virtual environment and I have to specify which pytest to run?
How to I get py.test to use only the python and packages that are in my virtualenv?
Also, since I have several version of Python on my system, how do I tell which Python that Pytest is using? Will it automatically use the Python within my virtual environment, or do I have to specify somehow?

There is a bit of a dance to get this to work:
activate your venv : source venv/bin/activate
install pytest : pip install pytest
re-activate your venv: deactivate && source venv/bin/activate
The reason is that the path to pytest is set by the sourceing the activate file only after pytest is actually installed in the venv. You can't set the path to something before it is installed.
Re-activateing is required for any console entry points installed within your virtual environment.

Inside your environment, you may try
python -m pytest

In my case I was obliged to leave the venv (deactivate), remove pytest (pip uninstall pytest), enter the venv (source /my/path/to/venv), and then reinstall pytest (pip install pytest). I don't known exacttly why pip refuse to install pytest in venv (it says it already present).
I hope this helps

you have to activate your python env every time you want to run your python script, you have several ways to activate it, we assume that your virtualenv is installed under /home/venv :
1- the based one is to run the python with one command line
>>> /home/venv/bin/python <your python file.py>
2- add this line on the top of python script file
#! /home/venv/bin/python and then run python <you python file.py>
3- activate your python env source /home/venv/bin/activate and then run you script like python <you python file.py>
4- use virtualenvwrapper to manager and activate your python environments

Related

"No Python at..." error after installing different version and changing the path variable

I use Windows 10. I was trying to pip install Vectorbt but the installation could not have been finished because there was an error when numba and llvmlite were suppose to be installed. I was using Python 3.10 and read that there is a compatibility problem with it. So I uninstalled it and installed Python 3.8.7. Afterwards I made sure that the system variable is set on the proper Python path. I closed everything and even restarted the system but in the command line or the powershell terminal in VSC I get the error "No Python at" and the directory of the previous 3.10 installation.
The variable is set corectly (was set as an admin).
Any ideas?
Maybe something with the registry?
Thanks!
I can recommend you use virtual environments to manage python versions (you'll never look back), high level steps:
cd mydir
virtualenv .venv # creates a virtual env dev (or python3 -m venv .venv)
source .venv/bin/activate # activates the virtual env
pip install -r requirements.txt # installs the required dependencies
python -m pytest # runs the unit tests
More details in the docs: https://docs.python.org/3/library/venv.html

How to create a Python virtual environment independent of OS and Python version

I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.
If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller.
https://realpython.com/pyinstaller-python/
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate command.
To deactivate environment simply run: deactivate

install packages in Python2 with Python3

I am trying to install numpy, nltk, etc packages for Python 2 to run a code. But I have Python3 as well and the path variable is set to it. When I try to use any pip install command it shows the package is available in Python3's directory.
Also, I am using VSCode, so I did not add the path variable.
I suggest you use virtual environments. Because if you read about virtual environments, you will find that they are created for such cases.
To create virtual environments, you must do the following:
Make a note of the full file path to the custom version of Python you just installed.
virtualenv -p /home/username/opt/python-2.7.15/bin/python venv
In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
source venv/bin/activate (Linux)
./venv/Scripts/activate.bat (Windows)
Notice how your prompt is now prefixed with the name of your environment (venv, in our case). This is the indicator that venv is currently active, which means the python executable will only use this environment’s packages and settings.
Now run the following:
(venv) $ which python
/Users/ashkan/python-virtual-environments/venv/bin/python (in my case)
now you have access to python2.7.
The best practice for this particular problem would be virtual environments.And for that matter Pipenv would be a good option.
Install Pipenv.
$ brew install pipenv (MacOs)
$ sudo apt install pipenv (Debian)
$ sudo dnf install pipenv (Fedora)
pip install pipenv (Windows)
Creating virtual env with Pipenv.
pipenv install --python 2.7 numpy
This command will install create a virtual environment and install python 2.7(which will be used as the main interpreter once you activate the environment) along with numpy in that environment. This will avoid the packages version conflicts too.
To activate the environment
pipenv shell
If you are working in the Vs Code workspace then you should set the interpreter path(python path) to the path of the virtual environment.
when we install anything using pip. it will install dependencies for default python version. so you can change the default python version using this link https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux
Hope this will solve your problem
After crating a virtual environment with python 2.7 you can install your required packages

Why does clear Python virtualenv use system Python library?

I am newbie in Automation (Python + Pytest).
I try to use clear virtualenv on my mac, but somehow Pytest runs from a local directory.
So, what I try to do:
I have a project with many pip packages (pytest, selene etc.).
I do :
$ pip3 install --user virtualenv
$ virtualenv --no-site-packages venv
$ cd venv/
$ source venv/bin/activate
$ echo $PATH
> /Users/.../venv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/puppetlabs/bin
$ which python
> /Users/.../venv/bin/python
$ which pytest
> /usr/local/bin/pytest
And when I try to run my pytest - it does! But shouldn't!
If I delete Pytest from my mac as:
$ pip3 uninstall pytest
it works, and I got clear venv.
Python 3.7.4
This is pytest version 5.2.1, imported from /usr/local/lib/python3.7/site-packages/pytest.py
setuptools registered plugins: pytest-reportportal-1.0.4 at /usr/local/lib/python3.7/site-packages/pytest_reportportal/plugin.py
Can someone say what I do wrong? Or what is the problem with it?
How to create clear venv without dependencies from local PATH libs?
The behavior seems perfectly normal, working as intended.
Install pytest in your virtual environment, it will then take priority over the one globally installed, since it will be located higher up in your $PATH. Consider uninstalling the global one if you don't intend to use it.

How to use a Python virtual env in my Ubuntu bash?

I am trying to package my project with a virtual env so that it is easier to implement.
I am trying to do this in a Ubuntu bash.
I have succesfully created a Python venv using the Python virtualenv library.
I do manage to activate it using source venv_name/bin/activate.
I can indeed see (venv_name) at the beginning of my command line.
However, I do not manage to actually use this virtual environment.
I have for proof that when I type which python3 I get my root python3; and I have tried to update a package in the virtualenv but it has been updated in the root python.
What should I do to actually use my virtual env ? For now I am trying:
python3 myscript.py
And it is working but I suspect it's running with my root python3.
I think you have two versions of python (2 and 3). You create virtualenv with python 2. Recreate virtualenv with correct python version
You have to make next steps to use python3 virtual environment in Ubutnu:
1. Install virtual environment lib for python3 with command:
pip3 install virtualenv
2. Create your virtual environment:
python3 -m venv venv
3. Activate it:
source venv/bin/activate
Works correct for me in Ubutnu 16.04

Categories