different result when running command indirectly though script - python

i am using pylint under virtual environment.
when running pylint directly, i get the following output
$ pylint src/**/*.py
************* Module main
src/main.py:1:0: C0114: Missing module docstring (missing-module-docstring)
src/main.py:3:0: C0116: Missing function or method docstring (missing-function-docstring)
------------------------------------------------------------------
Your code has been rated at 5.00/10 (previous run: 5.00/10, +0.00)
when the same command is wrapped in a bash script, executing the bash script yields a different output
$ $SHELL --version | head -n 1
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
$ which $SHELL
/usr/local/bin/bash
$ cat lint.sh
#!/usr/local/bin/bash
pylint *.py src/**/*.py
$ ./lint.sh
************* Module src/**/*.py
src/**/*.py:1:0: F0001: No module named src/**/*.py (fatal)
--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
where the lint.sh and src directory are within the same (root) directory, the src/**/*.py within the lint.sh is correct.
here is more information about the execution environment
$ pylint --version
pylint 2.6.0
astroid 2.4.2
Python 3.8.5 (default, Aug 9 2020, 16:57:39)
[Clang 12.0.0 (clang-1200.0.26.2)]
why running the command directly produces different output than running the same command indirectly (within a bash script, my assumption is that it has nothing to do with pylint directly)?
how can it be fixed to be able to run the command within a bash script?

You are using ** in your script. This feature needs to be activated:
shopt -s globstar
pylint *.py src/**/*.py
Likely, it is enabled in your interactive shell, and that's why it works there. You can query its setting by doing a
shopt globstar

Related

How to get python via virtual environment recognized in bash file via shebang

I have a script file:
#!/usr/bin/env python3.9
print("python is working")
However when I try and run it:
(karl-env) karl#Karls-MBP scripts (karl/test) $ . test.sh
bash: test.sh: line 3: syntax error near unexpected token `"python is working"'
bash: test.sh: line 3: `print("python is working")'
Following info:
(karl-env) karl#Karls-MBP scripts (karl/test) $ type -a python
python is /Users/karl/.pyenv/shims/python
python is /Users/karl/.pyenv/shims/python
python is /usr/bin/python
I'm in a virtual environment but I fail to understand how to get my environments python recognized via the shebang #!/usr/bin/env python3.9. I do not use Python often hence my noobiness!
This has a little to do with Python and a lot to do with the shell.
You're doing . test.sh – . is an alias for source, which has your shell attempt to interpret the given script as shell commands you'd enter. You want ./test.sh to execute the script.
Your shebang line is explicitly looking for a python3.9 executable, and your environment might not be Python 3.9, so you fall back to something else. Do python (or python3) instead: #!/usr/bin/env python
For the sake of sanity, rename your script to .py; it's not a .shellscript.

trying to activate pyenv, error: Load pyenv automatically by appending # the following to ~/.zshrc: eval "$(pyenv init -)

