Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Image Export Fails If Kaleido is Installed in a virtualenv with Spaces in the Path #78

Closed
dtoniolo opened this issue Mar 4, 2021 · 14 comments · Fixed by #199
Closed
Labels
bug something broken P3 not needed for current cycle

Comments

@dtoniolo
Copy link

dtoniolo commented Mar 4, 2021

Versions
Platform: macOS Catalina 10.15.7
Python: 3.8.5
Plotly: 4.14.3
Kaleido: 0.2.0

Description of the Problem
If kaleido is installed in a virtual environment with spaces in aitspath, an error will be generated. Here's a minimal reproducible example:

import numpy as np
import plotly.graph_objects as go

a = np.arange(10)
fig = go.Figure(go.Scatter(x=a, y=a))
fig.write_image('Foo.pdf')

Running this will produce the following error trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-c7394f7c2527> in <module>
----> 1 fig.write_image('Foo.png')

/path with venv with spaces/lib/python3.8/site-packages/plotly/basedatatypes.py in write_image(self, *args, **kwargs)
   3804         import plotly.io as pio
   3805
-> 3806         return pio.write_image(self, *args, **kwargs)
   3807
   3808     # Static helpers

/path with venv with spaces/lib/python3.8/site-packages/plotly/io/_kaleido.py in write_image(fig, file, format, scale, width, height, validate, engine)
    243     # -------------
    244     # Do this first so we don't create a file if image conversion fails
