how to import the third packages,ModuleNotFoundError: No module named 'pymysql' error: exit status 255 #5812

Closed
opened 2026-02-21 18:12:42 -05:00 by yindo · 6 comments
Owner

Originally created by @tingaicompass on GitHub (Sep 26, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.8.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

1.First I went into the sanbox container and installed mysql
pip install pymysql

anyio              4.4.0
certifi            2024.8.30
charset-normalizer 3.3.2
exceptiongroup     1.2.2
h11                0.14.0
httpcore           1.0.5
httpx              0.27.2
idna               3.8
Jinja2             3.1.4
MarkupSafe         2.1.5
pip                23.0.1
PyMySQL            1.1.1
PySocks            1.7.1
requests           2.32.3
setuptools         65.5.1
sniffio            1.3.1
socksio            1.0.0
typing_extensions  4.12.2
urllib3            2.2.2
wheel              0.44.0

2.Entering python verifies that import pymysql is OK

3.But I went to the front-end page, used the code execution module, and reported an error:

No module named 'pymysql' error: exit status 255

✔️ Expected Behavior

How to solve please give me a detailed case

Actual Behavior

No module named 'pymysql' error: exit status 255

Originally created by @tingaicompass on GitHub (Sep 26, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.8.2 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce 1.First I went into the sanbox container and installed mysql pip install pymysql ``` anyio 4.4.0 certifi 2024.8.30 charset-normalizer 3.3.2 exceptiongroup 1.2.2 h11 0.14.0 httpcore 1.0.5 httpx 0.27.2 idna 3.8 Jinja2 3.1.4 MarkupSafe 2.1.5 pip 23.0.1 PyMySQL 1.1.1 PySocks 1.7.1 requests 2.32.3 setuptools 65.5.1 sniffio 1.3.1 socksio 1.0.0 typing_extensions 4.12.2 urllib3 2.2.2 wheel 0.44.0 ``` 2.Entering python verifies that import pymysql is OK 3.But I went to the front-end page, used the code execution module, and reported an error: No module named 'pymysql' error: exit status 255 ### ✔️ Expected Behavior How to solve please give me a detailed case ### ❌ Actual Behavior No module named 'pymysql' error: exit status 255
yindo added the 🐞 bug🤔 cant-reproduce labels 2026-02-21 18:12:42 -05:00
yindo closed this issue 2026-02-21 18:12:42 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 26, 2024):

To resolve the "No module named 'pymysql'" error (exit status 255) when using the code execution module on the front-end page in a self-hosted Dify version 0.8.2 environment, you need to ensure that the pymysql module is installed in the environment where the code execution is taking place.

You can install the pymysql module using pip. Here is the command to install it:

pip install pymysql

If you are using a virtual environment, make sure to activate it before running the above command.

Additionally, ensure that the sandbox service used for code execution has access to the pymysql module. If the sandbox service is running in a containerized environment, you may need to update the container image to include the pymysql module.

Here is an example of how you might modify a Dockerfile to include the pymysql module:

FROM python:3.9-slim

# Install pymysql
RUN pip install pymysql

# Copy your application code
COPY . /app

# Set the working directory
WORKDIR /app

# Run your application
CMD ["python", "your_application.py"]

After updating the Dockerfile, rebuild the Docker image and redeploy the container.

By ensuring that the pymysql module is installed in the appropriate environment, you should be able to resolve the "No module named 'pymysql'" error [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Sep 26, 2024): <!-- Answer --> To resolve the "No module named 'pymysql'" error (exit status 255) when using the code execution module on the front-end page in a self-hosted Dify version 0.8.2 environment, you need to ensure that the `pymysql` module is installed in the environment where the code execution is taking place. You can install the `pymysql` module using pip. Here is the command to install it: ```sh pip install pymysql ``` If you are using a virtual environment, make sure to activate it before running the above command. Additionally, ensure that the sandbox service used for code execution has access to the `pymysql` module. If the sandbox service is running in a containerized environment, you may need to update the container image to include the `pymysql` module. Here is an example of how you might modify a Dockerfile to include the `pymysql` module: ```Dockerfile FROM python:3.9-slim # Install pymysql RUN pip install pymysql # Copy your application code COPY . /app # Set the working directory WORKDIR /app # Run your application CMD ["python", "your_application.py"] ``` After updating the Dockerfile, rebuild the Docker image and redeploy the container. By ensuring that the `pymysql` module is installed in the appropriate environment, you should be able to resolve the "No module named 'pymysql'" error <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/helper/code_executor/code_executor.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@tingaicompass commented on GitHub (Sep 26, 2024):

Run failed: Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/pymysql/connections.py", line 649, in connect
sock = socket.create_connection(
File "/usr/local/lib/python3.10/socket.py", line 845, in create_connection
raise err
File "/usr/local/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
OSError: [Errno 101] Network is unreachable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/sandbox/sandbox-python/tmp/41da4196_adf7_4fa0_a725_50afabb2ed90.py", line 48, in <module>
File "<string>", line 7, in <module>
File "/usr/local/lib/python3.10/site-packages/pymysql/connections.py", line 361, in init
self.connect()
File "/usr/local/lib/python3.10/site-packages/pymysql/connections.py", line 716, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '10.80.2.195' ([Errno 101] Network is unreachable)")
error: exit status 255

I encountered a new error, remote mysql cannot be linked, but it can be successfully operated on my local computer

@tingaicompass commented on GitHub (Sep 26, 2024): ``` Run failed: Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/pymysql/connections.py", line 649, in connect sock = socket.create_connection( File "/usr/local/lib/python3.10/socket.py", line 845, in create_connection raise err File "/usr/local/lib/python3.10/socket.py", line 833, in create_connection sock.connect(sa) OSError: [Errno 101] Network is unreachable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/var/sandbox/sandbox-python/tmp/41da4196_adf7_4fa0_a725_50afabb2ed90.py", line 48, in <module> File "<string>", line 7, in <module> File "/usr/local/lib/python3.10/site-packages/pymysql/connections.py", line 361, in init self.connect() File "/usr/local/lib/python3.10/site-packages/pymysql/connections.py", line 716, in connect raise exc pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '10.80.2.195' ([Errno 101] Network is unreachable)") error: exit status 255 ``` I encountered a new error, remote mysql cannot be linked, but it can be successfully operated on my local computer
Author
Owner

@dosubot[bot] commented on GitHub (Sep 26, 2024):

The error OSError: [Errno 101] Network is unreachable indicates that the sandbox environment is unable to reach the MySQL server at 10.80.2.195. This is likely due to network restrictions in the sandbox environment.

In Dify, the sandbox service is configured to use a custom network (ssrf_proxy_network) that is isolated and cannot access external networks directly. To allow the sandbox to connect to external services like your MySQL server, you need to adjust the network configuration.

Here are the steps to modify the network configuration:

  1. Update the docker-compose.middleware.yaml file:
    Ensure that the sandbox service is connected to a network that has access to external networks. You can modify the networks section to include the default network or another network that has external access.

    services:
      sandbox:
        ...
        networks:
          - default
          - ssrf_proxy_network
    
      ssrf_proxy:
        ...
        networks:
          - ssrf_proxy_network
          - default
    
    networks:
      ssrf_proxy_network:
        driver: bridge
        internal: true
      default:
        driver: bridge
    
  2. Rebuild and redeploy the containers:
    After updating the docker-compose.middleware.yaml file, rebuild and redeploy the containers to apply the changes.

    docker-compose down
    docker-compose up -d --build
    
  3. Test the connection:
    Once the containers are up and running, test the connection to the MySQL server from within the sandbox container.

    docker exec -it <sandbox_container_id> /bin/sh
    

    Inside the container, use ping or curl to test connectivity to the MySQL server.

    ping 10.80.2.195
    

By ensuring that the sandbox service is connected to a network with external access, you should be able to resolve the "Network is unreachable" error and connect to your remote MySQL server [1][2].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Sep 26, 2024): <!-- Answer --> The error `OSError: [Errno 101] Network is unreachable` indicates that the sandbox environment is unable to reach the MySQL server at `10.80.2.195`. This is likely due to network restrictions in the sandbox environment. In Dify, the sandbox service is configured to use a custom network (`ssrf_proxy_network`) that is isolated and cannot access external networks directly. To allow the sandbox to connect to external services like your MySQL server, you need to adjust the network configuration. Here are the steps to modify the network configuration: 1. **Update the `docker-compose.middleware.yaml` file**: Ensure that the `sandbox` service is connected to a network that has access to external networks. You can modify the `networks` section to include the default network or another network that has external access. ```yaml services: sandbox: ... networks: - default - ssrf_proxy_network ssrf_proxy: ... networks: - ssrf_proxy_network - default networks: ssrf_proxy_network: driver: bridge internal: true default: driver: bridge ``` 2. **Rebuild and redeploy the containers**: After updating the `docker-compose.middleware.yaml` file, rebuild and redeploy the containers to apply the changes. ```sh docker-compose down docker-compose up -d --build ``` 3. **Test the connection**: Once the containers are up and running, test the connection to the MySQL server from within the sandbox container. ```sh docker exec -it <sandbox_container_id> /bin/sh ``` Inside the container, use `ping` or `curl` to test connectivity to the MySQL server. ```sh ping 10.80.2.195 ``` By ensuring that the sandbox service is connected to a network with external access, you should be able to resolve the "Network is unreachable" error and connect to your remote MySQL server <sup>[[1]](https://github.com/langgenius/dify/blob/main/docker-legacy/docker-compose.middleware.yaml)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/docker/docker-compose.middleware.yaml)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@crazywoola commented on GitHub (Sep 26, 2024):

Duplicated

@crazywoola commented on GitHub (Sep 26, 2024): Duplicated
Author
Owner

@Victoriaheiheihei commented on GitHub (Oct 30, 2024):

can it work with the method mentioned before?

@Victoriaheiheihei commented on GitHub (Oct 30, 2024): can it work with the method mentioned before?
Author
Owner

@Modas-Li commented on GitHub (Nov 13, 2024):

Duplicated

duplicated with which issue, please give us a link, thx

@Modas-Li commented on GitHub (Nov 13, 2024): > Duplicated duplicated with which issue, please give us a link, thx
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5812