Tox installing external libraries in tox env not from requirements.txt - python

I use windows. My tox.ini:
[tox]
envlist =
docs
min_version = 4
skipsdist = True
allowlist_externals = cd
passenv =
HOMEPATH
PROGRAMDATA
basepython = python3.8
[testenv:docs]
changedir = docs
deps =
-r subfolder/package_name/requirements.txt
commands =
bash -c "cd ../subfolder/package_name/ && pip install ."
Within a tox environment, I install some additional packages. I do it as above in tox's commands.
However, tox is installing a library in WSL's python instance path:
/home/usr/.local/lib/python3.8/site-packages
Not in tox's environment place for libraries:
.tox/env/lib/site-packages
I trigger tox inside the python virtual environment. If I run it in Docker everything works fine.
I use Windows. Besides WSL I have two python versions installed on my Windows: 3.8 and the main one 3.10. However, the virtual env that I'm using for tox is with 3.8. But I tested it with 3.10 and I receive the same result. In the system variables' path they are in such order and on the top of the list:
C:\Users\UserName\AppData\Local\Programs\Python\Python310\Scripts
C:\Users\UserName\AppData\Local\Programs\Python\Python310
C:\Users\UserName\AppData\Local\Programs\Python\Python38\Scripts
C:\Users\UserName\AppData\Local\Programs\Python\Python38\
As I found in the tox documentation:
Name or path to a Python interpreter which will be used for creating
the virtual environment, first one found wins. This determines in
practice the Python for what we’ll create a virtual isolated
environment.
So:
I'm trying to understand and fix the way how tox picks up the path to the python instance. As a result it should install libraries inside tox environment in libs folder.

You should install additional Python packages via the deps directive, see the example in our documentation:
https://tox.wiki/en/latest/config.html#tox-ini
P.S.: I am one of the tox maintainers

Found a solution. Silly mistake:
Use python instead of bash to install libraries.
commands =
python -m pip install [path_to_library]
As simple.

Related

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

How to create virtual env with python3

I am using python 2.7 + virtualenv version 1.10.1 for running myproject projects. Due to some other projects requirement I have to work with other version of python(Python 3.5) and Django 1.9. For this I have installed python in my user directory. Also I have dowloaded and installed virtualenv( version - 15.1.0) into my user directory.
But whenever I am trying to create virtual env I am getting the below error
python virtualenv/virtualenv.py myproject
Using base prefix '/home/myuser/python3'
New python executable in /home/mount/myuser/project_python3/myproject/bin/python
ERROR: The executable /home/mount/myuser/project_python3/myproject/bin/python is not functioning
ERROR: It thinks sys.prefix is '/home/myuser/python3' (should be '/home/mount/myuser/project_python3/myproject')
ERROR: virtualenv is not compatible with this system or executable
Can anybody tell what I am doing wrong with this
In Python 3.6+, the pyvenv module is deprecated. Use the following one-liner instead:
python3 -m venv <myenvname>
This is the recommended way to create virtual environments by the Python community.
To create virtual env
virtualenv -p python3 venv_name
This will create new python executable in baseDirectory/bin/python3
How to activate newely created Venv:
cd baseDirectory/bin/
source activate
Deactivate new venv
deactivate
UPDATE_1
This method has been depreciated as The use of venv is now recommended for creating virtual environments.
Please check this link for updated answer
Python already ships with its builtin "virtualenv" called venv since version 3.3. You no longer need to install or download the virtualenv scripts for Python 3.3+.
https://docs.python.org/3/library/venv.html
Check that your installation provided the pyvenv command that should take care of creating the "virtualenv". Arguments are similar to the classic virtualenv project.
$ pyvenv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
[--upgrade] [--without-pip]
ENV_DIR [ENV_DIR ...]
Creates virtual Python environments in one or more target directories.
positional arguments:
ENV_DIR A directory to create the environment in.
optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks
are not the default for the platform.
--copies Try to use copies rather than symlinks, even when
symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it
already exists, before environment creation.
--upgrade Upgrade the environment directory to use this version
of Python, assuming Python has been upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual
environment (pip is bootstrapped by default)
Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.
virtualenv is the tool of choice for Python 2, while venv handles the task in Python 3.
Yet you can create the virtual environment for Python 3 using any of them.
Using venv
python3 -m venv virtualenvname
Command Syntax:
/path/to/python3 -m venv /path/to/directory/virtual_env_name
Using virtualenv
virtualenv -p python3 virtualenvname
Command Syntax:
virtualenv -p /path/to/python3 /path/to/directory/virtual_env_name
Activate the virtual environment
On Linux, Unix or MacOS, using the terminal or bash shell:
source /path/to/venv/bin/activate
e.g. source virtualenvname/bin/activate
On Unix or MacOS, using the csh shell:
source /path/to/venv/bin/activate.csh
On Unix or MacOS, using the fish shell:
source /path/to/venv/bin/activate.fish
On Windows using the Command Prompt:
path\to\venv\Scripts\activate.bat
On Windows using PowerShell:
path\to\venv\Scripts\Activate.ps1
Deactivating the virtual environment
On Linux, Unix or MacOS, using the terminal or bash shell:
deactivate
On Windows using the Command Prompt:
path\to\venv\Scripts\deactivate.bat
On Windows using PowerShell:
deactivate
This answer is for those who may use a different OS.
Since the launch of Python version 3.3, there has been no need to download the virtualenv package separately as it comes built-in in Python.
Refer to the documentation to gain complete insights on it.
Test the installation of virtualenv:
$ virtualenv --version
Usage:
1.Creating a virtual environment:
$ virtualenv --system-site-packages -p python3 ./virtual_env_name
2.For enabling it, use the following command:
$ source ./virtual_env_name/bin/activate
3.For disabling the virtual environment and get back to working with the local environment:
$ deactivate
For listing down the packages in the virtual environment, use the
following command:
$ pip3 list
I install it using the command (for Python 3.x),
$ python3 -m venv env
To create a virtual environment in python3:
virtualenv -p /usr/bin/python3 virtualenvname
After creating the virtual environment, we need to activate it using the below command:
source virtualenvname/bin/activate
to deactivate use the below command:
deactivate
If you are on windows.
manually download and install the version of python you want from the official site
after installation, search "python" to locate the folder, so you can identify the path
get the path of the .exe (for example: C:\Users\Path\Programs\Python\Python38\python.exe)
Inside the folder of which you want to create the environment...start bash or VSCode terminal, or whatever command prompt, type [python .exe path] -m venv [env name] like this:
C:/Users/Path/Programs/Python/Python38/python.exe -m venv myenv
(Note that you have to change forward slash to backward slash for the path on step 4)
Activate the environment like so:
source myenv/Scripts/activate
Install virtualenvwrapper on top of virtualenv to simplify things.
Follow the blog to install in easy steps: virtualenvwrapper
Steps to create it:
mkvirtualenv -p /usr/bin/python3
Install packages using - pip install package_name
workon - activates the virtualenv, deactivate - deactivates the viirtualenv

