How to run different python version based on project location? - python

My system is 5.0.9-2-MANJARO
I've already set the python scripts workaround described in archwiki
➜ ~ cat /home/nfl/bin/python
#!/bin/bash
script=$(readlink -f -- "$1")
case "$script" in (/opt/cocos2d-x/*)
exec python2 "$#"
;;
esac
exec python3 "$#"
➜ ~ which python
/home/nfl/bin/python
➜ ~ echo $PATH
/home/nfl/.nvm/versions/node/v10.15.3/bin:/home/nfl/bin:/usr/local/bin:/opt/cocos2d-x/tools/cocos2d-console/bin:/opt/cocos2d-x/tools/cocos2d-console/plugins/plugin_package:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin
➜ ~
When I run python script that output current python version, it output python2 in directory specified in the script(/opt/cocos2d-x/*) and python3 in non specified dir. So everything work fine now.
The problem is when I try to run sdkbox(cocos2d plugin) with PATH, it seem like python3 is used instead of python2.
➜ ~ which sdkbox
/opt/cocos2d-x/tools/cocos2d-console/plugins/plugin_package/sdkbox
➜ ~ sdkbox
RuntimeError: Bad magic number in .pyc file
➜ ~
But when I run it directly from the directory, it works
➜ plugin_package pwd
/opt/cocos2d-x/tools/cocos2d-console/plugins/plugin_package
➜ plugin_package ./sdkbox
_______ ______ _ _ ______ _____ _ _
|______ | \ |____/ |_____] | | \___/
______| |_____/ | \_ |_____] |_____| _/ \_
Copyright (c) 2016-2018 SDKBOX Inc. v1.0.2.8
usage: sdkbox [-h] [-v] [-p [PROJECT]] [-b [PLUGIN]] [-D SYMBOL] [-i INPUT]
[-o OUTPUT] [-q] [-d [DAYS]] [-l LEGACY] [--key KEY] [--dryrun]
[--forcedownload] [--noupdate] [--alwaysupdate] [--patcherrors]
[--nopatching] [--nopatchingcpp] [--jsonapi] [--forcecopy]
[--mkey MKEY] [--mvalue MVALUE] [--local] [--remote]
[--info INFO] [--runin RUNIN] [--apitoken APITOKEN]
[--silenttime SILENTTIME] [--projectpath PROJECTPATH]
[--platform PLATFORM]
{import,info,update,forget,restore,list,clean,symbols,version,set,tracking,encrypt,decrypt}
sdkbox: error: too few arguments
➜ plugin_package
What happen exactly and how to fix this?

Shebang might be your solution. It addresses the python interpreter by the shebang.
Should I put #! (shebang) in Python scripts, and what form should it take?

I would suggest using virtual environment for this purposes.
virtualenv

Related

The command mkvirtualenv does not seem to work?

I'm trying to create a virtual environment, but when I input the mkvirtualenv command this happens
PS C:\Users\jorel\Documents> mkvirtualenv '.\Trading Bot\
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--read-only-app-data] [--app-data APP_DATA] [--reset-app-data] [--upgrade-embed-wheels] [--discovery {builtin}] [-p py] [--try-first-with py_exe]
[--creator {builtin,cpython3-win,venv}] [--seeder {app-data,pip}] [--no-seed] [--activators comma_sep_list] [--clear] [--no-vcs-ignore] [--system-site-packages] [--copies] [--no-download | --download]
[--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip] [--no-setuptools] [--no-wheel] [--no-periodic-update] [--symlink-app-data] [--prompt prompt] [-h]
dest
virtualenv: error: unrecognized arguments: Bot"
SystemExit: 2
I was expecting something like this
(Trading Bot) PS C:\Users\jorel\Documents>
Can you try :
python -m venv venv
source venv/bin/activate
I create my virtual environment like this

python3 finds some programs in a folder, but not others

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

Run Python scripts without explicitly invoking `python`

I am trying to run my python script without writing python func.py.
I've added to my script file #!/usr/bin/python2.7
did chmod +x func.py
when I try to run: ./func.py -p show -c all
the script works fine but if I try to take off the "flags" (-p , -c) or the "./" or ".py" it won't work.
taking flags off returns:
[root#pg66 tmp]# ./func.py show all
usage: func.py [-h] [-p PROCESS] [-c CLUSTER] [-t TYPE]
func.py: error: unrecognized arguments: show all
taking "./" & ".py" off returns:
[root#pg66 tmp]# func.py -p show -c all
-bash: func.py: command not found
edit: i have 3 flags -p -c -t , dont know where the -h came from. -t can be null so when i write -p show -c all it works.
you can simply use the chmod +* x in the location with the python file. This makes it easier for linux/unix After that you can run it by ./main.py Assuming the file is named as main.py. You can also remove the .py extension then give the permission by chmod +* x and after that then just execute by ./main. Another way to do this is simply going to the execution script of your terminal then add an alias for directly running the script just by typing the alias name

Why does 'python --version' not print string [duplicate]

This question already has answers here:
Detect python version in shell script
(18 answers)
Closed 4 years ago.
I was trying to write a bash script to test the python version. However, I found python --version behave weirdly for python 2, as I can't process its output using any tool. I have tested the same script on Mac OS (10.13.5) and AWS Linux (GUN/Linux #1 SMP Fri Feb 16 00:18:48 UTC 2018). So I think the problem is related to python 2.
The script and corresponding output are:
$ echo $(python --version) | awk '{print $2}'
> Python 2.7.10
But the output should be 2.7.10.
$ echo $(python --version) > weird.txt
> Python 2.7.10
$ cat weird.txt
>
So the output cannot be written into a file as well.
The same script to test for python3 has a totally different result
$ echo $(python3 --version) | awk '{print $2}'
> 3.6.5
But python3's output can be written into a file.
$ echo $(python3 --version) > weird.txt
$ cat weird.txt
> Python 3.6.5
I have found the reason for this difference is that python --version does not output a normal string or whatsoever. Maybe it calls another command to output the result for it??? Thus the result cannot be caught by current process?? (just pure guess here)
Can anyone help me to figure out why there is the difference here? There are probably of million ways to test for python version. But I'm just super curious about what is happening here.
Thanks for all the replies. Just found a useful answer explaining that why python -V outputs to stderr:
Why does python print version info to stderr?
Python outputs the version to standard error (stderr) up to version 3.3 according to issue 18338 and as noted here, so redirect accordingly:
$ echo $(python --version 2>&1) | awk '{print $2}'
2.7.14
The command substitution is unnecessary and this could be written as:
$ python --version 2>&1 | awk '{print $2}'
How about using Python command itself using platform library of it(which is a very common in use).
python -c 'import platform; print(platform.python_version())'
When I run it I get Python's exact version.

Formatting Usage with Docopt

I am having a problem with my usage statements in docopt.
This is how I'd expect usage to work in the script. The optional parameters (defined with []), I would like to be able to use them together or individually. So -t -o or -o or -t should be valid. At the moments I cant use -o without -t.
If i use pipe | to separate them I can't use both at the same time. I've tried various combinations. I cant seem to get it work as id like. Can anyone point out where I am going wrong?
"""
Description:
Script does stuff
Usage:
script.py (-d <ditem>) (-u <uitem>) (-p <pitem>) (-s <sfile>) [-t <tfile>] [-o <ofile>] [-v]
script.py (-d <ditem>) (-l) [-t <tfile>] [-o <ofile>] [-v]
script.py -h | --help
script.py --version
Options:
-v --verbose Does stuff
-t --tfile Does stuff
-o --output Does stuff
-l --litem Does stuff
-u --uitem Does stuff
-p --pitem Does stuff
-d --ditem Does stuff
-s --sitem Does stuff
-h --help Show this screen.
--version Show version.
"""
I was able to resolve this by using the following:
By adding the usage strings script.py (-d <ditem>) (-l) ([-t <tfile>] | [-o <ofile>]) [-v] and another script.py (-d <ditem>) (-l) [-t <tfile>] [-o <ofile>] [-v] means I can use -t and -o independently or -t -o together. However Im not able to use them in this order -o -t.
Description:
Script does stuff
Usage:
script.py (-d <ditem>) (-u <uitem>) (-p <pitem>) (-s <sfile>) [-t <tfile>] [-o <ofile>] [-v]
script.py (-d <ditem>) (-l) ([-t <tfile>] | [-o <ofile>]) [-v]
script.py (-d <ditem>) (-l) [-t <tfile>] [-o <ofile>] [-v]
script.py -h | --help
script.py --version
To allow -t along, -o along, -t and -o together:
Script does stuff.
Usage:
script.py [-t] [-o]
Options:
-t --tfile Does stuff
-o --output Does stuff
If it is an error when both -t and -o are absent:
Script does stuff.
Usage:
script.py -t
script.py -o
script.py -t -o
Options:
-t --tfile Does stuff
-o --output Does stuff

Categories

Resources