Repository: streamlit/streamlit

Issue:
streamlit's webserver not working when using pdm for installation
### Summary

When I'm using [pdm](https://pdm.fming.dev/)  to install streamlit, I cannot connect to streamlit's webserver.
From pdm's web site: PDM is a modern Python package manager with PEP 582 support.

### Steps to reproduce

1. Install PDM: `pipx install pdm`
2. Create new directory and go there: `mkdir st_test; cd st_test`
3. Init pdm: `pdm init` (and use the defaults)
4. Install streamlit: `pdm add streamlit`
5. Use any minimal streamlit example and run streamlit: `pdm run streamlit run hello_world.py`

**Expected behavior:**

Browser opens correct web page served by streamlit.

**Actual behavior:**

Browser tries to connect to `localhost:3000` (which is also mentioned in the logs), but when I look at the logs, the server actually runs on port 8501. When I try this port, I get a 404, so I _can_ connect to the server on this port, but something's broken.

### Is this a regression?

That is, did this use to work the way you expected in the past?
never tried before

### Debug info

- Streamlit version: 0.80.0
- Python version: 3.8.8
- Using pdm
- OS version: Linux 5.11.11
- Browser version: Chrome 89.0.4389.114 (Official Build) (64-bit)

### Additional information

jupyter-lab show a similar issue with pdm, but it can be fixed by running `pdm run jupyter-lab --core-mode`

streamlit's webserver not working when using pdm for installation
### Summary

When I'm using [pdm](https://pdm.fming.dev/)  to install streamlit, I cannot connect to streamlit's webserver.
From pdm's web site: PDM is a modern Python package manager with PEP 582 support.

### Steps to reproduce

1. Install PDM: `pipx install pdm`
2. Create new directory and go there: `mkdir st_test; cd st_test`
3. Init pdm: `pdm init` (and use the defaults)
4. Install streamlit: `pdm add streamlit`
5. Use any minimal streamlit example and run streamlit: `pdm run streamlit run hello_world.py`

**Expected behavior:**

Browser opens correct web page served by streamlit.

**Actual behavior:**

Browser tries to connect to `localhost:3000` (which is also mentioned in the logs), but when I look at the logs, the server actually runs on port 8501. When I try this port, I get a 404, so I _can_ connect to the server on this port, but something's broken.

### Is this a regression?

That is, did this use to work the way you expected in the past?
never tried before

### Debug info

- Streamlit version: 0.80.0
- Python version: 3.8.8
- Using pdm
- OS version: Linux 5.11.11
- Browser version: Chrome 89.0.4389.114 (Official Build) (64-bit)

### Additional information

jupyter-lab show a similar issue with pdm, but it can be fixed by running `pdm run jupyter-lab --core-mode`



Discussion:
Thanks @pohlt for the report! I can repro the issue, but I cannot detect why this must be the case. I have never used pdm. We set the port here which is based on config options. https://github.com/streamlit/streamlit/blob/267bf98d9a8f8d94cf6d3e9f0ea8866233d4836e/lib/streamlit/report.py#L269

So I would imagine pdm is installing in some hidden config or something....Do you have a reference to that issue for `jupyter-lab`? I wonder if it could shed insight on why this might be the case.
Just tried the same setup with pdm and version 0.84.1 of streamlit with the same result.

What's special about how pdm installs packages is the mechanism described in [PEP 582](https://www.python.org/dev/peps/pep-0582/). The streamlit files and configurations might not end up where you would normally expect them, I guess.

I'm sorry that I cannot be of more help here.
Hey @pohlt Thank you for following up (and sorry for the delay. I will keep this open for tracking, but we have not prioritized it at the moment. Would love to hear how prevalent PDM is in development workflows and how much this impacts our users.
Closing, as pdm does not come up frequently enough for us to officially support it
I found a way that works: https://github.com/pdm-project/pdm/issues/989#issuecomment-1078293607. Hopefully it's not only for me, but for everyone.

Actually PDM is getting more and more popular recently. It would be great if it could be supported officially.
It looks like @AnOctopus is an enthusiast and is working towards this:

https://github.com/pdm-project/pdm/issues/972

I'll re-open, so everyone can be aware that its been requested.
> I found a way that works: [pdm-project/pdm#989 (comment)](https://github.com/pdm-project/pdm/issues/989#issuecomment-1078293607). Hopefully it's not only for me, but for everyone.
> 
> Actually PDM is getting more and more popular recently. It would be great if it could be supported officially.

That workaround gives me a very good idea about the source of the problem. We automatically set development mode by trying to detect how streamlit was installed, and I suspect being installed into `__pypackages__` breaks our heuristics for that. Should be relatively easy to fix.
@AnOctopus Thanks for the reply! Glad it helps. I realized the workaround is not ideal since with `--global.developmentMode false`, I'm not able to see live updates when I modify the code. I have to stop and relaunch the app to reflect the update. Hopefully in the future `pdm run streamlit run app.py` could simply work. 😆
Thanks @pohlt for the report! I can repro the issue, but I cannot detect why this must be the case. I have never used pdm. We set the port here which is based on config options. https://github.com/streamlit/streamlit/blob/267bf98d9a8f8d94cf6d3e9f0ea8866233d4836e/lib/streamlit/report.py#L269

So I would imagine pdm is installing in some hidden config or something....Do you have a reference to that issue for `jupyter-lab`? I wonder if it could shed insight on why this might be the case.
Just tried the same setup with pdm and version 0.84.1 of streamlit with the same result.

What's special about how pdm installs packages is the mechanism described in [PEP 582](https://www.python.org/dev/peps/pep-0582/). The streamlit files and configurations might not end up where you would normally expect them, I guess.

I'm sorry that I cannot be of more help here.
Hey @pohlt Thank you for following up (and sorry for the delay. I will keep this open for tracking, but we have not prioritized it at the moment. Would love to hear how prevalent PDM is in development workflows and how much this impacts our users.
Closing, as pdm does not come up frequently enough for us to officially support it
I found a way that works: https://github.com/pdm-project/pdm/issues/989#issuecomment-1078293607. Hopefully it's not only for me, but for everyone.

Actually PDM is getting more and more popular recently. It would be great if it could be supported officially.
It looks like @AnOctopus is an enthusiast and is working towards this:

https://github.com/pdm-project/pdm/issues/972

I'll re-open, so everyone can be aware that its been requested.
> I found a way that works: [pdm-project/pdm#989 (comment)](https://github.com/pdm-project/pdm/issues/989#issuecomment-1078293607). Hopefully it's not only for me, but for everyone.
> 
> Actually PDM is getting more and more popular recently. It would be great if it could be supported officially.

That workaround gives me a very good idea about the source of the problem. We automatically set development mode by trying to detect how streamlit was installed, and I suspect being installed into `__pypackages__` breaks our heuristics for that. Should be relatively easy to fix.
@AnOctopus Thanks for the reply! Glad it helps. I realized the workaround is not ideal since with `--global.developmentMode false`, I'm not able to see live updates when I modify the code. I have to stop and relaunch the app to reflect the update. Hopefully in the future `pdm run streamlit run app.py` could simply work. 😆

Resolving patch:
diff --git a/lib/streamlit/config.py b/lib/streamlit/config.py
--- a/lib/streamlit/config.py
+++ b/lib/streamlit/config.py
@@ -263,6 +263,7 @@ def _global_development_mode() -> bool:
         not env_util.is_pex()
         and "site-packages" not in __file__
         and "dist-packages" not in __file__
+        and "__pypackages__" not in __file__
     )
 
 


Test patch: