How do I treat the income from an ESPP I sold now that I am a non-resident alien?
That's a tricky question and you should consult a tax professional that specializes on taxation of non-resident aliens and foreign expats. You should also consider the provisions of the tax treaty, if your country has one with the US. I would suggest you not to seek a "free advice" on internet forums, as the costs of making a mistake may be hefty. Generally, sales of stocks is not considered trade or business effectively connected to the US if that's your only activity. However, being this ESPP stock may make it connected to providing personal services, which makes it effectively connected. I'm assuming that since you're filing 1040NR, taxes were withheld by the broker, which means the broker considered this effectively connected income.