Can't install gcloud on Amazon Linux : invalid syntax - python

I'm trying to install gcloud on my EC2 server running Amazon Linux 4.14.47-56.37 64bits, in interactive mode running the following command :
curl https://sdk.cloud.google.com | bash
The files download correctly, but the install then fails with the following Traceback :
File "/home/ec2-user/google-cloud-sdk/bin/bootstrapping/install.py", line 12, in <module>
import bootstrapping
File "/home/ec2-user/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 32, in <module>
import setup # pylint:disable=g-import-not-at-top
File "/home/ec2-user/google-cloud-sdk/bin/bootstrapping/setup.py", line 55, in <module>
from googlecloudsdk.core import properties
File "/home/ec2-user/google-cloud-sdk/lib/googlecloudsdk/core/properties.py", line 291
self.__sections = {section.name: section for section in sections}
^
SyntaxError: invalid syntax
Any idea why this for is causing issues?
I am running python 2.7 (2.7.14) as recommended by Google.

On top of python 2.7 installed on the "python" command, I also had python 2.6 installed on the "python2" command.
Uninstalling python 2.6 solved the issue, Google Cloud install went through without issue on the next try.

You locally update the install.sh file to use python2.7 instead of python2. This worked for me

In case someone meets this problem again, and if you do not want to uninstall python 2.6 or to modify install.sh, here is a procedure that worked for me on an ec2 instance :
open a new terminal and type :
curl https://sdk.cloud.google.com | bash
The install should not work as CLOUDSDK_PYTHON=python2 by default, which leads to Python 2.6. To correct this, enter this command :
export CLOUDSDK_PYTHON=python
this links CLOUDSDK_PYTHON to Python 3.6
then
bash google-cloud-sdk/install.sh
and finally
./google-cloud-sdk/bin/gcloud init
Once this is done, you can use any gcloud or gsutil command by doing this :
./google-cloud-sdk/bin/gcloud <your gcloud command>
./google-cloud-sdk/bin/gsutil <your gsutil command>
For example, in order to copy a folder from a gs bucket to your ec2 instance :
./google-cloud-sdk/bin/gsutil -m cp -r gs://<bucket_name>/path/to/folder /path/on/your/ec2/instance
the -m option allows fast transfer (I could transfer with an average speed of 81 MiB/s with this option)
Hope this helps !

Setup yum repo:
# Update YUM with Cloud SDK repo information:
sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM
# The indentation for the 2nd line of gpgkey is important.
Install yum downloader and download the rpm, then install with "--nodeps":
yum install yum-utils
yumdownloader google-cloud-sdk-231.0.0-1.el7.noarch
mv 23873bd6e8459ba6e70e96eb8f03f6ac03cd707ce3c80baa8264c714e030c915-google-cloud-sdk-231.0.0-1.el7.noarch.rpm /usr/local/src/google-cloud-sdk-231.0.0-1.el7.noarch
rpm -ivh --nodeps /usr/local/src/google-cloud-sdk-231.0.0-1.el7.noarch

This solved issue:
export CLOUDSDK_PYTHON=python

Related

How to get the location of installed Python package into the shell

I want my users to be able to reference a file in my python package (specifically a docker-compose.yml file) directly from the shell.
I couldnt find a way to get only the location from pip show (and grep-ing out "location" from its output feels ugly), so my current (somewhat verbose) solution is:
docker compose -f $(python3 -c "import locust_plugins; print(locust_plugins.__path__[0])")/timescale/docker-compose.yml up
Is there a better way?
Edit: I solved it by installing a wrapper command I call locust-compose as part of the package. Not perfect, but it gets the job done:
#!/bin/bash
module_location=$(python3 -c "import locust_plugins; print(locust_plugins.__path__[0])")
set -x
docker compose -f $module_location/timescale/docker-compose.yml "$#"
Most of the support you need for this is in the core setuptools suite.
First of all, you need to make sure the data file is included in your package. In a setup.cfg file you can write:
[options.package_data]
timescale = docker-compose.yml
Now if you pip install . or pip wheel, that will include the Compose file as part of the Python package.
Next, you can retrieve this in Python code using the ResourceManager API:
#!/usr/bin/env python3
# timescale/compose_path.py
import pkg_resources
if __name__ == '__main__':
print(pkg_resources.resource_filename('timescale', 'docker-compose.yml'))
And finally, you can take that script and make it a setuptools entry point script (as distinct from the similarly-named Docker concept), so that you can just run it as a single command.
[options.entry_points]
console_scripts=
timescale_compose_path = timescale:compose_path
Again, if you pip install . into a virtual environment, you should be able to run timescale_compose_path and get the path name out.
Having done all of those steps, you can finally run a simpler
docker-compose -f $(timescale_compose_path) up