i installed pyenv using homebrew, but when i try to activate pyenv with the command:
pyenv shell 3.8.3,
i get the following message:
pyenv: shell integration not enabled. Run `pyenv init' for instructions.
So i run:
pyenv init
and then it says, "
# Load pyenv automatically by appending # the following to ~/.zshrc: eval "$(pyenv init -)
I don't know how to do this, does any1 know how to do this? I am running a mac os High Sierra 10.13.6
Ultimately, I want to use Python 3.7.4, this is why I installed pyenv. If i type
pyenv global 3.7.4
into the command line I get
pyenv: version `3.7.4' not installed
but i did install python 3.7.4. If I type
python -V
into the command line, I get Python 3.8.3. How do I install python3.7.4 so pyenv will see it. Also it should be noted, i've been having problems b4 with bash wherein i have to type:
PATH=/Users/myHomepage/opt/anaconda3/bin:$PATH
to activate my virtual environment previously.
I saw a tutorial on how to Setup pyenv for Multiple Pythons" wherein he says we have to add settings to our bash profile to type the following three commands:
$ echo ‘export PYENV_ROOT=“$HOME/.pyenv”’ >> ~/.bash_profile
$ echo 'export PATH=“$PYEN_ROOT/bin:$PATH” >>.bash.profile
$ echo ‘eval “$(pyenv init -)”’ >>~/.bash_profile
but i am using zsh so i input:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
I only ran the two commands under zsh, I don't know if this is why I am getting an error message when I restarted zsh:
[oh-my-zsh] Insecure completion-dependent directories detected:
drwxrwxr-x 3 myHomepage admin 102 Oct 7 20:04 /usr/local/share/zsh
drwxrwxr-x 4 myHomepage admin 136 Oct 7 20:26 /usr/local/share/zsh/site-functions
[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.
[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh] compaudit | xargs chmod g-w,o-w
[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.
➜ ~ exec "$SHELL"
[oh-my-zsh] Insecure completion-dependent directories detected:
drwxrwxr-x 3 myHomepage admin 102 Oct 7 20:04 /usr/local/share/zsh
drwxrwxr-x 4 myHomepage admin 136 Oct 7 20:26 /usr/local/share/zsh/site-functions
[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.
[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh] compaudit | xargs chmod g-w,o-w
[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.
so I typed:
compaudit | xargs chmod g-w,o-w
but still got:
There are insecure directories:
so then I put this in the command line:
exec "$SHELL"
then I installed python 3.7.4 into my folder project, it seems to be working, but I'm not sure about the error message " There are insecure directories:"
In your example you cite 3 commands to run in bash profile config file. And you say that you are using zsh, but show only the 2 first commands. Do you run the 3 command to zsh config file? Like:
$ echo 'eval "$(pyenv init -)"' >> ~/.zshrc

GitStats doesn't recognise Gnuplot (Windows)

I tried to set up GitStats but I can't seem to get it working on Windows 10 with Git Bash
I've added an environment variable to "C:\gnuplot\bin" for Gnuplot
Gnuplot installed:
$ gnuplot --version
gnuplot 5.2 patchlevel 7
GitStats:
$ python gitstats ../ ../stats/
'C:\gnuplot\bin' is not recognized as an internal or external command,
operable program or batch file.
[0.04700] >> C:\gnuplot\bin --version
gnuplot not found
How can I get it working?
The gitstats scripts includes:
# By default, gnuplot is searched from path, but can be overridden with the
# environment variable "GNUPLOT"
gnuplot_cmd = 'gnuplot'
if 'GNUPLOT' in os.environ:
gnuplot_cmd = os.environ['GNUPLOT']
That means you need to set GNUPLOT to the gnuplot executable, not its parent folder (bin).
set GNUPLOT=C:\gnuplot\bin\gnuplot.exe

How can I get VSCode to correctly activate conda when running pytest?

When using Anaconda and VSCode on Windows 10, the VSCode debugger correctly activates the environment when I run/debug. But when I use the test module, it fails.
Here's an example setup:
I create an environment using the command: conda create -n sqlite_test python=3.7.3
My folder is setup like so:
./src/
sql.py
test_sql.py
I open a new VSCode window, and open the src folder.
The source code for sql.py is:
import sqlite3
import os
def do_sql():
db_path = os.path.join(os.environ['TEMP'], 'test.db')
conn = sqlite3.connect(db_path)
print("SQL code ran successfully")
return True
do_sql()
The source code for test_sql.py is:
import pytest
from sql import do_sql
def test_do_sql():
assert do_sql()
In VSCode, I select the python interpreter in my sqlite_test Conda env.
If I just run sql.py, I get the no errors and the print statement prints to the console.
If I run the tests from pytest using VSCode (VSCode installs pytest into the sqlite_test environment using Pip), then I get the following error:
________________________ ERROR collecting test_sql.py _________________________
ImportError while importing test module 'c:\Users\UserName\Documents\src\tmp\sqllite\test_sql.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_sql.py:2: in <module>
from sql import do_sql
sql.py:1: in <module>
import sqlite3
..\..\..\..\Anaconda3\envs\sqllite\lib\sqlite3\__init__.py:23: in <module>
from sqlite3.dbapi2 import *
..\..\..\..\Anaconda3\envs\sqllite\lib\sqlite3\dbapi2.py:27: in <module>
from _sqlite3 import *
E ImportError: DLL load failed: The specified module could not be found.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
If I run pytest from the command line (with my conda environment activated), I get a successful test passed:
>pytest
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\UserName\Documents\src\tmp\sqllite
collected 1 item
test_sql.py . [100%]
============================================== 1 passed in 0.04 seconds ===============================================
Turns out this is a known bug: https://github.com/microsoft/vscode-python/issues/4300
Follow the issue on GitHub: https://github.com/microsoft/vscode-python/issues/10668
My work around for VS Code using remote WSL2 was to bootstrap the test execution to ensure the VS Code instance was launched from a properly activated conda environment:
.vscode/test-wrapper.sh
#!/bin/bash
echo "PyTest Wrapper Starting Bootstrap"
current_env=`conda info | grep "active environment :" | awk '{print $4}'`
if [ "${current_env}" == "pyspark" ]; then
pytest "$#"
else
echo "PySpark is not the current environment. Launch vscode from an activated environment."
echo "Track this issue here: https://github.com/microsoft/vscode-python/issues/10668"
echo "Using a native terminal: "
echo " conda activate pyspark"
echo " code"
echo
fi
Relevant settings:
.vscode/settings.json
{
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestPath": ".vscode/test-wrapper.sh",
"python.terminal.activateEnvironment": true,
"python.pythonPath": "${env:HOME}/anaconda3/envs/pyspark/bin/python",
"python.linting.pylintPath": "/${env:HOME}/anaconda3/bin/pylint",
"python.condaPath": "${env:HOME}/anaconda3/condabin/conda"
}
For people not aware, after you set this up and reload the window make sure to select the correct interpreter, in my case its called pyspark: cntrl + shift + p Python: Select Interpreter ~/anaconda3/envs/pyspark/bin/python
If you suspect you do not have the right conda environment loaded you can use the test-wrapper.sh to debug the issue with conda env list
wrong env selected because code was not launched from a native terminal session using the desired environment:
conda env list
# conda environments:
#
base * /home/name/anaconda3
pyspark /home/name/anaconda3/envs/pyspark
Update: Not sure why this was downvoted, but I can no longer reproduce the bug so this solution may be moot.
For those of you on Linux, I found a possible solution:
In the project directory, create .vscode/conda-pytest.sh:
#!/usr/bin/env bash
. /path/to/miniconda3/etc/profile.d/conda.sh
conda activate my_env && pytest "$#"
and then, in the project's .vscode/settings.json:
{
"python.testing.pytestPath": "/path/to/project/.vscode/conda-pytest.sh"
}
Remember to set conda-pytest.sh as executable.
For Windows, I'm sure an equivalent batch/powershell script could be written in place of conda-pytest.sh.

Emacs Inferior Python shell shows the send message with each python-shell-send-region command

I am using the Python inferior shell on OS X (El Capitan) and every time I send a piece of code to the Python process (using C-c C-r which is bound to python-shell-send-region) I get a message like this in the Python shell:
>>> import codecs, os;
__pyfile = codecs.open('''/var/folders/6j/g9hcs1s17g1dgxr3p_m04rq80000gn/T/py5013kfY''', encoding='''utf-8''');
__code = __pyfile.read().encode('''utf-8''');
__pyfile.close();
os.remove('''/var/folders/6j/g9hcs1s17g1dgxr3p_m04rq80000gn/T/py5013kfY''');
exec(compile(__code, '''/Users/user/Desktop/sample.py''', 'exec'));
I would like to not have this message shown with each invocation of the command. The same version of Emacs on Linux (together with python.el file) does not have this problem. The template of the message is defined in python-shell-send-file function (in python.el file), which in itself is puzzling as I cannot see how this function is called when I invoke python-shell-send-region. For the first run I invoke the Python interpreter (version 2.7.10) with the following command
/usr/bin/python -i
but it also behaves the same with other python interpreters (installed via brew). I am using Emacs 24.5.1 (again installed via brew). The only python-mode specific configuration I use in .emacs are related to the tab width:
(add-hook 'python-mode-hook
(lambda ()
(setq indent-tabs-mode t)
(setq tab-width 2)
(setq python-indent 2)))
Any help or suggestion is appreciated.
Edit and a possible solution:
(I found this https://github.com/jorgenschaefer/elpy/issues/1307):
$ easy_install-3.6 gnureadline
$ cd /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
$ mv readline.cpython-36m-darwin.so readline.cpython-36m-darwin.so.bak
If you get an error while installing gnureadline, fatal error: 'stdio.h' file not found, try xcode-select --install.
$ source ./your/venv/activate
$ pip install pyreadline
Should work automatically with Python. I experienced a similar issue on Windows 10 earlier this year, installing pyreadline as a dev dependency resolved it.

Categories

Resources