os x, python, homebrew -> something weird going on - python

hoping someone can help me out. I followed a couple of guides about setting up a new machine for python development. (one) (two)
I followed pretty much everything but I am really confused about the changes to my .bash_profile
When I comment out export PATH=/usr/local/bin:$PATH in .bash_profile and type which python into terminal, I see what I expect.
which python
/usr/local/bin/python
python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
What is happening in bash_profile, how come it doesn't matter if I comment everything out, save it, and run the commands?
More importantly, how do I get to my default install of python and make sure it is still there and unaffected? Am I correct in understanding that the above version is the python installed by Homebrew?

.bash_profile is read only when the Bash shell is loaded, so changing this file after the shell has been loaded doesn't affect it. To see the changes after modifying and saving the file, you need to restart the shell or run source ~/.bash_profile.
Modifying the PATH environment variable doesn't affect the Python itself, it only modifies where it's searched first to be executed, when you will run python .... E.g., if you have python installed in /a/python and /b/python, then:
if your PATH is set to /a/python:/b/python:$PATH and you run python ....py actually the /a/python ....py is executed;
if your PATH is set to /b/python:/a/python:$PATH and you run python ....py actually the /b/python ....py is executed.
If you didn't modified the PATH (e.g. but executing something like export PATH=/usr/local/bin:$PATH in the shell or adding it to .bash_profile), then executing which python should show the default python path.
In the case you described, it shows you the python, that you set it to use by modifying the PATH. To see the default one, comment the export... string, save the .bash_profile file and either run source ~/.bash_profile or restart the shell, and only then see the which python output.

Related

Adding external library to Pycharm when using Python in ArcGIS [duplicate]

This question already has answers here:
"python" not recognized as a command
(14 answers)
Closed 2 years ago.
So I have recently installed Python Version 2.7.5 and I have made a little loop thing with it but the problem is, when I go to cmd and type python testloop.py I get the error:
'python' is not recognized as an internal or external command
I have tried setting the path but no avail.
Here is my path:
C:\Program Files\Python27
As you can see, this is where my Python is installed. I don't know what else to do. Can someone help?
Try "py" instead of "python" from command line:
C:\Users\Cpsa>py
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
I have found the answer... click on the installer and check the box "Add python to environment variables" DO NOT uninstall the old one rather click on modify....Click on link for picture...
You need to add that folder to your Windows Path:
https://docs.python.org/2/using/windows.html Taken from this question.
If you want to see python version then you should use py -V instead of python -V
C:\Users\ghasan>py -V
Python 3.7.1
If you want to go to python's running environment then you should use py instead of python
C:\Users\ghasan>py
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Here you can run the python program as:
print('Hello Python')
Hello Python
Firstly, be sure where your python directory. It is normally in C:\Python27. If yours is different then change it from the below command.
If after you install it python still isn’t recognized, then in PowerShell enter this:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27",
"User")
Close PowerShell and then start it again to make sure Python now runs. If it doesn’t,
restart may be required.
Type py -v instead of python -v in command prompt
i solved this by running CMD in administration mode, so try this.
Open CMD with administrative access(Right click then run as administrator) then type the following command there:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
Replace My_python_lib with the folder name of your installed python like for me it was C:\python27.
Then to check if the path variable is set, type echo %PATH% you'll see your python part in the end. Hence now python is accessible.
From this tutorial
I have installed python 3.7.4. First, I tried python in my command prompt. It was saying that 'Python is not recognized command......'. Then I tried 'py' command and it works.
My sample command is:
py hacker.py
I have met same issue when I install Python, and it is resolved when I set a PATH in system, here are the steps.
Navigate to "Control Panel" -> "System"
Click "Advanced system settings" on the left
Click "Environment Variables"
Search and click "Path" variable
Click "Edit"
Add "C:\"to the environment variables field, if you are using Windows7, then separate it by a semicolon from the existing entry. If you are using Windows10, just simply click "New" to add.
Reopen the Command Prompt and try
enter image description here
From the Python docs, set the PATH like you did as above.
You should arrange for Python’s installation directory to be added to the PATH of every command window as it starts. If you installed Python fairly recently then the command dir C:\py* will probably tell you where it is installed; the usual location is something like C:\Python27. Otherwise you will be reduced to a search of your whole disk
Use Tools ‣ Find or hit the Search button and look for “python.exe”. Supposing you discover that Python is installed in the C:\Python27 directory (the default at the time of writing), you should make sure that entering the command
Then execute the Python command using the full path name to make sure that works.
Another helpful but simple solution might be restarting your computer after doing the download if Python is in the PATH variable. This has been a mistake I usually make when downloading Python onto a new machine.
If you uninstalled then re-installed, and running 'python' in CLI, make sure to open a new CMD after your installation for 'python' to be recognized. 'py' will probably be recognized with an old CLI because its not tied to any version.
Option 1 : Select on add environment var during installation
Option 2 : Go to C:\Users-> AppData (hidden file) -> Local\Programs\Python\Python38-32(depends on version installed)\Scripts
Copy path and add to env vars path.
For me this path worked : C:\Users\Username\AppData\Local\Programs\Python\Python38-32\Scripts
It was a bit more confusing with the Python instructions once SQL Server 2019 was installed with Python. The actual path I find is as follows:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
Scripts run with an Execute command:
Declare #script nvarchar(max)=N'print(11-2)' execute sp_execute_external_script #language = N'Python', #script = #script
There is additional documentation in reference to SQL 2019's version of Python. There is a statement that recommends PIP be used only from a download of sqlmutils-x.x.x.zip located on git (https://www.github.com/Microsoft/sqlmutils) But there is a caveat. Currently this only works for R and not for Python (Anaconda and consequently pip). Python over SQL works but pip is not yet available. (11/25/2019)
Would be great to get an update when this occurs.

