I'm getting an error while uploading my code to esp8266 via an arduino (FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error)))
So, you found the solution.
&quot;if I reset the ESP the code start uploading&quot;
This behaviour of ESP8266 is absolutely normal.
If you have an Arduino board with ATmega328 chip in it, and the board is connected to the PC's USB port, and you want to upload a new code, you just click &quot;upload&quot; in the IDE, and so the IDE makes a connection to the onboard USB chip (ATmega16U2 in Arduino UNO), and this chip puts the ATMega in programming mode (it resets Atmega, and when ATMega powers up its bootloader waits for an upcoming programming code from serial connection)
For ESP12, ESP8266 enter the programming mode when it is started this way:

GPIO15 - low
GPIO0 - low
GPIO2 - high

Those pins may differ for other implementations of ESP; Google can help you if you need. But if you is having success, of course you did put ESP in programming mode.
ESP doesn't behave as an Arduino. If you intend to program ESP8266, you had to manually start ESP in programming mode.
The correct order would be: 1) load the code in Arduino IDE; 2) restart ESP in programming mode; 3) Command the IDE to upload the compiled code to ESP. 4) Restart ESP out of programming mode.
You also will be sucessfull, as you said, if you restart ESP in programming mode while IDE is still trying to connect to ESP (before the IDE gave up and desist to contacting the chip).
After the new code is loaded in ESP's ROM, you needed to restart it again (in flash boot mode) in order to execute the software that you have just written to its ROM.