I am new to shell scripting and what I basically want is to write a script that installs all the software needed for my project to work on a Windows machine without asking the user any additional input aside from running the script itself.
For instance, I want to check if python3 and conda are installed and if not install them without asking the user for additional input.
For conda I was able to figure out the next couple of lines:
if ! command -v conda &> /dev/null
then
echo "conda could not be found"
echo "installing conda..."
MINICONDA_INSTALLER_SCRIPT=Miniconda3-latest-Windows-x86_64.exe
curl --location -C - --output "$MINICONDA_INSTALLER_SCRIPT" "https://repo.continuum.io/miniconda/$MINICONDA_INSTALLER_SCRIPT"
start /wait "" ./$MINICONDA_INSTALLER_SCRIPT /InstallationType=JustMe /RegisterPython=0 /S /C=%UserProfile%\Miniconda3
fi
The code above does not work completely, but I am sure I will be able to figure it out. It is just proof of concept.
I want to have something similar for python3 as well. That is, the script should download and install python3 in silent mode with default settings. On Linux I would just use something amount the lines:
sudo apt-get install python3
but on Windows, I don't know how to do that. Is there a way to do that in the first place?
Related
I tried adding autocomplete using shtab but it doesn't work :(
The completion file (/etc/bash_completion.d/shtab) looks ok, but hitting TAB does nothing
Reproduce:
virtualenv -p python3.6 venv && . venv/bin/activate
pip install -U shtab
shtab --shell=bash shtab.main.get_main_parser --error-unimportable | sudo tee /etc/bash_completion.d/shtab
I changed the "$BASH_COMPLETION_COMPAT_DIR"/shtab into /etc/bash_completion.d/shtab because it is not relevant anymore (though I did also run the original command)
My environment:
Ubuntu 18.04
Python3.6
shtab==1.5.2
Bash 4.4.20
Tried also on:
zsh==5.4.2
Docker (docker run -it python /bin/bash)
Is there an issue with my environment?
It may be that you are trying to complete something where there is nothing to complete. For example, shtab <TAB> will do nothing since it has no subcommands. The same is true for cp <TAB>, grep <TAB>, etc.
Meanwhile, shtab -<TAB> (and cp -<TAB>, grep -<TAB>, etc.) will happily complete the supported option flags. This comment may help: shtab#45#901736610.
my anaconda (4.5.4) works fine as long as I just use it via a linux terminal (bash shell). However, running conda commands in a bash script does not work at all.
The script test.sh containes these lines:
#!/bin/bash
conda --version
conda activate env
Now, running bash test.sh results in the error
test.sh: line 2: conda: command not found
test.sh: line 3: conda: command not found
As recommended for anaconda version > 4.4 my .bashrc does not contain
export PATH="/opt/anaconda/bin:$PATH",
but
. /opt/anaconda/etc/profile.d/conda.sh
Thank you.
I solved the problem thanks to #darthbith 's comment.
Since conda is a bash function and bash functions can not be propagated to independent shells (e.g. opened by executing a bash script), one has to add the line
source /opt/anaconda/etc/profile.d/conda.sh
to the bash script before calling conda commands. Otherwise bash will not know about conda.
If #randomwalker's method doesn't work for you, which it won't any time your script is run in a more basic shell such as sh, then you have two options.
Add this to your script: eval $(conda shell.bash hook)
Call your script with: bash -i <scriptname> so that it runs in your interactive environment.
Let's say you try to access user name with "miky" # "server" address.First when you login to your user ; learn conda path with "which conda" then probably you will get a path such as "/home/miky/anaconda3/bin/conda"
then put your conda commands as follow (in my example i use conda to install a mysql plugin forexample.): shh miky#server -t "/home/miky/anaconda3/bin/conda install -y -c anaconda mysql-connector-python" thats all.
do sudo ln -s /home/<user>/miniconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh and try again. This should activate conda for all users permenantly
source
I just entered a class as an introduction to Python, and so I downloaded GitBash and Andaconda on my Windows laptop. I navigated to my python file on GitBash and tried to execute my python file, to no avail. I searched this website for answers on what to do and tried as many solutions as I could find, but I had no luck. I am quite a noob at this, so I think I am missing something obvious. Perhaps just one line of code that could save me?
...
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ ls
python_intro.py
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python python_intro.py
bash: python: command not found
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ PATH=$PATH:/c/Python27/
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python python_intro.py
bash: python: command not found
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ export PATH="$PATH:/c/Python27"
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python python_intro.py
bash: python: command not found
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python
bash: python: command not found
Sorry if this is such a rookie question, it just seems that everything I find on the internet is above my level to understand, whereas I am at this very basic level.
Meanwhile, I thought Anaconda came with the same py launcher as "official" Python, and offered the same option to install it to somewhere on your PATH.
Try first to check that in a regular CMD.
where python
That will check if python is in the PATH or not.
From there, you can start using python, in a CMD or a git bash session.
Here is the process you can follow to run the Python on your Git Gash.
Launch the program Git Bash in the usual way that you launch Windows programs or VScode. A shortcut for Git Bash was created during installation.
At the command prompt, paste this command export PATH="$PATH:/C/Users/ComputerName/AppData/Local/Programs/Python/Python37-32". That will tell Windows where to find Python. (This assumes that you installed it in export PATH="$PATH:/C/Users/ComputerName/AppData/Local/Programs/Python/Python37-32", as we told you to above.)
Check to make sure that this worked correctly by entering the command python --version. It should say Python 2.7.8 (or 2.7.something), as shown in the figure below.
Assuming that worked correctly, you will want to set up git bash so that it always knows where to find python. To do that, enter the following command:
echo 'export PATH="$PATH:/C/Users/ComputerName/AppData/Local/Programs/Python/Python37-32"' > .bashrc. That will save the command into a file called .bashrc. It will be executed every time git bash launches, so you won’t have to manually tell the shell where to find python again.
Check to make sure that worked by typing exit, relaunching git bash, and then typing python --version again.
Apply above process inside your project folder so that you can use it on VScode.
I have some limited experience with Python and Django in Windows, and now I am trying to understand how to deploy my code to an Ubuntu 16.04 LTS VPS. Having read various tutorials and a lot of answers on SE, I managed to proceed pretty far (well, for me), but now I am stuck.
Manually (via Putty) I can do the following:
# check that Python 3.5 is installed
python3 --version
# install pip
sudo -kS apt-get -y install python3-pip
# upgrade pip to newest version
pip3 install --upgrade pip
# check result
pip3 --version
# install venv
sudo -kS pip3 install virtualenv virtualenvwrapper
# create venv
virtualenv ~/Env/firstsite
# make sure venv is created
ls -l ~/Env/firstsite/bin/python # /home/droplet/Env/firstsite/bin/python3.5 -> python3
# switch on venv
source ~/Env/firstsite/bin/activate # (firstsite) droplet#hostname:~$
# check that python3 is taken from venv
which python3 # /home/droplet/Env/firstsite/bin/python3
So the virtual environment is properly created and switched on. I could proceed installing Django.
However when I am trying to do exactly the same in the automated regime, using Paramiko (I execute commands using paramiko.SSHClient().exec_command(cmd, input_string, get_pty=False), everything goes exactly the same way, until the last command:
exec_command('which python3')
returns /usr/bin/python3. So I assume source activate doesn't work via Paramiko's SSH.
Why?
How can I cope with it?
Can I check that the venv is enabled in some more direct (and reliable) way?
Taken from #Pablo Navarro's answer here :How to source virtualenv activate in a Bash script helped me with this same issue (activating environments in a paramiko ssh session).
In the exec_command give the path to the python executable within the environment eg:
stdin, stdout, stderr = ssh.exec_command(/path/to/env/bin/python script.py)
In my case (using miniconda and a env called pyODBC):
stdin, stdout, stderr = ssh.exec_command(~/miniconda2/envs/pyODBC/bin/python run_script.py)
running the command ~/miniconda2/envs/pyODBC/bin/python -m pip list printed the list of modules in this env to confirm
We can easily activate the virtualenv and execute commands on same.
Example:
import paramiko
hostname = 'host'
port = 22
username = 'root'
password = 'root'
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname, port, username, password)
command = 'source /root/Envs/env/bin/activate;python3 --version;qark;echo hello'
(stdin, stdout, stderr) = s.exec_command(command)
for line in stdout.readlines():
print(line)
for line in stderr.readlines():
print(line)
s.close()
If you are using anaconda and creating your virtual environments that way, I found a work around. Taken from [this github page][1] I use send the following command to my remote pc through paramiko
f'source ~/anaconda3/etc/profile.d/conda.sh && conda activate {my_env} && {command}'
I also wish you could just activate a venv and then all the following commands would be in the venv, but this work around is nice since the only thing I have to change is the venv name. Since everythnig is in one line, it executes perfectly and I don't need to reactivate anything. If you just have a wrapper function in python it makes it all very easy to use and read. Something like this:
def venv_wrapper(command, ssh, venv=None):
if venv:
conda_location = 'source ~/anaconda3/etc/profile.d/conda.sh'
activate_env = f'conda activate {venv}'
command = f'{conda_location} && {activate_env} && {command}'
ssh.exec_command(command, get_pty=True)
I just send all of my commands through this code (which is a little more developed/complicated in my own toolkit) whether or not im using a venv. Works pretty nicely so far
[1]: https://github.com/conda/conda/issues/7980
I would like to install Anaconda on a remote server.
The server is running Ubuntu 12.04.
I only have access to this server via SSH.
How can I install Anaconda via the command line?
Something along the lines of:
wget https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh
to get the installer for 64 bit linux followed by:
bash Anaconda3-2020.07-Linux-x86_64.sh
You can get the latest release from here
Please take a look at the Anaconda repo archive page and select an appropriate version that you'd like to install.
After that, just do:
# replace this `Anaconda3-version.num-Linux-x86_64.sh` with your choice
~$ wget -c https://repo.continuum.io/archive/Anaconda3-vers.num-Linux-x86_64.sh
~$ bash Anaconda3-version.num-Linux-x86_64.sh
Concrete Example:
As of this writing, Anaconda3-2020.07 is the latest version. So,
~$ wget -c https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh
~$ bash Anaconda3-2020.07-Linux-x86_64.sh
P.S. Based on comments, this should also work in CentOS systems.
You can do as Prashant said or you can use bash scripts to automate the installation. Just simply copy and paste depending on the version of Python you want
If you are trying to it entirely in command line you use a bash script
python 2 anaconda install bash script:
# Go to home directory
cd ~
# You can change what anaconda version you want at
# https://repo.continuum.io/archive/
wget https://repo.continuum.io/archive/Anaconda2-4.2.0-Linux-x86_64.sh
bash Anaconda2-4.2.0-Linux-x86_64.sh -b -p ~/anaconda
rm Anaconda2-4.2.0-Linux-x86_64.sh
echo 'export PATH="~/anaconda/bin:$PATH"' >> ~/.bashrc
# Refresh basically
source .bashrc
conda update conda
python 3 anaconda install bash script
# Go to home directory
cd ~
# You can change what anaconda version you want at
# https://repo.continuum.io/archive/
wget https://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.sh
bash Anaconda3-4.2.0-Linux-x86_64.sh -b -p ~/anaconda
rm Anaconda3-4.2.0-Linux-x86_64.sh
echo 'export PATH="~/anaconda/bin:$PATH"' >> ~/.bashrc
# Refresh basically
source .bashrc
conda update conda
Source: https://medium.com/#GalarnykMichael/install-python-on-ubuntu-anaconda-65623042cb5a
Download Anaconda for linux, place in your ubuntu system through WinScp, then
$ sudo bash Anaconda2-4.3.0-Linux-x86_64.sh
after this logout of your ssh session and then login, you will get base environment.
1 - Go to Anaconda Repository, find the installation for your OS and copy the address
2 - wget {paste}. Ex: https://repo.continuum.io/archive/Anaconda3-5.2.0-Linux-x86_64.sh
3 - Execute with: bash. Ex: bash Anaconda3-5.2.0-Linux-x86_64.sh
Run!
$ sudo bash Anaconda2-4.3.0-Linux-x86_64.sh
Video tutorial::
https://youtu.be/JP60kTsVJ8E
Just download the anaconda installer and execute it as it is a shell script. Follow the steps :
In the terminal type "wget https://repo.continuum.io/archive/Anaconda-2.3.0-Linux-x86_64.sh"
The file will be downloaded in current directory. Now execute the downloaded file by "bash ./Anaconda-2.3.0-Linux-x86_64.sh"
Restart the terminal. This is very important for python version provided by anaconda to be set to default for that user.
Note- Try using environment for using different version of python. Changing the default python version for root might result in non functioning of some functionalities like yum.