How to set environment variables in virtualenv

If I have python script which activates the virtualenv like this:
#!/path/to/venv/bin/python
How can I set variables for this script without modifying this script?
I want this environment variable to be active for all scripts which use this virtualenv.
This means modifying this script is not a solution, since there are twenty scripts, and I don't want to modify twenty scripts.
Writing a shell wrapper-script around the python scripts would work, but I would like to avoid this.
In the past I thought a custom sitecustomize.py can be used for start-up code. But Ubuntu (AFAIK the only distribution which does this) comes with its own sitecustomize.py file, with the effect that my sitecustomize.py does not get called. See https://bugs.launchpad.net/ubuntu/+source/python2.5/+bug/197219
Here are some ways how I want to use the virtualenv:
Script which gets executed via unix cron.
virtualenv in a systemd service. See: How to enable a virtualenv in a systemd service unit?
via mod_wsgi (Apache): https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#daemon-mode-single-application
(I have thought about this again. I guess it setting the variables is not the job of python or virtualenv. I need a unified way to set environment variables. And in my case I would like to do this without using a shell wrapper).
while writing sitecustomize.py file and changing bin/python all are feasible solutions, I would suggest another method that does not involve directly change contents inside virutalenv, by simply install a .pth file:
./venv/lib/python2.7/site-packages/_set_envs.pth
with content:
import os; os.environ['FOO'] = 'bar'
test:
$ ./venv/bin/python -c "import os; print os.getenv('FOO')"
bar
the trick is, python will load every .pth file on startup, and if there is a line starts with import, this line will be get executed, allowing inject arbitrary code.
the advantage is, you could simply write a python package to install this .pth file with setuptools, install to the virtualenv you want to change.
From what I have tried, it seems if you create a sitecustomize.py file inside the virtual environment, it will take precedence over the global sitecustomize.pyinstalled in /usr/lib/python2.7 directory. Here is what I did:
Create a sitecustomize.py in the virtual environment
$ echo "import os; os.environ['FOO'] = 'BAR'" > ~/venvs/env_test/lib/python2.7/sitecustomize.py
Verify that it is getting imported and executed when running the Python binary from the virtual environment
$ ~/venvs/env_test/bin/python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sitecustomize
>>> sitecustomize.__file__
'/home/abhinav/venvs/env_test/lib/python2.7/sitecustomize.py'
>>> import os
>>> os.environ['FOO']
'BAR'
>>>
Just to verify that FOO is set even without explicitly importing sitecustomize:
$ ~/venvs/env_test/bin/python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['FOO']
'BAR'
>>>
After trying dotenv package as well as the .pth method, I discovered they didn't work for me. So, I just modified the venv/bin/activate script, and exported the variables there.
Here's the idea.
$ cat venv/bin/activate
deactivate () {
unset FOO
unset BAR
...
}
...
export FOO='xxx'
export BAR='xxx'
Bit hackish, but should work.
Rename python link in virtual environment bin to something like python.lnk
In bin folder create file python that looks like
#!/bin/sh
export TEST='It works!'
"$0.lnk" "$#"
Make it executable
chmod +x python
Then if you run a script like
#!/work/venv/bin/python
import os
print 'Virtualenv variable TEST="{}"'.format(os.environ['TEST'])
as follows:
./myscript.py
it prints out:
Virtualenv variable TEST="It works!"
How to do in Ubuntu
The problem with Ubuntu it seems that python package comes with PYTHONNOUSERSITE as cPython compilation flag. You can find it with site module and see that site.ENABLE_USER_SITE is set to False.
Specific problem with the question
tl;dr #!/path/to/venv/bin/python is not enough to execute your virtual environment.
Although the rest of the answers seems to be correct in some contexts, the problem with the question is that he forces the execution with #!/path/to/venv/bin/python but hi is not providing any contextual virtual environment in his question, so I suppose he is not activating his virtual environment previous to run his script.
That is useless because that command usually links to your system python executable[^1] and does anything more. Also, by default you will use your system environment variables if you don't activate the virtual environment with source /path/to/venv/bin/activate or configure apache or nginx to manage your venv.
Solution for environment vars in venv
Then, is possible to add a specific line in your venv/bin/activate script that adds a environment value in bash language:
export FOO=BAR
and then activate it with source command.
[^1]: Probably you will prefer a generic #!/usr/bin/env python3 (python2 is deprecated). The environment will take properly your exectuable.

