Creating python virtualenv in jenkins with shiningpanda and requirements.txt - python

I have some problems with jenkins and creating a virtualenv. I'm using the shiningpanda plugin and the "Virtualenv Builder" build step combined with pyenv.
I can install packages with "pip install package" but I cannot install requirements from a requirements file, because the subsequent packages cannot find the installed packages, e.g. numexpr cannot find/import numpy.

As I was typing my question, I found the answer to that problem: The current version (v0.21) of the shiningpanda plugin does NOT support pip's requirements.txt in virtualenv builders.
https://wiki.jenkins-ci.org/display/JENKINS/ShiningPanda+Plugin

Current version (0.23) works in our setup like this (in Build-Virtualenv Builder, with Nature: Shell):
pushd %run_dir%
SET PYTHONPATH=%CD%
python -m pip install --upgrade -r configurations/requirements.txt
This has worked well even if libraries require each other.

Related

Why is the wheel package not installed by default in Python 3.6?

Here is my problem: I need to distribute Python packages that I created. I would like to create wheels, as this is now the preferred way of distributing Python packages.
On my machine: no problem.
On my client server, however, I do not have control over the Python (3.6.3) used to create the wheels. And - surprise! - the wheel package is not included by default in Python 3.6!
And yes, I know I can do: sudo pip install wheel but I do not have sudo rights in that environment.
I could create a virtualenv, install wheel in that virtual environment, ans then create my packages (and I will probably end up doing just that), but what a pain in the neck!!
Am I missing something here?
If not, there is an inconsistency, in my mind: on the one hand, we are told to use wheels, but on the other hand the "preferred" mechanism is not available in a vanilla Python (at least in Python 3.6)
Any thoughts on that?
There is a an intermediate between installing packages system-wise via sudo pip and installing in virtual environment: install packages for your user!
$ pip --user install --upgrade pip wheel
(in some platforms pip automatically selects --user when invoked without sudo)
Packages that ship binaries (such as pip and wheel) will have them installed by default at ~/.local/bin, so make sure that dir is in your $PATH. The default /etc/profile or ~/.profile in most distros already do that if dir exists, so you might have to logout/login once for $PATH to be updated after installing your first package.
Now you can enjoy wheel (and latest pip) just like as if they were any other system package, and without any the of trouble of dealing with virtualenvs.

Record python version to rebuild virtualenv

The convention to record package requirements for future installation in a python virtual environment is to use pip freeze > requirements.txt and then install using pip install -r requirements.txt
The python interpreter version, however, is not recorded in the requirements.txt file.
Is there a similar convention to record the python version so that the entire virtual env, including the python interpreter used, can be easily rebuilt?
You can use pipenv for virtualenv creation and package management needs, e.g.:
pipenv --python 3.6
pipenv --python 3
See the documentation:
https://pipenv.kennethreitz.org/basics/#specifying-versions-of-python
That way, when you'll recreate the env with pipenv, it'll use the specified Python version and install the required packages.
Such requirements cannot be written in requirements.txt or setup.py: when one runs
pip install -r requirements.txt
pip is already being run with some version of Python.
Python version requirement could be implemented in a script that creates and populates a virtual env.
But best of all it must be written in the docs!

How can I upgrade Python version and packages in pyenv virtualenv?

I used pyenv, pyenv-virtualenv for managing python virtual environment.
I have a project working in Python 3.4 virtual environment.
So all installed packages(pandas, numpy etc) are not newest version.
What I want to do is to upgrade Python version from 3.4 to 3.6 as well as upgrade other package version to higher one.
How can I do this easily?
Here is how you can switch to 3.9.0 for a given virtual environement venv-name:
pip freeze > requirements-lock.txt
pyenv virtualenv-delete venv-name
pyenv virtualenv 3.9.0 venv-name
pip install -r requirements-lock.txt
Once everything works correctly you can safely remove the temporary requirements lock file:
rm requirements-lock.txt
Note that using pip freeze > requirements.txt is usually not a good idea as this file is often used to handle your package requirements (not necessarily pip freeze output). It's better to use a different (temporary) file just to be sure.
Use pip freeze > requirements.txt to save a list of installed packages.
Create a new venv with python 3.6.
Install saved packages with pip install -r requirements.txt. When pip founds an universal wheel in its cache it installs the package from the cache. Other packages will be downloaded, cached, built and installed.
OP asked to upgrade the packages alongside Python. No other answers address the upgrade of packages. Lock files are not the answer here.
Save your packages to a requirements file without the version.
pip freeze | cut -d"=" -f1 > requirements-to-upgrade.txt
Delete your environment, create a new one with the upgraded Python version, then install the requirements file.
pyenv virtualenv-delete venv-name
pyenv virtualenv 3.6.8 venv-name
pip install -r requirements-to-upgrade.txt
The dependency resolver in pip should try to find the latest package. This assumes you have the upgrade Python version installed (e.g., pyenv install 3.6.8).
If you use anaconda, just type
conda install python==$pythonversion$

