eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: When running my docker image, I get an import error: File "./app/main.py", line 8, in <module> import wekinator ModuleNotFoundError: No module named 'wekinator'`  How do I import local python modules in Docker? Wouldn't the COPY command copy the entire "app" folder (including both files), hence preserving the correct import location? . ├── Dockerfile ├── README.md └── app ├── main.py └── wekinator.py  FROM python:3.7 RUN pip install fastapi uvicorn python-osc EXPOSE 80 COPY ./app /app CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] 

Answer: Setting the PYTHONPATH as such works but feels clumsy: ENV PYTHONPATH "${PYTHONPATH}:/app/"  Using Docker's WORKDIR allows for a much cleaner solution: FROM python:3.7 WORKDIR /code RUN pip install fastapi uvicorn python-osc EXPOSE 80 COPY app ./app CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]  Note how the COPY statement changed.
Question: Building wheels for collected packages: argon2-cffi Building wheel for argon2-cffi (PEP 517) ... error ERROR: Command errored out with exit status 1: command: 'c:\users\prasa\appdata\local\programs\python\python39\python.exe' 'c:\users\prasa\appdata\local\programs\python\python39\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\prasa\AppData\Local\Temp\tmpcczeigwt' cwd: C:\Users\prasa\AppData\Local\Temp\pip-install-iele2h25\argon2-cffi Complete output (17 lines): running bdist_wheel running build running build_py creating build creating build\lib.win-amd64-3.9 creating build\lib.win-amd64-3.9\argon2 copying src\argon2\exceptions.py -> build\lib.win-amd64-3.9\argon2 copying src\argon2\low_level.py -> build\lib.win-amd64-3.9\argon2 copying src\argon2\_ffi_build.py -> build\lib.win-amd64-3.9\argon2 copying src\argon2\_legacy.py -> build\lib.win-amd64-3.9\argon2 copying src\argon2\_password_hasher.py -> build\lib.win-amd64-3.9\argon2 copying src\argon2\_utils.py -> build\lib.win-amd64-3.9\argon2 copying src\argon2\__init__.py -> build\lib.win-amd64-3.9\argon2 copying src\argon2\__main__.py -> build\lib.win-amd64-3.9\argon2 running build_clib building 'argon2' library error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ ---------------------------------------- ERROR: Failed building wheel for argon2-cffi Failed to build argon2-cffi ERROR: Could not build wheels for argon2-cffi which use PEP 517 and cannot be installed directly  I installed python 3.9 on my computer. When I try to install jupyter notebook I got this error. SO how to solve this issue ?

Answer: In case, you are a mac user on Intel CPUs, just check your pip version, if you are installing through the command : pip install notebook  Upgrade your PIP, the command that worked for me: /Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip  Check if its the latest version of PIP , by using command : pip --version  After this again, try : pip install notebook  This time you should not see any error. Then include ~/Library/Python/3.8/bin in your path variable. Check if its there by : echo $PATH  And then launch the jupyter notebook by command : jupyter notebook  Pls upvote, if you found this answer useful :)
Question: I have a found a Julia function that nicely does the job I need. How can I quickly integrate it to be able to call it from Python? Suppose the function is f(x,y) = 2x.+y  What is the best and most elegent way to use it from Python?

Answer: Assuming your Python and Julia are installed you need to take the following steps.  Run Julia and install PyCall using Pkg pkg"add PyCall"   Put your code into a Julia package using Pkg Pkg.generate("MyPackage")  In the folder src you will find MyPackage.jl, edit it to look like this: module MyPackage f(x,y) = 2x.+y export f end   Install pyjulia python -m pip install julia  (On Linux systems you might want to use python3 instead of python command) For this step note that while an external Python can be used with Julia. However, for a convenience it might be worth to consider using a Python that got installed together with Julia as PyCall. In that case for installation use a command such this one: %HOMEPATH%\.julia\conda\3\python -m pip install julia  or on Linux ~/.julia/conda/3/python -m pip install julia  Note that if you have JULIA_DEPOT_PATH variable defined you can replace %HOMEPATH%\.julia or ~/.julia/ with its value.  Run the appropiate Python and tell it to configure the Python-Julia integration: import julia julia.install()   Now you are ready to call your Julia code: >>> from julia import Pkg >>> Pkg.activate(".\\MyPackage") #use the correct path Activating environment at `MyPackage\Project.toml` >>> from julia import MyPackage >>> MyPackage.f([1,2],5) [7,9]    It is worth noting that the proposed approach in this answer has several advantages over a standalone Julia file which would be possible, although is less recommended. The advantages include:  Packages get precompiled (so they are faster in subsequent runs) and can be loaded as a package in Python. Packages come with their own virtual environment via 1Project.toml` which makes production deployments much comfortable. A Julia package can be statically compiled into Julia's system image which can slash itsloading time --- see https://github.com/JuliaLang/PackageCompiler.jl .  EDIT In February 2022 juliacall was announced, as of December 2022 juliacall might be an easier option for some users - have a look at: How to load a custom Julia package in Python using Python's juliacall
Question: This is a followup to this question on the basic functionality of Playwright for Python. How do I select an option from a drop down list? This example remote controls a vuejs-webseite that has a drop down list of fruits like "Apple", "Banana", "Carrot", "Orange" Here I want to select the option "Banana" from playwright import sync_playwright import time URL = '<my url>' with sync_playwright() as p: browser = p.chromium.launch(headless=False) page = browser.newPage() page.goto(URL) # identify this element by ID. Wait for it first new_selector = 'id=name-fruit' page.waitForSelector(new_selector) handle = page.querySelector(new_selector) # at this point I have the element and can print the content print(handle.innerHTML())  The drop down list HTML like this <select data-v-c2cef47a="" id="name-fruit" autocomplete="true" class="form-select__select"> <option data-v-c2cef47a="" disabled="disabled" value=""><!----></option> <option data-v-c2cef47a="" value="[object Object]"> Apple </option> <option data-v-c2cef47a="" value="[object Object]"> Banana </option> <option data-v-c2cef47a="" value="[object Object]"> Carrot </option> <option data-v-c2cef47a="" value="[object Object]"> Orange </option> </select>  in Selenium, I would select an option like this from selenium.webdriver.support.ui import Select Select(handle).select_by_visible_text('Banana') # Note: no spaces needed!  The Javascript docs for Playwright have this, which is not exactly the same, because it seems to identify the object at the same time. // Single selection matching the label await page.selectOption('select#colors', { label: 'Blue' });  How do I do the selection in Playwright for Python? I tried both of these and nothing happens. handle.selectOption("text=Banana") handle.selectOption("text= Banana ") 

