I have two versions of python installed on my Linux box 2.6 and 2.7, as indicated in the link I have installed python 2.7 in /usr/local/bin/ and have created alias and updated PATH variable. The 2.6 is installed in /usr/bin/.
After this when I check the python version it displays 2.7.3 on terminal but on the same terminal when I run a bash script (that need to detect the python version) it displays as 2.6.
How should I enforce the bash script to refer to alias or /usr/local/bin for picking the right python version.
You have an alias in your profile. However, aliases do not carried along when running a script.
So what you need to do is to either use the full path everywhere in the script or to indicate the path in the very beginning.
#!/bin/bash
PYTHON_PATH=/usr/local/bin
MY_PYTHON=$PYTHON_PATH/python2.7.3
And then call it like:
$MY_PYTHON ... things
You need to update your PATH. Then you can use, like this:
export PATH="/usr/local/bin:$PATH"
Note I am including usr/local/bin before the other path
Include path before it. Include this in ~/.bashrc or ~/.bash_profile so that you don't need to do this every time you open a new shell.
Related
When using the script addon for Atom it brings up:
Unable to run
/usr/bin/python3
Did you start Atom from the command line?
atom .
Is it in your PATH?
PATH: /usr/bin/python3:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/bin/python3:/usr/local/share/dotnet:/Library/Frameworks/Mono.framework/Versions/Current/Commands:~/.dotnet/tools
How do I get Atom to recognize Python 3?
I tried installing Python using Homebrew and it is installed to /usr/local/bin/python3 as well as using the Terminal to add all of the suggested path locations to /etc/paths.
You need to properly configure the script package to use a profile that points to the correct python3.
Open a Terminal and navigate to the directory containing your scripts.
$ pwd
/Users/cerberus/Scripts
Get the path to python3
If you installed it via Homebrew, then it should be at:
$ python3 -V
Python 3.7.3
$ which python3
/usr/local/bin/python3
You can also check that Homebrew already updated PATH to add /usr/local/bin, but unless you did something wrong with the Python installation, this part is unnecessary.
$ echo $PATH
.../usr/local/bin/:/...
Now, start Atom from the command line as explained in the package docs
Make sure to launch Atom from the console/terminal. This gives atom
all your useful environment variables. Additionally, make sure to run
it with the project path you need.
$ cd /path/to/scripts
$ atom .
OR
$ atom /path/to/scripts
Go to Packages > Command Palette > Toggle (or use CMD+SHIFT+P)
Select Script: Run Options
Input the path to your scripts and the path to the python3 command
NOTE: On my machine, just setting python3 also works. But if you are having problems with your python path, you can try to specify the full path (/usr/local/bin/python3) as shown.
Save the profile (ex. as "Python3")
Now, when you want to run your Python scripts, use the Script: Run with Profile command and then select the profile you just created.
That should work now.
I am using ubuntu 18.04. I have Python-3.6.6 installed as a default version. But for practice I have installed Python-3.6.5 from source code to a directory where I wanted to install it and changed the .bashrc file accordingly. But it is showing me the default python3 path when I call "which" command.
How to use the python version which one I have installed recently?
If you only want to run some practice scripts you can select directly which python you want to use for your script. In the command line, e.g. type
PYTHONPATH=/foo/bar/python365 python somescript.py
where /foo/bar/python365 points to your python-3.6.5
You can also change your PYTHONPATH variable to look first in the python-3.6.5 folder before it looks to the default python (3.6.6) you had installed before. However, I do not recommend this, since it will make all python applications choose the python-3.6.5 as default. For extended practice with python-3.6.5 consider a virtual environment or an anaconda environment.
You should use virtualenv or something similar, like pipenv, I myself use virtualenv.
Here's a guide on how to do that: https://docs.python-guide.org/dev/virtualenvs/
virtualenv is a bit lower level and so not that intuitive to use, but since I used virtualenv before I found pipenv, I just stuck with it, while adding a few aliases to my .bashrc to make it easier.
I used two different python versions because discord.py would break on 3.7 (that doesn't seem to be the case anymore, but I still have it setup with 3.6 and 3.7), so here's what my .bashrc for two different versions looked like, this is on windows, but if you are on unix system or macOS (I want to make this answer as general as possible, it works the same on all operating systems really), just change the paths to lead to your interpreters (whichever you have) and don't use winpty:
alias python36="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe"
alias python37="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe"
alias pip36="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe -m pip"
alias pip37="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe -m pip"
alias venv36="python36 -m virtualenv"
alias venv37="python37 -m virtualenv"
alias python="winpty python"
Now, to use Python 3.6, I use venv36 venv in my project folder, and it setups Python 3.6 environment, using venv37 venv would create Python 3.7 environment.
I also wrote a function to make activating less of a hassle (again, this is in bashrc)
activate() {
if [ "$#" == 1 ]; then
source "$1/Scripts/activate"
else
echo "Usage: activate <env_name>"
fi
}
So, after I created my env once, I go to project folder, type activate venv and it starts up whatever version I wanted the project to have all by itself, to run my project I just use python myfile.py, when virtual environment is activated, it will use the interpreter that's in active virtual environment, regardless of what you have in your PATH variable. Also, using pip will automatically install to your virtual environment, not your actual installation, meaning global package list will be clean, or in best case, completely empty.
You should only install what you need and keep it separate environments, normally you use a single environment per project. Well, unless your project is client/server at once (for whatever reason) and server uses different libraries than client, for example, more of them, client environment could have less modules since that's what the server is for, to provide something without having to do it yourself, so they could be two projects in one, hence two different environments, separating dependencies based on what the "unit" in the project really needs.
Note: you have to install virtualenv (or pipenv or whatever you want to use really) into all of your python installations, if you want to create virtual environments from them, that's what pip36 and pip37 here is for - for installing packages directly into actual python installations, for now, I only had to do pip36 install virtualenv and pip37 install virtualenv.
I recently downloaded version 3.4 and installed it but when I go to the command line and type python -V I get Python 2.7.6 How do I set python 3.4 as my default version.
The first step to do is to find where is your python.
You can do it with which or where command (which for unix where for windows). Once you have this information you will know what is actually executed as "python" command. Then you need to change it for windows (i believe) you need to change the PATH variable in such a way that your python 3.4 will be found earlier then 2.6
For the unix you need to either do the same or link it in your package manager.
You need to use python3 to use python 3.4. For example, to know version of Python use:
python3 -V
This will use python 3.4 to interpret your program or you can use the shebang to make it executable. The first line of your program should be:
#!/usr/bin/env python3
If you want python3 to be used when you type python on the terminal, you can use an alias. To add a new alias, open your ~/.bash_aliases file using gedit ~/.bash_aliases and type the following:
alias python=python3
and then save and exit and type
source ~/.bash_aliases
and then you can type
python -V
to use python3 as your default python interpreter.
I am trying to make python3 executable scripts and run them from shell.I have python 3.4.0 installed on my system.
So, I added '/home/spandan/python_codes' directory to PYTHONPATH, as I am planning to keep my scripts and modules here.
However, while trying to execute these, the above error is thrown by the system, and the scripts wont execute unless I go into the python_codes directory and then execute them.
Executing python program : Here I found out that PYTHONPATH is irrelevant while making scripts, and also how to set the python shebang. So I set mine as #!/usr/bin/env python3.4.0
Is it correct?
You don't have to put your python codes in a global path. Just make your python 3.4 interpreter interpreter available globally. For that, edit .bash_profile or .bashrc file in your home directory and add the following line:
export PATH=${PATH}:/usr/bin/python3
That will make python3 executable irrespective of your current working directory.
In order to execute code from your codes directory, you just have to write:
$ python3 ./your_code.py
Another way is to add the shebang at the top of your code as
#/usr/bin/python3
and change the permission to executable by the current user (by default it will not have execute permission).
$ chmod 744 your_code.py
and then executing the script directly as
$ your_code.py
I hope I could address your problem.
Another way to do it is to use python-is-python2 or python-is-python3 debian packages with /usr/bin/env python sheabang.
This option will let the end user select which interpreter he want to use while maintaining your code versionless and avoiding some people to install unwanted interpreter versions.
As an example, run this commands to make a sample file:
cat << EOF > version.py && chmod +x version.py
#!/usr/bin/env python
import sys
print(sys.version)
EOF
And run the following command if you want to set python2 as default:
sudo apt install python-is-python2
Or run this command if you want to set python3 as default:
sudo apt install python-is-python3
Finally you could see the python interpreter version that you have been selected by running:
./version.py
The only drawback of this solution is that you could make your python scripts compatible across python interpreter versions, but thats your decision!
You can install which you want and change from python2 to python3 and vice- versa as you want, but the idea is to fix a python interpreter version in a system and not change it again unless definitive upgrade.
References:
https://askubuntu.com/questions/1296790/python-is-python3-package-in-ubuntu-20-04-what-is-it-and-what-does-it-actually
I have some scripts that I often use in both windows(cygwin) and linux, I'd like to make the scripts executable in both environments. Is there a way to alias the location of my python installation, for example so that
#!/usr/bin/python
will still find my python installation, even though, as far as cygwin is concerned, it is located at /cygdrive/c/Python26/python?
Just install Python via Cygwin's setup.exe, and it will be in /usr/bin.
Or create symbolic links on both systems, for example in /usr/local/bin/python, and use that in the shebang.
Or write an installation script that adjusts the #! line in your Python scripts as it installs them.
Note that if your /cygdrive/c/Python26/python (C:\Python26\python) is a native Windows Python installation, scripts using it won't be able to use Cygwin-style paths.
Put /cygdrive/c/Python26/python in your PATH variable, and replace the shebang line with
#!/usr/bin/env python
assuming that cygwin can run that version of python.