conda command will prompt error: "Bad Interpreter: No such file or directory"

I'm using arch linux and I've installed Anaconda as per the instruction on the Anaconda site. When I'm attempting to run conda info --envs I get the following error:
bash: /home/lukasz/anaconda3/bin/conda:
/opt/anaconda1anaconda2anaconda3/bin/python: bad interpreter: No such
file or directory
I've tried looking for the directory /opt/anaconda1anaconda2anaconda3/bin/python: but it simply doesn't exist.
Furthermore, when I run python from the terminal it runs as normal with the following displayed at the top
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
for completeness my .bashrc file resembles:
#
# ~/.bashrc
#
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
alias ls='ls --color=auto'
PS1='[\u#\h \W]\$ '
# added by Anaconda3 4.0.0 installer
export PATH="/home/lukasz/anaconda3/bin:$PATH"
# python startup for up keys
export PYTHONSTARTUP=$HOME/.pythonstartup
I've tried following this and making the the appropriate changes but nothing, I've also attempted to do this but there really isn't a solution posted.
I would like to try to fix this without having to remove Anaconda and reinstalling it.
Something must have gone wrong during the installation, I suppose.
The bad interpreter means that a script is looking for an interpreter that doesn't exist - as you rightfully pointed out.
The problem is likely to be in the shebang #! statement of your conda script.
From Wikipedia: Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest
of the script's initial line as an interpreter directive; the
specified interpreter program is run instead, passing to it as an
argument the path that was initially used when attempting to run the
script.
If you run
cat ~/anaconda3/bin/conda
You will probably get the following:
#!/opt/anaconda1anaconda2anaconda3/bin/python
if __name__ == '__main__':
import sys
import conda.cli
sys.exit(conda.cli.main())
Changing the first line to point a correct interpreter, i.e., changing it to:
#!/home/lukasz/anaconda3/bin/python
Should make the conda command work.
If you are sure that you installed everything properly, then I'd suggest maybe reaching out for support from the anaconda community.
I encountered the same error while trying
conda
The error you should interpret as follows:
bash: "path_to_file_with_error": "path_to_file_it_points_to":
bad interpreter: No such file or directory
How to fix
Type in terminal
nano "path_to_file_with_error"
Change first line of the file to correct path of the python (in my case it was in miniconda/bin)
As the response above, this issue can be solved by changing the
#!/opt/anaconda1anaconda2anaconda3/bin/python
to
#!/opt/anaconda3/bin/python
However, as soon as you do the next installation, e.g. "conda install [...]" this will be changed again to anaconda1anaconda2anaconda3, for whatever reasons.
You might also realize some installation warnings and errors which are very likely to be related to this problem. If you want to get rid of this problem, you have to solve this warnings and errors. My strongest assumption is that there are missing administrator rights causing this problem, when you attempt to installs some conda packages the first time.
When you change the path to the interpreter conda will not be activated, so by following any of the previous answers you will end up in dead end.
you get the following
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
So to solve this renaming path issue you need to:
you can use any text editor:
nano ~/anaconda3/etc/profile.d/conda.sh
change the CONDA_EXE and CONDA_PYTHON_EXE paths to the correct path
example:
export
CONDA_EXE='/home/yourusername/anaconda3/bin/conda'
export _CE_M=''
export _CE_CONDA=''
export CONDA_PYTHON_EXE='/home/yourusername/anaconda3/bin/python'
then last step do:
source ~/anaconda3/etc/profile.d/conda.sh
To test your conda
Try:
conda activate
conda deactivate
to make this change permanent to all terminals
please add this line to ~/.bashrc
source ~/anaconda3/etc/profile.d/conda.sh
If somebody runs into this problem and none of the above solutions works, it could be that on some update process the conda executable (which is a python script) was replaced with an identical-looking script with one key difference, it contains windows line endings.
This results in that executing the script via bash e.g.:
<some_path>/conda/bin/conda
will result in the error, but executing directly via python works
<some_path>/conda/bin/python <some_path>/conda/bin/conda
Can be fixed by dos2unix
dos2unix <some_path>/conda/bin/conda
or just move the file away and move it back.
For centos/Rocky Linux 8 OS:
conda is a 32 bit application but centos OS is 64 bit,
you need to install package below:
yum install glibc.i686
reference:
https://forums.centos.org/viewtopic.php?t=14169