Answer: After trying many different variants, I guessed a working syntax handle.selectOption({"label": "Banana"})
Question: I'm working with a json file in Python and I wanted to convert it into a dict. This is what my file looks like: [ { "label": "Label", "path": "/label-path", "image": "icon.svg", "subcategories": [ { "title": "Main Title", "categories": { "column1": [ { "label": "Label Title", "path": "/Desktop/Folder" } ] } } ] } ]  So this is what I did: import json # Opening JSON file f = open('file.json') # returns JSON object as a list data = json.load(f)  However, data is now a list, not a dict. I tried to think about how can I convert it to a dict, but (I) not sure how to do this; (II) isn't there a way of importing the file already as a dict?

Answer: Your data get's imported as list, because in your JSON file the main structure is an Array (squared brackets), which is comparable to a list in Python. If you want just inner dict you can do data = json.load(f)[0]
Question: Is there a way to get the name of the tag of a Selenium web element? I found, there is .getTagName() in selenium for Java, in How do I get a parent HTML Tag with Selenium WebDriver using Java?. Example: In this HTML, if I iterate though class='some_class_name', how can I get the tag_name (h2, p, ul or li)? <div class='some_class_name'> <h2>Some Random Heading 1</h2> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry</p> <h2>Some Random Heading 2</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <p>Random list are:</p> <ul> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ul> </div>  The Python code may look like this... content = driver.find_elements_by_xpath("//div[@class='some_class_name']//child::*") for c in content: print(c.getTagName()) # Something like this to get the # tag of inner content... 

