beelit94 / python-terraform

MIT License
472 stars 172 forks source link

Getting no such file or directory issue #83

Open taragurung opened 4 years ago

taragurung commented 4 years ago

I am just trying to run simple terraform script with the package and getting the file not found issue even though the file exists.

I am running a python container and testing it inside it. Tried with both python3 and python2 but same problem.

Here is my python script

Screen Shot 2020-04-16 at 5 52 09 PM

Here is my simple terraform script:

Screen Shot 2020-04-16 at 5 51 41 PM

And this is the issue it is throwing: Tried passing both absolute and relative path. Doubled confirmed with file and directory

Screen Shot 2020-04-16 at 5 52 40 PM
Spikeophant commented 4 years ago

I believe running init first will take care of this. I always init before apply in any scripts I use this in. Mostly I run this in ci/cd pipelines.

mike-19 commented 4 years ago

@Spikeophant it fails during init as well, running this inside a container. However the same works fine if I run it outside a container

# python
Python 3.7.6 (default, Feb 26 2020, 15:44:31)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_terraform import *
>>> tf = Terraform()
>>> tf.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/python_terraform/__init__.py", line 180, in init
    return self.cmd('init', *args, **options)
  File "/usr/local/lib/python3.7/site-packages/python_terraform/__init__.py", line 293, in cmd
    cwd=working_folder, env=environ_vars)
  File "/usr/local/lib/python3.7/subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.7/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'terraform': 'terraform'
>>>
Spikeophant commented 4 years ago

It doesn't appear that terraform is in the path of the container. In your python can you echo $PATH and see what it outputs, then run the container in interactive mode and echo $PATH and see if they match? I misunderstood your issue originally, thinking it couldn't find the .tf files, but it cannot find the terraform executable itself.

On Thu, Aug 27, 2020 at 12:22 PM mike-19 notifications@github.com wrote:

@Spikeophant https://github.com/Spikeophant it fails during init as well, running this inside a container. However the same works fine if I run it outside a container

python

Python 3.7.6 (default, Feb 26 2020, 15:44:31) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

from python_terraform import tf = Terraform() tf.init() Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.7/site-packages/python_terraform/init.py", line 180, in init return self.cmd('init', args, **options) File "/usr/local/lib/python3.7/site-packages/python_terraform/init.py", line 293, in cmd cwd=working_folder, env=environ_vars) File "/usr/local/lib/python3.7/subprocess.py", line 800, in init restore_signals, start_new_session) File "/usr/local/lib/python3.7/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'terraform': 'terraform'

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/beelit94/python-terraform/issues/83#issuecomment-682143945, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADR432WSEY3MD2DIQSXWJSLSC2W6VANCNFSM4MJVISYA .

alexcargf commented 3 years ago

Hi I am struggling with this as well.in my case i cannot find the working directory,please help! Thanks

amitanand-ms commented 3 years ago

I am also seeing same error

mike-19 commented 3 years ago

What's the content in your dockerfile ?

On Fri, May 7, 2021, 8:39 AM amitanand-ms @.***> wrote:

I am also seeing same error

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/beelit94/python-terraform/issues/83#issuecomment-834029703, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKXNST3JHYEYW35XE6VH6DTMNKYLANCNFSM4MJVISYA .

amitanand-ms commented 3 years ago

I am not using docker, just trying to use terraform through Python on a VM through a simple python code.

from python_terraform import * tf = Terraform(working_dir='/root/terraform',variables={'azure_pass':passw, 'resource_group_name':"testrgpython" ,'resource_location':"eastus"}) approve = {"auto-approve": True} tf.plan()

And this gives the error

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.6/site-packages/python_terraform/init.py", line 154, in plan return self.cmd('plan', *args, **options) File "/usr/local/lib/python3.6/site-packages/python_terraform/init.py", line 293, in cmd cwd=working_folder, env=environ_vars) File "/usr/lib64/python3.6/subprocess.py", line 729, in init restore_signals, start_new_session) File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'terraform': 'terraform'

.tf does exist in working directory

@.*** ~]# ls /root/terraform terraform.tf

On Fri, May 7, 2021 at 7:50 AM mike-19 @.***> wrote:

What's the content in your dockerfile ?

On Fri, May 7, 2021, 8:39 AM amitanand-ms @.***> wrote:

I am also seeing same error

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/beelit94/python-terraform/issues/83#issuecomment-834029703 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/ABKXNST3JHYEYW35XE6VH6DTMNKYLANCNFSM4MJVISYA

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/beelit94/python-terraform/issues/83#issuecomment-834303535, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALEPG7MH7EBZ4Y2LRVEJACTTMPHZ5ANCNFSM4MJVISYA .

-- Thanks Amit Anand

mike-19 commented 3 years ago

Can you paste the output of the command

Which terraform

On Fri, May 7, 2021, 7:25 PM amitanand-ms @.***> wrote:

I am not using docker, just trying to use terraform through Python on a VM through a simple python code.

from python_terraform import * tf = Terraform(working_dir='/root/terraform',variables={'azure_pass':passw, 'resource_group_name':"testrgpython" ,'resource_location':"eastus"}) approve = {"auto-approve": True} tf.plan()

And this gives the error

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.6/site-packages/python_terraform/init.py", line 154, in plan return self.cmd('plan', *args, **options) File "/usr/local/lib/python3.6/site-packages/python_terraform/init.py", line 293, in cmd cwd=working_folder, env=environ_vars) File "/usr/lib64/python3.6/subprocess.py", line 729, in init restore_signals, start_new_session) File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'terraform': 'terraform'

.tf does exist in working directory

@.*** ~]# ls /root/terraform terraform.tf

On Fri, May 7, 2021 at 7:50 AM mike-19 @.***> wrote:

What's the content in your dockerfile ?

On Fri, May 7, 2021, 8:39 AM amitanand-ms @.***> wrote:

I am also seeing same error

— You are receiving this because you commented. Reply to this email directly, view it on GitHub <

https://github.com/beelit94/python-terraform/issues/83#issuecomment-834029703

, or unsubscribe <

https://github.com/notifications/unsubscribe-auth/ABKXNST3JHYEYW35XE6VH6DTMNKYLANCNFSM4MJVISYA

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/beelit94/python-terraform/issues/83#issuecomment-834303535 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/ALEPG7MH7EBZ4Y2LRVEJACTTMPHZ5ANCNFSM4MJVISYA

.

-- Thanks Amit Anand

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/beelit94/python-terraform/issues/83#issuecomment-834418604, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKXNST6I44ROCYF6HP63ZDTMPWMVANCNFSM4MJVISYA .

garciassolutions commented 2 years ago

Ran into the same issue, terraform needed to be installed on the VM. Hope it helps.