I am learning how to run containerized PyTests and I am failing to run a test with arguments.
My Dockerfile looks like this:
FROM python:2
ADD main.py /
RUN pip install docker
RUN pip install fake_useragent
RUN pip install pytest
RUN pip install requests
CMD ["pytest", "main.py --html=report.html"]
But I tried all kinds of CMD/RUN variations I found online.
Anybody has a clue?
The full project is here if helps:
https://github.com/pavelzag/DockerSDKLearn
"main.py --html=report.html" will be passed in pytest as a single argument and will appear in sys.argv[1] there. Hence pytest is trying to locate a file with the exact same name with stuff like --html in it. You should fully tokenize the command:
CMD ["pytest", "main.py", "--html=report.html"]
Related
i am trying to build a container for my express.js application. The express.js-app makes use of python via the npm package PythonShell.
I have plenty of python-code, which is in a subfolder of my express-app and with npm start everything works perfectly.
However, i am new to docker and i need to containerize the app. My Dockerfile looks like this:
FROM node:18
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD ["node", "./bin/www"]
I built the Image with:
docker build . -t blahblah-server and ran it with docker run -p 8080:3001 -d blahblah-server.
I make use of imports at the top of the python-script like this:
import datetime
from pathlib import Path # Used for easier handling of auxiliary file's local path
import pyecma376_2 # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class.
from assi import model
When the pythonscript is executed (only in the container!!!) I get following error-message:
/usr/src/app/public/javascripts/service/pythonService.js:12
if (err) throw err;
^
PythonShellError: ModuleNotFoundError: No module named 'pyecma376_2'
at PythonShell.parseError (/usr/src/app/node_modules/python-shell/index.js:295:21)
at terminateIfNeeded (/usr/src/app/node_modules/python-shell/index.js:190:32)
at ChildProcess.<anonymous> (/usr/src/app/node_modules/python-shell/index.js:182:13)
at ChildProcess.emit (node:events:537:28)
at ChildProcess._handle.onexit (node:internal/child_process:291:12)
----- Python Traceback -----
File "/usr/src/app/public/pythonscripts/myPython/wtf.py", line 6, in <module>
import pyecma376_2 # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class. {
traceback: 'Traceback (most recent call last):\n' +
' File "/usr/src/app/public/pythonscripts/myPython/wtf.py", line 6, in <module>\n' +
' import pyecma376_2 # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class.\n' +
"ModuleNotFoundError: No module named 'pyecma376_2'\n",
executable: 'python3',
options: null,
script: 'public/pythonscripts/myPython/wtf.py',
args: null,
exitCode: 1
}
If I comment the first three imports out, I get the same error:
PythonShellError: ModuleNotFoundError: No module named 'assi'
Please notice, that assi actually is from my own python-code, which is included in the expressjs-app-directory
Python seems to be installed in the container correctly. I stepped inside the container via docker exec -it <container id> /bin/bash and there are the python packages in the #/usr/lib-directory.
I really have absolute no idea how all this works together and why python doesn't find this modules...
You are trying to use libs that are not in Standard Python Library. It seems that you are missing to run pip install , when you build the docker images.
Try adding RUN docker commands that can do this for you. Example:
RUN pip3 install pyecma376_2
RUN pip3 install /path/to/assi
Maybe, that can solve your problem. Don't forget to check if python are already installed in your container, it semms that it is. And if you have python2 and pyhton3 installed, make sure that you use pip3 instead of only pip.
I want my users to be able to reference a file in my python package (specifically a docker-compose.yml file) directly from the shell.
I couldnt find a way to get only the location from pip show (and grep-ing out "location" from its output feels ugly), so my current (somewhat verbose) solution is:
docker compose -f $(python3 -c "import locust_plugins; print(locust_plugins.__path__[0])")/timescale/docker-compose.yml up
Is there a better way?
Edit: I solved it by installing a wrapper command I call locust-compose as part of the package. Not perfect, but it gets the job done:
#!/bin/bash
module_location=$(python3 -c "import locust_plugins; print(locust_plugins.__path__[0])")
set -x
docker compose -f $module_location/timescale/docker-compose.yml "$#"
Most of the support you need for this is in the core setuptools suite.
First of all, you need to make sure the data file is included in your package. In a setup.cfg file you can write:
[options.package_data]
timescale = docker-compose.yml
Now if you pip install . or pip wheel, that will include the Compose file as part of the Python package.
Next, you can retrieve this in Python code using the ResourceManager API:
#!/usr/bin/env python3
# timescale/compose_path.py
import pkg_resources
if __name__ == '__main__':
print(pkg_resources.resource_filename('timescale', 'docker-compose.yml'))
And finally, you can take that script and make it a setuptools entry point script (as distinct from the similarly-named Docker concept), so that you can just run it as a single command.
[options.entry_points]
console_scripts=
timescale_compose_path = timescale:compose_path
Again, if you pip install . into a virtual environment, you should be able to run timescale_compose_path and get the path name out.
Having done all of those steps, you can finally run a simpler
docker-compose -f $(timescale_compose_path) up
so effectively I am tryinning to run a Django website that i can share the link. The issue is the RUN file with the .replit file. I need to pip install crispy forms then run the server. How can this be accomplished in the .replit file?
tried the following:
language = "python3"
run = "pip install django-crispy-forms"
run = "python manage.py runserver 0.0.0.0:3000"
You can't have multiple run lines. Poetry (built-in, no setup required) should automatically install the modules you need, but in case it doesn't try this instead:
language = "python3"
run = "pip install django-crispy-forms && python manage.py runserver 0.0.0.0:3000"
I have installed allure 2.8.17. It is on a pipenv environment. When I am running the below command in terminal:
behave -f allure_behave.formatter:AllureFormatter -o reports/
It is failing with the below error
usage: behave [options] [ [DIR|FILE|FILE:LINE] ]+
behave: error: format=allure_behave.formatter:AllureFormatter is unknown
(behave) sharathkrishnan#sharaths-mbp features %
I think you are missing allure-behave:
pip install allure-behave ?
you need to install using pip install allure-behave and then run the above command
This also commonly happens when you point the file path with a typo.
Mostly case sensitive problems like ./Reports instead of ./reports
I'm trying to install gcloud on my EC2 server running Amazon Linux 4.14.47-56.37 64bits, in interactive mode running the following command :
curl https://sdk.cloud.google.com | bash
The files download correctly, but the install then fails with the following Traceback :
File "/home/ec2-user/google-cloud-sdk/bin/bootstrapping/install.py", line 12, in <module>
import bootstrapping
File "/home/ec2-user/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 32, in <module>
import setup # pylint:disable=g-import-not-at-top
File "/home/ec2-user/google-cloud-sdk/bin/bootstrapping/setup.py", line 55, in <module>
from googlecloudsdk.core import properties
File "/home/ec2-user/google-cloud-sdk/lib/googlecloudsdk/core/properties.py", line 291
self.__sections = {section.name: section for section in sections}
^
SyntaxError: invalid syntax
Any idea why this for is causing issues?
I am running python 2.7 (2.7.14) as recommended by Google.
On top of python 2.7 installed on the "python" command, I also had python 2.6 installed on the "python2" command.
Uninstalling python 2.6 solved the issue, Google Cloud install went through without issue on the next try.
You locally update the install.sh file to use python2.7 instead of python2. This worked for me
In case someone meets this problem again, and if you do not want to uninstall python 2.6 or to modify install.sh, here is a procedure that worked for me on an ec2 instance :
open a new terminal and type :
curl https://sdk.cloud.google.com | bash
The install should not work as CLOUDSDK_PYTHON=python2 by default, which leads to Python 2.6. To correct this, enter this command :
export CLOUDSDK_PYTHON=python
this links CLOUDSDK_PYTHON to Python 3.6
then
bash google-cloud-sdk/install.sh
and finally
./google-cloud-sdk/bin/gcloud init
Once this is done, you can use any gcloud or gsutil command by doing this :
./google-cloud-sdk/bin/gcloud <your gcloud command>
./google-cloud-sdk/bin/gsutil <your gsutil command>
For example, in order to copy a folder from a gs bucket to your ec2 instance :
./google-cloud-sdk/bin/gsutil -m cp -r gs://<bucket_name>/path/to/folder /path/on/your/ec2/instance
the -m option allows fast transfer (I could transfer with an average speed of 81 MiB/s with this option)
Hope this helps !
Setup yum repo:
# Update YUM with Cloud SDK repo information:
sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM
# The indentation for the 2nd line of gpgkey is important.
Install yum downloader and download the rpm, then install with "--nodeps":
yum install yum-utils
yumdownloader google-cloud-sdk-231.0.0-1.el7.noarch
mv 23873bd6e8459ba6e70e96eb8f03f6ac03cd707ce3c80baa8264c714e030c915-google-cloud-sdk-231.0.0-1.el7.noarch.rpm /usr/local/src/google-cloud-sdk-231.0.0-1.el7.noarch
rpm -ivh --nodeps /usr/local/src/google-cloud-sdk-231.0.0-1.el7.noarch
This solved issue:
export CLOUDSDK_PYTHON=python