Answer: In Python, you have tag_name to get the tag name. content = driver.find_elements_by_xpath("//div[@class='some_class_name']//child::*") for c in content: print(c.tag_name)
Question: I wanted to display the first 10 images from the mnist dataset with plotly. This is turning out to be more complicated than I thought. This does not work: import numpy as np np.random.seed(123) import plotly.express as px from keras.datasets import mnist (X_train, y_train), (X_test, y_test) = mnist.load_data() fig = subplots.make_subplots(rows=1, cols=10) fig.add_trace(px.imshow(X_train[0]), row=1, col=1)  as it results in ValueError: Invalid element(s) received for the 'data' property of Invalid elements include: [Figure({ 'data': [{'coloraxis': 'coloraxis', 'type': 'heatmap', 'z': array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)}], 'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111, '#46039f'], [0.2222222222222222, '#7201a8'], [0.3333333333333333, '#9c179e'], [0.4444444444444444, '#bd3786'], [0.5555555555555556, '#d8576b'], [0.6666666666666666, '#ed7953'], [0.7777777777777778, '#fb9f3a'], [0.8888888888888888, '#fdca26'], [1.0, '#f0f921']]}, 'margin': {'t': 60}, 'template': '...', 'xaxis': {'constrain': 'domain', 'scaleanchor': 'y'}, 'yaxis': {'autorange': 'reversed', 'constrain': 'domain'}} })] The 'data' property is a tuple of trace instances that may be specified as: - A list or tuple of trace instances (e.g. [Scatter(...), Bar(...)]) - A single trace instance (e.g. Scatter(...), Bar(...), etc.) - A list or tuple of dicts of string/value properties where: - The 'type' property specifies the trace type One of: ['area', 'bar', 'barpolar', 'box', 'candlestick', 'carpet', 'choropleth', 'choroplethmapbox', 'cone', 'contour', 'contourcarpet', 'densitymapbox', 'funnel', 'funnelarea', 'heatmap', 'heatmapgl', 'histogram', 'histogram2d', 'histogram2dcontour', 'image', 'indicator', 'isosurface', 'mesh3d', 'ohlc', 'parcats', 'parcoords', 'pie', 'pointcloud', 'sankey', 'scatter', 'scatter3d', 'scattercarpet', 'scattergeo', 'scattergl', 'scattermapbox', 'scatterpolar', 'scatterpolargl', 'scatterternary', 'splom', 'streamtube', 'sunburst', 'surface', 'table', 'treemap', 'violin', 'volume', 'waterfall'] - All remaining properties are passed to the constructor of the specified trace type (e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])  neither does fig.add_trace(go.Image(X_train[0]), row=1, col=1)  or fig.add_trace(go.Figure(go.Heatmap(z=X_train[0])), 1,1)  I am starting to run out of ideas. It should be possible to have a row of images as a header.

Answer: Using facets Please consider the following answer, which is much simpler: import plotly.express as px from keras.datasets import mnist (X_train, y_train), (X_test, y_test) = mnist.load_data() fig = px.imshow(X_train[:10, :, :], binary_string=True, facet_col=0, facet_col_wrap=5)  It produces the following output:  Using animation For exploration purposes and although this is not exactly what you are asking for, you can also make an animation and visualize one digit at a time: fig = px.imshow(X_train[:10, :, :], binary_string=True, animation_frame=0)   Source Imshow documentation
Question: I'm trying to run this code. I don't know what is wrong with it, but this code is not running. and I don't know how to solve this problem. import cv2 from facial_emotion_recognition import EmotionRecognition emotion_detector = EmotionRecognition(device='gpu', gpu_id=1) camera = cv2.VideoCapture(0) while True: image = camera.read()[1] image = emotion_detector.recognise_emotion(image, return_type='BGR') cv2.imshow('Camera', image) key = cv2.waitKey(1) if key == 27: break camera.release() cv2.destroyAllWindows()  but I'm getting this error: Traceback (most recent call last): File "/home/fahim/Documents/Python_projects/Python tutorials/pantech AI Master/Computer_Vision/Day 8 Face emotion recognition/emotion.py", line 4, in <module> emotion_detector = EmotionRecognition(device='gpu', gpu_id=1) File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/facial_emotion_recognition/facial_emotion_recognition.py", line 25, in __init__ self.network = NetworkV2(in_c=1, nl=32, out_f=7).to(self.device) File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 607, in to return self._apply(convert) File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 354, in _apply module._apply(fn) File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 354, in _apply module._apply(fn) File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 376, in _apply param_applied = fn(param) File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 605, in convert return t.to(device, dtype if t.is_floating_point() else None, non_blocking) RuntimeError: CUDA error: invalid device ordinal Process finished with exit code 1  This is my the configuration of my computer: GPU: NVIDIA GeForce MX130 CPU: Intel i5-10210U (8) @ 4.200GHz Help me to solve this please.

Answer: Try changing: emotion_detector = EmotionRecognition(device='gpu', gpu_id=1)  To: emotion_detector = EmotionRecognition(device='gpu', gpu_id=0)  gpu_id is only effective when more than one GPU is detected, you only seem to have one GPU, so it throws an error since you tell the function to get GPU 2 (since we count from 0).
Question: I'm trying to install a package on Python, but Python is throwing an error on installing packages. I'm getting an error every time I tried to install pip install google-search-api. Here is the error how can I successfully install it? error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/  I already updated that and have the latest version of 14.27 but the problem is throwing the same error.

Answer: Go to this link and download Microsoft C++ Build Tools: https://visualstudio.microsoft.com/visual-cpp-build-tools/  Open the installer, then follow the steps. You might have something like this, just download it or resume.  If updating above doesn't work then you need to configure or make some updates here. You can make some updates here too by clicking "Modify". Check that and download what you need there or you might find that you just need to update Microsoft Visual C++ as stated on the error, but I also suggest updating everything there because you might still need it on your future programs. I think those with the C++ as I've done that before and had a similar problem just like that when installing a python package for creating WorldCloud visualization.   UPDATE: December 28, 2020 You can also follow these steps here:  Select: Workloads → Desktop development with C++ Then for Individual Components, select only:  Windows 10 SDK C++ x64/x86 build tools    You can also achieve the same automatically using the following command: vs_buildtools.exe --norestart --passive --downloadThenInstall --includeRecommended --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Workload.MSBuildTools  Reference: https://www.scivision.dev/python-windows-visual-c-14-required
Question: x_train = x_train[..., tf.newaxis].astype("float32") x_test = x_test[..., tf.newaxis].astype("float32") Can someone please explain how tf.newaxis works ? I found a brief mention in the documentation https://www.tensorflow.org/api_docs/python/tf/strided_slice but I could not properly understand.

Answer: Check this example: a = tf.constant([100]) print(a.shape) ## (1) expanded_1 = tf.expand_dims(a,axis=1) print(expanded_1.shape) ## (1,1) expanded_2 = a[:, tf.newaxis] print(expanded_2.shape) ## (1,1)  It is similar to expand_dims() which adds a new axis. If you want to add a new axis at the beginning of the tensor, use expanded_2 = a[tf.newaxis, :]  otherwise (at the end) expanded_2 = a[:,tf.newaxis]
Question: it would be really helpful for me if you would help me understand some underlying concepts about Spacy. I understand some spacy models have some predefined static vectors, for example, for the Spanish models these are the vectors generated by FastText. I also understand that there is a tok2vec layer that generates vectors from tokens, and this is used for example as the input of the NER components of the model. If the above is correct, then I have some questions:  Does the NER component also use the static vectors?  If yes, then where does the tok2vec layer comes into play? If no, then is there any advantage on using the lg or md models if you only intend to use the model for e.g. the NER component?   Is the tok2vec layer already trained for pretrained downloaded models, e.g. Spanish? If I replace the NER component of a pretrained model, does it keep the tok2vec layer untouched i.e. with the learned weights? Is the tok2vec layer also trained when I train a NER model? Would the pretrain command help the tok2vec layer learn some domain-specific words that may be OOV?  Thanks a lot!

Answer:  Does the NER component also use the static vectors?  This is addressed in point 2 and 3 of my answer here.  Is the tok2vec layer already trained for pretrained downloaded models, e.g. Spanish?  Yes, the full model is trained, and the tok2vec layer is a part of it.  If I replace the NER component of a pretrained model, does it keep the tok2vec layer untouched i.e. with the learned weights?  No, not in the current spaCy v2. The tok2vec layer is part of the model, if you remove the model, you also remove the tok2vec layer. In the upcoming v3, you'll be able to separate these so you can in fact keep the tok2vec model separately, and share it between components.  Is the tok2vec layer also trained when I train a NER model?  Yes - see above  Would the pretrain command help the tok2vec layer learn some domain-specific words that may be OOV?  See also my answer at https://stackoverflow.com/a/63520262/7961860 If you have further questions - happy to discuss in the comments!
Question: Is there any way that I can install "pip" for "Python2.7" ? I could install python2.7 by sudo apt install python2-minimal  I tried installing pip for this. sudo apt install python-pip / python2-pip / python2.7-pip  but none worked. Can anybody have solution for this.

Answer: Pip for Python 2 is not included in the Ubuntu 20.04 repositories. Try this guide which suggests to fetch a Python 2.7 compatible get_pip.py and use that to bootstrap pip. curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
Question: The typical command to export a Anaconda environment to a YAML file is: conda env export --name my_env > myenv.yml However, one huge issue is the readbility of this file as it includes hard specifications for all of the libraries and all of their dependencies. Is there a way for Anaconda to export a list of the optimally smallest subset of commands that would subsume these dependencies to make the YAML more readable? For example, if all you installed in a conda environment was pip and scipy, is there a way for Anaconda to realize that the file should just read: name: my_env channels: - defaults dependencies: - scipy=1.3.1 - pip=19.2.3  That way, the anaconda environment will still have the exact same specification, if not an improved on (if an upstream bug is fixed) and anyone who looks at the yml file will understand what is "required" to run the code, in the sense that if they did want to/couldn't use the conda environment they would know what packages they needed to install?

Answer: Options from the Conda CLI This is sort of what the --from-history flag is for, but not exactly. Instead of including exact build info for each package, it will include only what are called explicit specifications, i.e., the specifications that a user has explicitly requested via the CLI (e.g., conda install scipy=1.3.1). Have a try: conda env export --from-history --name my_env > myenv.yml  This will only include versions if the user originally included versions during installation. Hence, creating a new environment is very likely not going to use the exact same versions and builds. On the other hand, if the user originally included additional constraints beyond version and build they will also be included (e.g., a channel specification conda install conda-forge::numpy will lead to conda-forge::numpy). Another option worth noting is the --no-builds flag, which will export every package in the YAML, but leave out the build specifiers. These flags work in a mutually exclusive manner.  conda-minify If this is not sufficient, then there is an external utility called conda-minify that offers some functionality to export an environment that is minimized based on a dependency tree rather than through the user's explicit specifications.
Question: I'm looking for a good way to convert .ipynb to .py files in VSCode. So far I've tried:  the "Export As" Option built into vscode. Not ideal as it produces the following at the start of the script, as well as "Run Cell", "Run Below", etc. buttons/links:   "To add a new cell, type '# %%' To add a new markdown cell, type '# %% [markdown]' %% from IPython import get_ipython"   nbconvert. I usually insert this as a command in the script itself (https://stackoverflow.com/a/19779226/14198216) so it's automatically converted and saved once run. But this also leaves "Run Cell" etc, as well as execution markings (ex: "In [1]")  jupytext. I also usually insert this as a command. At the start of the .py, it produces:    -- coding: utf-8 -- --- jupyter: jupytext: text_representation:  Is there a nice, minimalist solution that doesn't insert a bunch of gunk (which needs to be manually edited out) in the .py version of my notebooks and can be easily/automatically executed from the notebook itself? This could also be setting tweaks that I'm currently unaware of that can make one of the things I mentioned work better. Thanks in advance.

Answer: We can add the following settings in "settings.json", the generated python file will not have "Run Cell", "Run Above", "Debug Cell": "jupyter.codeLenses": " ",