2

I am working with PyCharm IDE and have encountered an issue where I need to run my Python script within both the PyCharm internal debugger and under proxychains. The reason for using proxychains is that the modules I'm working with do not support working via a SOCKS5 proxy, which is necessary for internet access.

Running the script directly from the internal debugger does not provide an option to use proxychains, and using the built-in console does not allow me to step through the code with the debugger.

Is there a way to configure PyCharm to run my script with both the internal debugger and proxychains? I want to be able to debug my code while also routing network requests through proxychains. Any help or suggestions on how to achieve this would be greatly appreciated.

IDE version: PyCharm 2021.3 (Community Edition) under Linux

1 Answer 1

3
+50

This can be accomplished by creating a shell script which would run your script through ProxyChains and then using this script as an interpreter in PyCharm.

First, create a shell script named python in your project directory (let's say it's located at ~/MyProject):

#!/bin/sh
proxychains4 /usr/bin/python3 "$@"

$@ is used to pass all the arguments (specifically, path to your main script) through this script to the actual Python interpreter (/usr/bin/python3).

Make this script executable:

chmod +x ~/MyProject/python

Now we can add this script as an interpreter in PyCharm.

If you're not using a virtual environment for this project, you can just add a new system interpreter and pick the created python shell script (~/MyProject/python) as the executable. Then add a new configuration using this new interpreter and path to your main script.

If you need a virtual environment for this, create a new virtual environment, specify its location and path to your default python interpreter (say, /usr/bin/python3), install all the necessary dependencies inside it and then once again go to Preferences -> Python Interpreter -> Show all, select your newly created virtual environment and change the "interpreter path" field to the shell script path (~/MyProject/python).

And one more step. Since PyCharm's debugger uses a built-in local server, you need to exclude connection to it in proxychains.conf:

localnet 127.0.0.1/255.255.255.255

Voilà!

[proxychains] config file found: /usr/local/etc/proxychains.conf
[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.17/lib/libproxychains4.dylib
[proxychains] DLL init: proxychains-ng 4.17
[proxychains] DLL init: proxychains-ng 4.17
Connected to pydev debugger (build 233.13763.11)
[proxychains] Strict chain  ...  171.244.140.160:3991  ... api.ipify.org:443  ...  OK
{'ip': '171.244.140.160'}

enter image description here

2
  • Brilliant! Debugger throws a lot of warnings, but it works! pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute) pydev debugger: The debugger may still function, but it will work slower and may miss breakpoints pydev debugger: Unable to find real location for: <frozen codecs>
    – cridnirk
    Commented Jan 30, 2024 at 19:17
  • 1
    A little fix: For a virtual environment, the script should contain the path to the appropriate virtual environment interpreter, not the system-wide /usr/bin/python3. Also: I emphasize that in order to get PyCharm to accept the new path for the interpreter, the script must be called exactly python or python3, and not like proxy_python.sh (as I tried at first) or any other name.
    – cridnirk
    Commented Jan 30, 2024 at 19:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.