unable to read .replit: unable to decode .replit: Near line 5 (last key parsed 'run'): Key 'run' has already been defined

so effectively I am tryinning to run a Django website that i can share the link. The issue is the RUN file with the .replit file. I need to pip install crispy forms then run the server. How can this be accomplished in the .replit file?
tried the following:
language = "python3"
run = "pip install django-crispy-forms"
run = "python manage.py runserver 0.0.0.0:3000"
You can't have multiple run lines. Poetry (built-in, no setup required) should automatically install the modules you need, but in case it doesn't try this instead:
language = "python3"
run = "pip install django-crispy-forms && python manage.py runserver 0.0.0.0:3000"

Run python program in jenkins job

I am trying to run below code in jenkins job, code is to delete files older than 30 days from a directory in ftp server.
I created freestyle project job in jenkins and in build section I have selected "Execute shell" and I have added below code.
#! /usr/bin/python
import time
import ftputil
host = ftputil.FTPHost('host', 'user', 'pass')
mypath = '/path/directory'
now = time.time()
host.chdir(mypath)
names = host.listdir(host.curdir)
for name in names:
      if host.path.isfile(name):
         host.remove(name)
host.close()
I am facing below error on build
Building remotely on docker-4 (maven linux docker) in workspace /var/lib/jenkins/workspace/Capacity/folder/Test_job
[Test_job] $ /usr/bin/python /tmp/jenkins8422988908580909797.sh
File "/tmp/jenkins8422988908580909797.sh", line 6
SyntaxError: Non-ASCII character '\xc2' in file /tmp/jenkins8422988908580909797.sh on line 6, but no encoding declared;
and I also tried with "Execute python script" build option I am facing similar error like below.
Building remotely on docker-4 (maven linux docker) in workspace /var/lib/jenkins/workspace/Capacity/folder/Test_job
[Test_job] $ python /tmp/jenkins5375363980435767190.py
File "/tmp/jenkins5375363980435767190.py", line 6
SyntaxError: Non-ASCII character '\xc2' in file /tmp/jenkins5375363980435767190.py on line 6, but no encoding declared;
I am new to jenkins job and python, can any one guide how can I resolve this issue.
2) If I select jenkins pipeline job how can I call this python code from jenkinsfile.
Try to install ftputil globally.
pip install ftputil
or
sudo pip install ftputil
The following shell script works with no error, once I installed ftputil globally with sudo.
#!/usr/bin/env python
import time
import datetime
import ftputil
There is nothing special about this python program. If you want to execute it from shell. Then create shell script file e.g. python_prog.sh then, change permissions chmod +x python_prog.sh python_prog.py
python_prog.sh
#!/bin/sh
python python_prog.py
Finally, run the script from terminal . python_prog.sh or ./python_prog.sh

git executable not found in python