How to switch to python2 in a particular terminal window temporarily?

I have a big third-party python2.7 application, a bunch of python scripts, which is added into PATH and it requires python2.7 instead of python 3.5 which I have by default.
$ python
Python 3.5.1 (default, Mar 3 2016, 09:29:07)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python2
Python 2.7.11 (default, Mar 3 2016, 11:00:04)
[GCC 5.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
I run that application as $ some_app arg1 arg2. How can make it use python2 as a temporary solution, preferably only in that single terminal window?
I don't substitute a call of that application with "python". I call it like $ some_app arg1 arg2. I don't want to change the version of python permanently or modify the source code of the application.
My favorite way: use virtualenv or pyenv
The most used tool for keeping multiple Python environments is virtualenv. I like to use a sugary wrapper around it called virtualenvwrapper.
Usually I install it using:
sudo pip install virtualenvwrapper
Then I add a line like the following one to my shell profile:
source /usr/local/bin/virtualenvwrapper.sh
Now you can make a virtual environment for each version of Python. Gosh, you can make one for each Python project!
mkvirtualenv py3
mkvirtualenv py2 --python=python2
Then you can switch temporarily to python2 by typing:
workon py2
There is another tool called pyenv that lets you easily switch between multiple versions of Python. Quoting the project readme, "It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. This project was forked from rbenv and ruby-build, and modified for Python". Looks like it is getting popular.
Both are elegant, easy to use and a perfect fit for this use case (switch to a particular python version in a terminal window temporarily).
The Unix way: set $PATH
It is a best practice to write the shebang for Python scripts like:
#!/usr/bin/env python
If the 3rd-party app you are running follows this convention, you can change the PATH. For example, suppose the default python interpreter is not the one you want:
$ env python --version
Python 3.5.1
Make a local copy of the python you want to make the default temporarily (you just have to do this one time):
$ mkdir ~/local
$ mkdir ~/local/python2
$ ln -s `which python2` ~/local/python2/python
Then every time you want to make python2 the default, do:
$ export PATH=~/local/python2/:$PATH
Now the default is the version you want:
$ env python --version
Python 2.7.10
The positive karma way: if the 3rd-party app has a hardcoded path in the shebang
Perhaps the app author did not follow the best practices and hardcoded the Python path in the shebang. For example, suppose the first line of the program is:
#!/usr/bin/python
In this case, none of the previous tricks would work. Personally, I would change it to the canonical form and send a pull request to the author of the 3rd-party app.
The bossy way: change the default Python version system-wide
While this does not really answer your question - because it affects every window and avery other user in the system - I'm mentioning it for the sake of completeness.
For example, if you are using Ubuntu you can change the default python interpreter using:
$ update-alternatives --config python
Other Linux distributions probably have something like this.
Quick and dirty
The shebang just tells the shell to call the script using the given interpreter. You can override the shebang when calling the app as interpreter app:
$ python2 `which app_name`
Or:
$ python2 /path/to/app_name
You can also just edit the program and change the shebang:
#!/usr/bin/python2
Note that this would change permanently the Python interpreter for that program, it is not temporary nor restricted to a particular terminal. Worst, if the program is in the global PATH, all the other users of the system will be affected. If you want to restore the old behavior, you must undo your changes.
That said, sometimes you just don't care about doing it right, you just want to make it work.
You can make use of terminal alias. The alias will not be temporary but it will not affect your system, so you can have a short alias like:
alias pyt "/usr/bin/python2.7"
in your .bashrc file inside home directory
As the question is tagged with Linux, I assume your scripts starts with that line
#! /usr/bin/env python
This is great to allow the system to choose the current Python installation, but here your requirement is to specifically use version 2.7
So IMHO you just have to change the hashbang line to select the required version:
#! /path/to/python-2.7
If you are using Anaconda as your Python distribution, it has virtual enviroments built in. If not, use virtualenv and virtualenvwrapper as Paul recommended.
In addition I recommend autoenv. With that you can automatically change your venv depending on the folder you're in. There is one for bash and one for zsh.

setting Python path

I installed both Python 2.7 and 3.4 on my computer, but now Python seems to confuse about the paths. It always shows syntax error when I run the scripts that worked before. I uninstalled both of the versions by removing them in application, and reinstall 2.7, but the problem remains the same. What should I do now?
Now I type Python in terminal:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
I tried this http://hints.binaryage.com/how-to-install-python-3-2-3-on-mac-os-x/ before I uninstall both versions but it didn't work.
If you want to understand which path variables you have set, you can type:
echo $PATH
at the command line in terminal and it'll tell you the 'path' (where it's looking when you type a command like 'python'). Another test you can do uses the 'which' command to see which file it'll pick when you type a command e.g.
which python
You can specify what you want the PATH variable to look like by editing one of several files. These are always stored in your home directory (shortcut on command line is ~ E.G. ~/myfile.txt) but there are a few file names that might be being used depending on your setup. Commonly the file names are .profile or .bash_profile, but there are others. NB: the '.' at the start of the file name means they're hidden from normal view. You can view all files in your home directory using the command:
ls -al ~
On my Mac, the file used is .bash_profile
Anyway, that'll help you see where the problem lies. As the previous answer states, often when you install both, one is given a specific name e.g. python27 or python3 or whatever.
However, the best approach is probably to get to know and then use, VirtualEnv: https://virtualenv.pypa.io/en/latest/
This lets you create a new, virtual python environment for every project. You set them up, telling them which python version to use and which libraries/packages to include, and then there are no clashes between projects. There is a stack overflow for how to tell virtualenv which python to use here: Use different Python version with virtualenv
And that brings me back to where I was. Understanding where your python files are and what they are called (e.g. python, python27, python3 etc) is necessary for this process. So hopefully the stuff at the top of my answer will help you to discover your pythons and then you can make use of VirtualEnv.

Categories