--> 245     img_data = to_image(
    246         fig,
    247         format=format,

/path with venv with spaces/lib/python3.8/site-packages/plotly/io/_kaleido.py in to_image(fig, format, width, height, scale, validate, engine)
    129     # ---------------
    130     fig_dict = validate_coerce_fig_to_dict(fig, validate)
--> 131     img_bytes = scope.transform(
    132         fig_dict, format=format, width=width, height=height, scale=scale
    133     )

/path with venv with spaces/lib/python3.8/site-packages/kaleido/scopes/plotly.py in transform(self, figure, format, width, height, scale)
    151         # Transform in using _perform_transform rather than superclass so we can access the full
    152         # response dict, including error codes.
--> 153         response = self._perform_transform(
    154             figure, format=format, width=width, height=height, scale=scale
    155         )

/path with venv with spaces/lib/python3.8/site-packages/kaleido/scopes/base.py in _perform_transform(self, data, **kwargs)
    291         """
    292         # Ensure that kaleido subprocess is running
--> 293         self._ensure_kaleido()
    294
    295         # Perform export

/path with venv with spaces/lib/python3.8/site-packages/kaleido/scopes/base.py in _ensure_kaleido(self)
    196                             self._get_decoded_std_error()
    197                         )
--> 198                         raise ValueError(message)
    199                     else:
    200                         startup_response = json.loads(startup_response_string)

ValueError: Failed to start Kaleido subprocess. Error stream:

/path with venv with spaces/lib/python3.8/site-packages/kaleido/executable/kaleido: line 4: cd: /path: No such file or directory
/path with venv with spaces/lib/python3.8/site-packages/kaleido/executable/kaleido: line 5: ./bin/kaleido: No such file or directory
@jonmmease jonmmease added the bug something broken label Mar 4, 2021
@jonmmease
Copy link
Collaborator

Thanks for the report

@mherndon7
Copy link

mherndon7 commented Mar 5, 2021

I am having the same issues with the same package versions, except I am using Windows, with Anaconda. My conda environment is located in a spot with spaces ('C:\Path\with spaces") and the error I receive is:

"C:\Path\with" is not recognized as an internal or external command, operable program or batch file.

@huni1023
Copy link

i have similar issue.

i've already installed package kaleido using "pip install -U kaleido"
but it occurs error msg.

my code
fig1.to_image(f"{os.getcwd()[:-23]}/코드개발/0630_그림저장/{subject_idx}_fig1.png", engine='kaleido')

error msg

ValueError: 
Image export using the "kaleido" engine requires the kaleido package,
which can be installed using pip:
    $ pip install -U kaleido

ver. plotly: 4.12.0
kaleido: 0.2.1

@dgoppenheimer
Copy link

@huni1023, I had the same issue running kaleido on Google Colab. I ran !pip install -U kaleido, and got Successfully installed kaleido-0.2.1. But when I ran fig.write_image("fig1.png") I got the same error as you.

I solved it by running !pip uninstall kaleido (returned: WARNING: Skipping kaleido as it is not installed.) followed by

!pip install -q condacolab
import condacolab
condacolab.install()

Kernel restart,
!conda install -c conda-forge python-kaleido
Then fig.write_image("fig1.png") worked perfectly.

ver. Plotly: 5.5.0

@tran-david
Copy link

Thanks for this useful conversation guys, @dgoppenheimer solution worked for me as well.

It seems getting Kaleido installed through conda rather than pip helped resolve my issue, too.

@drew-hill
Copy link

drew-hill commented Mar 30, 2022

I have what seems to be the same problem, which I may have tagged onto a less-relevant Issue here: #131 (comment)

I am using a virtual environment through "pipenv", and a recent update in homebrew pipenv and python 3.9 (3.9.10 -> 3.9.12) seems to have surfaced this bug for me. When I move the project to a location with no spaces in the dir, it works just fine, but I cannot recreate @dgoppenheimer's fix in my pipenv virtual environment (using either pipenv or pip to uninstall/install). Downgrading versions of plotly (5.6.0 -> 5.5.0) and kaleido ( 0.2.1 --> 0.2.0) did not improve things.

Has anyone else run into this and/or found a fix?

kaleido==0.2.1
pipenv==2022.3.28
plotly==5.6.0
python==3.9.12
Mac OS Monterey (12.3), Intel chip

Output of "python3 -m pip show kaleido":
Name: kaleido
Version: 0.2.1
Summary: Static image export for web-based visualization libraries with zero dependencies
Home-page: UNKNOWN
Author: Jon Mease
Author-email: [email protected]
License: MIT
Location: <path to .venv>/lib/python3.9/site-packages
Requires:
Required-by:

@ZacksAmber
Copy link

ZacksAmber commented Mar 30, 2022 via email

@drew-hill
Copy link

Thanks @ZacksAmber. Unfortunately, I am working on a project that currently requires a virtual environment.

On the other hand, you did inspire me to confirm that this bug is indeed only in the virtual environment space -- running in my user environment works just fine.

@ZacksAmber
Copy link

ZacksAmber commented Mar 30, 2022 via email

@portfedh
Copy link

I just wanted to add myself to this problem. I'm having the same Issue.

"name": "ValueError",
"message": "Failed to start Kaleido subprocess. Error stream:<my_path>/venv_mac/lib/python3.9/site-packages/kaleido/executable/kaleido: line 4: cd: <my_path>: No such file or directory\n<my_path>/venv_mac/lib/python3.9/site-packages/kaleido/executable/kaleido: line 5: ./bin/kaleido: No such file or directory\n"

Im going to stop using a virtual enviroment for now.

@Tammura
Copy link

Tammura commented Sep 30, 2023

Hi all, any news on this thread? Actually I got the same error after more than 2 years from @dtoniolo report!

@milesgpt
Copy link

happening to me too still.

@MadsLang
Copy link

MadsLang commented Jul 17, 2024

I also ran into this issue, and for me the solution was in the title of this issue. I was using a virtual environment with poetry inside a folder with spaces (like "My folder/.venv"). The error was fixed by simply removing spaces in the folder path to something like "My_folder/.venv". Maybe it is obvious, but for me this solution is a lot better than installing plotly and kaleido in your global environment.

Running on Ubuntu with:
Poetry (version 1.8.3)
Python 3.10.12
plotly==5.22.0
kaleido==0.2.1

@BlueDrink9
Copy link
Contributor

An easy in-place fix is to quote the paths in the kaleido script in venv/lib/python3.x/site-pakacegs/kaleido/executable/kaleido, i.e.:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

export LD_LIBRARY_PATH="$DIR/lib:$LD_LIBRARY_PATH"
export FONTCONFIG_PATH="$DIR/etc/fonts"
export XDG_DATA_HOME="$DIR/xdg"
unset LD_PRELOAD

cd "$DIR"
./bin/kaleido "$@"

@gvwilson gvwilson self-assigned this Jul 26, 2024
@gvwilson gvwilson removed their assignment Aug 3, 2024
@gvwilson gvwilson added the P3 not needed for current cycle label Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P3 not needed for current cycle
Projects
None yet
Development

Successfully merging a pull request may close this issue.