Python - Distributing Library With Source

I'm writing a program that uses some cryptography for a class. Since I'm low on time, I'd like to go with Python for this assignment. The issue that I run into is that the code must be able to work on the Linux machines at the school. We are able to SSH into those machines and run the code, but we aren't allowed to install anything. I'm using the Cryptography library for Python:
pip install cryptography
Is there a straightforward way that I can include this with my .py file such that the issue of not being able to install the library on the Linux machines won't be a problem?
You have few options:
virtualenv
Install into virtualenv (assuming command virtualenv is installed):
$ cd projectdir
$ virtualenv venv
$ source venv/bin/activate
(venv)$ pip install cryptography
(venv)$ vim mycode.py
(venv)$ python mycode.py
The trick is, you install into local virtual environment, which does not
requires root priviledges.
tox
tox is great tool. After investing a bit of time, you can easily create multiple virtualenvs.
It assumes, you have tox installed in your system.
$ tox-quickstart
$ ...accept all defaults
$ vim tox.ini
The tox.ini my look like:
[tox]
envlist = py27
skipsdist = true
[testenv]
commands = python --version
deps =
cryptography
then run (with virtualenvs being deactivated):
$ tox
it will create virtualenv in directory .tox/py27
Activate it (still being in the same dir):
$ source .tox/py27/bin/activate
(py27)$ pip freeze
cryptography==1.2.2
... and few more...
Install into --user python profile
While this allows installing without root priviledges, it is not recommended as
it soon ends in one big mess.
EDIT (reaction to MattDMo comment):
If one user has two project with conflicting requirements (e.g. different
package versions), --user installation will not work as the packages are
living in one scope shared across all user projects.
With virtualenvs you may keep virtualenv inside of project folders and feel
free to destroy and recreate or modify any of them without affecting any other
project.
Virtualenvs have no problem with "piling up": if you can find your project
folder, you shall be able to find and manage related virtualenv(s) in it.
Use of virtualenv became de-facto recommended standard. I remember numerous
examples starting with creating virtualenv, but I cannot remember one case
using $ pip install --user.

How do I use pytest with virtualenv?

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

Categories