Reputation: 3535
I created a package, containing Pipfile, and I want to test it with Docker.
I want to install packages listed in Pipfile with pip, without creating virutalenv.
# (do something to create some-file)
RUN pip install (some-file)
How can I do this?
Upvotes: 7
Views: 11761
Reputation: 15653
One of the other answers lead to me to this, but wanted to explicitly call it out, and why it's a useful solution.
Pipenv is useful as it helps you create a virtual environment. This is great on your local dev machine as you will often have many projects, with different dependencies etc.
In CICD, you will be using containers that are often are only spun up for a few minutes to complete part of your CICD pipeline. Since you will spin up a new container each time you run your pipeline, there is no need to create a virtual environment in your container to keep things organised. You can simply install all your dependencies directly to the main OS version of python.
To do this, run the below command in your CICD pipeline:
pipenv install --system
Upvotes: 7
Reputation: 131590
Eventually pip should be able to do this itself, at least that's what they say. Currently, that is not yet implemented.
For now, a Pipfile is a TOML file, so you can use a TOML parser to extract the package constraints and emit them in a format that will be recognized by pip. For example, if your Pipfile contains only simple string version specifiers, this little script will write out a requirements.txt
file that you can then pass to pip install -r
:
import sys
import toml
with open(sys.argv[1]) as f:
result = toml.load(f)
for package, constraint in result['packages'].items():
if constraint == '*':
print(package)
else:
print(f'{package} {constraint}')
If your Pipfile contains more complicated constructs, you'll have to edit this code to account for them.
An alternative that you might consider, which is suitable for a Docker container, is to use pipenv
to install the packages into the system Python installation and just remove the generated virtual environment afterwards.
pipenv install --system
pipenv --rm
However, strictly speaking that doesn't achieve your stated goal of doing this without creating a virtualenv.
Upvotes: 4