Equivalent in python of package.json and "npm install --save" command to easly save every new package [duplicate]

I've been looking around for a package manager that can be used with python. I want to list project dependencies in a file.
For example ruby uses Gemfile where you can use bundle install.
How can I achieve this in Python?
The pip tool is becoming the standard in equivalent of Ruby's gems.
Like distribute, pip uses the PyPI package repository (by default) for resolving and downloading dependencies.
pip can install dependencies from a file listing project dependencies (called requirements.txt by convention):
pip install -r requirements.txt
You can "freeze" the current packages on the Python path using pip as well:
pip freeze > requirements.txt
When used in combination with the virtualenv package, you can reliably create project Python environments with a project's required dependencies.
Pipenv
(I know it's an old question, and it already has an answer but for anyone coming here looking for a different answer like me.)
I've found a very good equivalent for npm, It's called pipenv. It handles both virtualenv and pip requirements at the same time so it's more like npm.
Simple Use Case
pip install pipenv
then you can make a new virtualenv with third version of python, as well as making a pipfile that will be filled with your projects requirement and other stuff:
pipenv install --three
using your created virtualenv:
pipenv shell
installing a new python package:
pipenv install requests
running your .py file is like:
pipenv run python somefile.py
you can find it's doc here.
Python uses pip for a package manager. The pip install command has a -r <file> option to install packages from the specified requirements file.
Install command:
pip install -r requirements.txt
Example requirements.txt contents:
Foo >= 1.2
PickyThing <1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1
SomethingWhoseVersionIDontCareAbout
See the Requirements Parsing section of the docs for a full description of the format: https://pip.pypa.io/en/stable/user_guide/#requirements-files
This is how I restrict pip's scope to the current project. It feels like the opposite if you're coming from NodeJS's npm or PHP's composer where you explicitly specify global installations with -g or --global.
If you don't already have virtualenv installed, then install it globally with:
pip install virtualenv
Each Python project should have its own virtualenv installation. It's easy to set one up, just cd to your project's root and:
python3 -m virtualenv env # creates env folder with everything you need
Activate virtualenv:
source env/bin/activate
Now, any interaction with pip is contained within your project.
Run pip install package_name==version for each of your dependencies. They are installed in ./env/lib/python3.x/site-packages/
When you want to save your project's dependencies to a file, run:
pip freeze > requirements.txt
You actually don't need -l or --local if you're in an activated project-specific virtualenv (which you should be).
Now, when you want to install your dependencies from requirements.txt, set up your virtualenv, and run:
pip install -r requirements.txt
That's all.
This is an old question but things are constantly evolving.
Further to the other answer about pipenv. There is also a python package manger called poetry.
There is a detailed comparison between pipenv and poerty here: Feature comparison between npm, pip, pipenv and poetry package managers. It also links the features to common npm features.
Here is a comparison of pipenv vs poetry vs pdm: https://dev.to/frostming/a-review-pipenv-vs-poetry-vs-pdm-39b4
The conclusion is that pdm is the winner.
But in my experience, poetry is easier than pdm to integrate with IDEs.

Is there an automated way to install python modules (à la rubygems)?

I'm new to python. Are there alternatives to downloading a tarball for a python module & installing it via python setup install ? Anything like rubygems?
UPDATE I'm surprised that there are so many solutions for this one problem.
setuptools is one option. Install that and then you can install many Python modules using the easy_install command-line tool.
There are many options - you can stick with your system's default packaging system (if it has one) or you can pick one (or more) of existing Python tools like easy_install, zc.buildout or pip. I would recommend you to use Distribute together with pip.
easy_install or pip. I also recommend checking out virtualenv to isolate environments for test running packages. The Python Package Index(pypi, also called Cheeseshop) is the official third-party software repository for Python.
Pip is great:
$ pip install some_python_module
$ pip freeze > requirements.txt
$ cat requirements.txt
Output from freeze:
Creoleparser==0.7.3
Django==1.3
Genshi==0.6
PIL==1.1.7
South==0.7.3
django-debug-toolbar==0.8.5
....
After this in any other place:
$ pip install < requirements.txt
Checkout pip

Categories

Resources