I was trying to clone a git repo with access key, but when I am trying to run it, It throws an exception saying git executable not found.
But i have installed git and the in_it.py shows correct path "C:\Program Files\Git\bin" Also I have installed gitpython to use the library in python
here's my code...
import git
git.Git("D:/madhav/myrep/").clone("#github.com:myrepo/scripts")
========= and it throws the following exception =================
Traceback (most recent call last): File
"C:\Users\1096506\Desktop\gitclone.py", line 1, in <module>
from git import Repo File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git\__init__.py",
line 84, in <module>
refresh() File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git\__init__.py",
line 73, in refresh
if not Git.refresh(path=path): File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git\cmd.py",
line 293, in refresh
raise ImportError(err) ImportError: Bad git executable. The git executable must be specified in one of the following ways:
- be included in your $PATH
- be set via $GIT_PYTHON_GIT_EXECUTABLE
- explicitly set via git.refresh()
All git commands will error until this is rectified.
This initial warning can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
- quiet|q|silence|s|none|n|0: for no warning or exception
- warn|w|warning|1: for a printed warning
- error|e|raise|r|2: for a raised exception
Example:
export GIT_PYTHON_REFRESH=quiet
I had the same issue. What I did is:
I went to: System Properties -> Environment Variables
On System Variables Section I clicked Path, Edit and Move Up.
Environment Variables
Edit and Moved Up two places from the bottom
Error occurs because git is not in the path. So its not able to import git module.
Couple of ways to resolve it.
As suggested above adding the git binary path to environment variable path.
If git is not being used directly in the module and its only a dependent module import thats throwing this exception before importing git we could add
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
and import git after this line, this would suppress the error caused due to git import
Had the same problem got it to work thanks to Muthukumaran. Just make Muthukumaran answer more clear.
Follow these steps:
import os
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
import git
See if you have installed Git in the OS.
If not install git first this will solve your error.
Centos
sudo yum -y install git
Ubuntu/Debian
sudo apt-get install git
Mac Os
sudo brew install git
This solved my problem.
Make sure you're not in an inaccessible directory on *nix, such as when you've just been root and then done a su username
you may still be in root's home folder and that will trigger this error (assuming you have the correct environment variables set, and have sourced the .profile or .bashrc etc with source ~/.bashrc )
which git
/usr/bin/git
I was getting this error even after setting the environment:
in ~/.bashrc
# for bench
PATH=$PATH:/usr/bin/git
export PATH
GIT_PYTHON_GIT_EXECUTABLE=/usr/bin/git
export GIT_PYTHON_GIT_EXECUTABLE
cd
and it's working
$ bench --version
WARN: Command not being executed in bench directory
5.3.0
I came across similar problem recently and installing git followed by restarting Windows Powershell CommandLine solved the problem. May it helps.
For those who are using a Lambda layer with it. It worked adding as the comment above says just adding GIT_PYTHON_REFRESH=quiet as an environment variable.
Execute GIT_PYTHON_REFRESH=quiet in your terminal and then try to run the code.

Installing node.js on CentOS 5

I'm pretty new to CentOS (5) and also node.js, but I already got an older version of node.js to work on my virtual server.
Now I'm trying to install a newer version, and I know that CentOS needs Python 2.4 while node needs 2.6 or newer, so I installed Python 2.7 using altinstall.
But even if I set an alias for Python that points to version 2.7 before running ./configure, I still get this error:
/root/node/wscript: error: Traceback (most recent call last):
File "/root/node/tools/wafadmin/Utils.py", line 274, in load_module
exec(compile(code, file_path, 'exec'), module.__dict__)
File "/root/node/wscript", line 222
"-pre" if node_is_release == "0" else ""
^
SyntaxError: invalid syntax
That's the content of ./configure:
#! /bin/sh
# v8 doesn't like ccache
if [ ! -z "`echo $CC | grep ccache`" ]; then
echo "Error: V8 doesn't like cache. Please set your CC env var to 'gcc'"
echo " (ba)sh: export CC=gcc"
exit 1
fi
CUR_DIR=$PWD
#possible relative path
WORKINGDIR=`dirname $0`
cd "$WORKINGDIR"
#abs path
WORKINGDIR=`pwd`
cd "$CUR_DIR"
"${WORKINGDIR}/tools/waf-light" --jobs=1 configure $*
exit $?"
And at the top of wscript there is the following line: "#!/usr/bin/env python". I also tried replacing that with something else, though I think it should work when using a Python alias
Any ideas what I need to do to get this to work?
Thanks!
I have python 2.7.3 'altinstalled' on Centos 5.x, with the binary named "/usr/local/bin/python2.7"
I compile and install nodejs v0.8.16 using:
PYTHON=/usr/local/bin/python2.7
export PYTHON
python2.7 configure && make && make install
running configure with python2.7 overrides the default python handling
creating a PYTHON env var allows make install to find the correct version of python
(I still had to identify and install missing development modules one by one before the install would succeed)
I changed the PATH in bash_profile to include the path to the desired version of python as follows:
vi ~/.bash_profile
replace PATH=$PATH:$HOME/bin
with PATH=/usr/local/python272/bin:$PATH:$HOME/bin
source ~/.bash_profile
./configure
make
It picks up the correct python version. No need to change wscript
I'm on CentOS 5.6, python 2.7.2 (installed in /usr/local/python272) and using node.js 0.4.12
I ran into this same exact problem. I wound up editing the wscript file and changed that line (222) from this:
"-pre" if node_is_release == "0" else ""
...to this:
""

Categories