python3 finds some programs in a folder, but not others - python

I have the following directory:
~/Library/Python/3.9/bin
Inside that folder, for example, appear these two programs:
Executing:
python3 -m submit50
gives...
usage: submit50 [-h] [--logout] [--log-level {debug,info,warning,error}] [-V]
slug
...
However, executing:
python3 -m youtube-dl
gives...
/Library/Developer/CommandLineTools/usr/bin/python3: No module named youtube-dl
What's happening here ?
Why isn't the program youtube-dl being located, and
Why is the path /Library/Developer/CommandLineTools/usr/bin/ appearing there ?
OS: macOS Ventura 13.1

Related

different result when running command indirectly though script

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

Can't open file 'demo.py': [Errno 2] No such file or directory when running through a bash script

I have a folder named test-folder with two files.
# file name: demo.py
print('hello world')
# file name: script.sh
python3.7 demo.py
The test-folder is present inside /home/username/Documents/
I have exported the above path to the .bashrc.
Now when I try to execute the script.sh using the following command,
bash script.sh
I get the following error
python3.7: can't open file 'demo.py': [Errno 2] No such file or directory
How can I solve this? I'm on Ubuntu 18.04
Move to the folder containing the file :
$ cd /home/username/Documents/test-folder
after making sure you are in the folder ccontaining the file try:
$ python file_name.py
if it doesn't work try the full path to python and the full path to the file like this:
$ /usr/lib/python3.7 /home/username/Documents/test-folder/file_name.py
If you want to use script.sh from anywhere, your approach won't work.
When executing $ bash script.sh, the file script.sh is expected to be located in the working directory.
You can use absolute paths to solve this:
# filename: script.sh
python3.7 /home/username/Documents/test-folder/demo.py
Try executing this with bash /home/username/Documents/test-folder/script.py.
You can achieve your goal in an arguably simpler way by adding /home/username/Documents/test-folder to your PATH, adding a shebang to demo.py and making demo.sh executable.
$ export PATH=/home/username/Documents/test-folder:$PATH
$ chmod +x /home/username/Documents/test-folder/demo.py
demo.sh will look like this with a shebang:
#!/usr/lib/python3.7
print('hello world')
and can be executed from anywhere with $ demo.sh.
It works for me. Is there anything about your setup that is different than mine?
$ cat demo.py
print("Hello World")
$ cat script.sh
python3.7 demo.py
$ tree
.
└── test-folder
├── demo.py
└── script.sh
1 directory, 2 files
$ cd test-folder/
$ bash script.sh
Hello World
Edit: Try the following.
$ cat script.sh
python3.7 "`dirname "$0"`/demo.py"
If your script.sh file is only there to run the Python script, it’s not needed. Instead make the Python script itself executable.
(1) add this as the first line in your Python file — this tells Unix which interpreter to use when running this file:
#!/usr/bin/env python37
(2) make your Python file executable so it can be run directly:
chmod +x demo.py
(3) put that file on the path, e.g. a path you set via a Bash config file, or one that’s already on the path like /usr/local/bin.
Now any time you give the command demo.py your script will run. Note the .py extension isn’t required — you can name the file anything you like.

problem with Run python3 code in Atom on ubuntu

script i am using this package on atom.
getting this error
[Command: python3 -u '/home/aditya/Documents/python/py4e book solved problems/not done py4e 5.10ex q1 loop.py']
after making change to. [/home/aditya/.atom/packages/script/lib/grammars] i had changed python to python3
exports.Python =
'Selection Based':
command: 'python3'
args: (context) -> ['-u', '-c', context.getCode()]
'File Based':
command: 'python3'
args: ({filepath}) -> ['-u', filepath]
EDIT: if you just want to run python3 through atom:
Open the atom-python-run package settings (atom Settings >> packages >> find atom-python-run >> settings)
In F5 command section, change the default to python3 {file}. It should look something like this.
EDIT: Make sure that F5 doesn't interfere with another command from another package. Or else you can use F6 as well.
Restart, atom. You should be good to go!
If you aren't able to run python scripts through atom. Then here's the package you can install.
atom-python-run
In short:
You need python installed on your computer and you need to add it to your PATH
Restart atom
To run your (.py) file, hit F5 or F6
Hope this helps!
Try running "sudo ln -s /usr/bin/python3 /usr/bin/python".
Anyway you can change the default to python3 {file} on settings atom python run from.

How to use wildcard in command prompt while executing the pytest test cases

I have below project structure in Pycharm.
Project Folder: PythonTutorial
Package: pytestpackage
Python Files: test_conftest_demo1.py, test_conftest_demo2.py
I'm trying to run the above 2 python files having almost similar name using pytest from command prompt with the below command. But I'm facing the below issue. Please help me on the same.
Note: I'm using windows 10 operating system .
Command Used:
py.test -s -v test_conftest_demo*.py
use the -k option to specify substring matching.
$ pytest -s -v -k "test_conftest_demo"

python script doesn't execute when calling it from shell

I have a python 3 program in file foo the file has execution permissions, and the first line of the script is
#!/usr/bin/python3
When I run the file from python directly, i.e:
/usr/bin/python3 foo 3 boo
Everything runs perfectly well, but when I try to run the program by calling the file name I get:
foo 3 boo
foo: Command not found.
Even when specifying the relative or full path to the script I get the same response.
./foo 3 boo
./foo: Command not found.
/full/path/to/the/file/foo 3 boo
/full/path/to/the/file/foo: Command not found.
Some more info that was requested:
head -2 foo
#!/usr/bin/python3
which python3
/usr/bin/python3
Working from tcsh, where is the problem?
I'm working on a remote university computer through ssh, could it be the case that the sysadmins somehow prevent this?
You need to specify the absolute path of the script. Try
./foo 3 boo
while in the same directory as the script. Otherwise, the shell will only look in your PATH for something named foo, which doesn't exist.
You must give the path to the file
./foo
If you just call "foo", the shell will look in the environment variable $PATH the folder that contains the "foo" binary, and he will not find it ...
I'm using python3, just an example illustration..
Just a test script ..
$ cat test.py
#!/python/v3.6.1/bin/python3
import os
print(os.system("ls"))
$ which python3
/python/v3.6.1/bin/python3
Permission to make it executable.. i'm have doubt if the script foo is executable in your case..
$ chmod +x test.py
Test Run..
$ ./test.py
a.py Backup File_Write_Method NWK-old Python_aritsTest Python_Ftp Python_Mail Python_Primer test.py
argpass BASH Network_DeOps Python-3.6.3 Python_Dump Python_FTP Python_Panda readme_python tmp
awk.sh dnspython nslookup Python-3.6.3.tar.xz Python_excep Python_ldap Python_Parsers Regular_Expr tt.csv
0
It might be shell problem as i reproduced the tcsh shell & it fails to run..
$ tcsh
$ ./test
./test: Command not found.
but it runs , when i run it like below..
tcsh -c ./test.py
Assuming your script is executable and you have python3 in /usr/bin/python3. I think you are trying to run your script from a partition mounted with the noexec argument to check if it is the problem you should run:
mount | grep <partition> | grep noexec
If I'm right you can just mount the partition with the exec option or move your script elsewhere.

Categories

Resources