In James 2:1-11, he was speaking to people who has already sinned by showing favoritism, so he was not telling them that they needed to have perfect obedience because that would have already been too late, and he was not trying to discourage them from keeping the law, but rather he was encouraging them to repent and to more consistent in their obedience to it. If we needed to have perfect obedience, then repentance would not change that we have already fallen short of perfect obedience, so repentance would have no value, but the fact that we can repent from the fact that we have already sinned means that we do not need to have perfect obedience.
Our salvation is from sin (Matthew 1:21) and sin is the transgression of God's law (1 John 3:4), so while we our salvation is not the result of our obedience to it, living in obedience to it through faith in Jesus is nevertheless intrinsically part of the concept of him saving us from not living in obedience to it. Jesus is God's word made flesh, so obeying God's word is the way to have faith in him, which is why there are many verses that say that the way to have the gift of eternal life is by believing in him/by obeying God's commandments, and why those who do not practice righteousness in obedience to His law are not born again as children of God (1 John 3:4-10).