Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Can't activate Python virtual enviroment due to syntax error
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Sat Nov 12, 2022 3:22 pm    Post subject: Can't activate Python virtual enviroment due to syntax error Reply with quote

For some reason I get syntax error when I try to activate Python virtual environment.

Code:
python3 -m venv env
source env/bin/activate


Produces following error:
Code:
bash: env/bin/activate: line 36: syntax error near unexpected token `}'
bash: env/bin/activate: line 36: `}'


The sourced file has the following contents:
Code:
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

deactivate () {
    # reset old environment variables
    if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
        PATH="${_OLD_VIRTUAL_PATH:-}"
        export PATH
        unset _OLD_VIRTUAL_PATH
    fi
    if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
        PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
        export PYTHONHOME
        unset _OLD_VIRTUAL_PYTHONHOME
    fi

    # This should detect bash and zsh, which have a hash command that must
    # be called to get it to forget past commands.  Without forgetting
    # past commands the $PATH changes we made may not be respected
    if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
        hash -r 2> /dev/null
    fi

    if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
        PS1="${_OLD_VIRTUAL_PS1:-}"
        export PS1
        unset _OLD_VIRTUAL_PS1
    fi

    unset VIRTUAL_ENV
    unset VIRTUAL_ENV_PROMPT
    if [ ! "${1:-}" = "nondestructive" ] ; then
    # Self destruct!
        unset -f deactivate
    fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="/home/msj/testtti/env"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
    _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
    unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
    _OLD_VIRTUAL_PS1="${PS1:-}"
    PS1="(env) ${PS1:-}"
    export PS1
    VIRTUAL_ENV_PROMPT="(env) "
    export VIRTUAL_ENV_PROMPT
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands.  Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
    hash -r 2> /dev/null
fi


Is this something to do with env variables since the syntax itself should be fine?
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23671

PostPosted: Sat Nov 12, 2022 4:09 pm    Post subject: Reply with quote

No, environment variables should not be able to cause that problem.

That script is well-formed for me. In the shell where you received the error message, what is the output of /proc/self/exe --version?
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Sat Nov 12, 2022 5:31 pm    Post subject: Reply with quote

Hmm, I see..

The output was:
Code:

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23671

PostPosted: Sat Nov 12, 2022 6:45 pm    Post subject: Reply with quote

That looks reasonable. Your shell is fairly current. Are there any carriage returns in the script? Those could have been lost when pasting it onto the forum, allowing it to work as posted, but not as stored on your disk. If you copy the script shown in your post into a new script file, and source that, does it also fail? Have you tried trimming down the script to find the minimal version that still fails?
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Sun Nov 13, 2022 9:22 am    Post subject: Reply with quote

No, I got the same error when copy-pasting the contents. I have other machine that has pretty identical Gentoo install and it doesn't have same kind of a problem.

I also tried to trim down the script but if it had anything else than variable declarations I got "unexpected end of file"-error.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23671

PostPosted: Sun Nov 13, 2022 5:17 pm    Post subject: Reply with quote

Since the script works for me, and works for one of your other Gentoo installations, that suggests an obscure problem with the shell on the bad system. Since I cannot reproduce the problem as posted, I cannot directly debug this. There are plenty of ways to reduce the script that will make it invalid. The goal is to find the one which is closest to the version shown, but is accepted even on the bad system.

What is the output of emerge --info app-shells/bash on the bad system?
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9647
Location: beyond the rim

PostPosted: Sun Nov 13, 2022 7:50 pm    Post subject: Reply with quote

Do you get the same error when you run the script with bash -n (that's just parsing, not executing it)?
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Tue Nov 15, 2022 10:19 am    Post subject: Reply with quote

Hu wrote:
Since the script works for me, and works for one of your other Gentoo installations, that suggests an obscure problem with the shell on the bad system. Since I cannot reproduce the problem as posted, I cannot directly debug this. There are plenty of ways to reduce the script that will make it invalid. The goal is to find the one which is closest to the version shown, but is accepted even on the bad system.

What is the output of emerge --info app-shells/bash on the bad system?


I see.. The output:
Code:

Portage 3.0.38.1 (python 3.10.8-final-0, default/linux/amd64/17.1/desktop, gcc-11.3.0, glibc-2.35-r8, 5.15.41-gentoo-x86_64 x86_64)
=================================================================
                         System Settings
=================================================================
System uname: Linux-5.15.41-gentoo-x86_64-x86_64-Intel-R-_Core-TM-_i5-7200U_CPU_@_2.50GHz-with-glibc2.35
KiB Mem:     8020328 total,   4758356 free
KiB Swap:          0 total,         0 free
Timestamp of repository gentoo: Tue, 08 Nov 2022 10:30:01 +0000
Head commit of repository gentoo: 00c0ce86981e14560b908363ccaa92cd30e93966
sh bash 5.1_p16-r2
ld GNU ld (Gentoo 2.37_p1 p2) 2.37
app-misc/pax-utils:        1.3.5::gentoo
app-shells/bash:           5.1_p16-r2::gentoo
dev-java/java-config:      2.3.1::gentoo
dev-lang/perl:             5.34.1-r4::gentoo
dev-lang/python:           3.10.8_p2::gentoo, 3.11.0_p1::gentoo
dev-lang/rust-bin:         1.64.0-r1::gentoo
dev-util/cmake:            3.24.2::gentoo
dev-util/meson:            0.63.2-r1::gentoo
sys-apps/baselayout:       2.8::gentoo
sys-apps/openrc:           0.45.2-r1::gentoo
sys-apps/sandbox:          2.29::gentoo
sys-devel/autoconf:        2.71-r1::gentoo
sys-devel/automake:        1.16.5::gentoo
sys-devel/binutils:        2.37_p1-r2::gentoo, 2.38-r2::gentoo
sys-devel/binutils-config: 5.4.1::gentoo
sys-devel/gcc:             11.3.0::gentoo
sys-devel/gcc-config:      2.8::gentoo
sys-devel/libtool:         2.4.7::gentoo
sys-devel/llvm:            14.0.6-r2::gentoo, 15.0.3::gentoo
sys-devel/make:            4.3::gentoo
sys-kernel/linux-headers:  5.15-r3::gentoo (virtual/os-headers)
sys-libs/glibc:            2.35-r8::gentoo
Repositories:

gentoo
    location: /var/db/repos/gentoo
    sync-type: rsync
    sync-uri: rsync://rsync.gentoo.org/gentoo-portage
    priority: -1000
    sync-rsync-verify-jobs: 1
    sync-rsync-verify-metamanifest: yes
    sync-rsync-verify-max-age: 24
    sync-rsync-extra-opts:

ACCEPT_KEYWORDS="amd64"
ACCEPT_LICENSE="*"
CBUILD="x86_64-pc-linux-gnu"
CFLAGS="-march=native -O2 -pipe"
CHOST="x86_64-pc-linux-gnu"
CONFIG_PROTECT="/etc /usr/share/gnupg/qualified.txt"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/dconf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo"
CXXFLAGS="-march=native -O2 -pipe"
DISTDIR="/var/cache/distfiles"
ENV_UNSET="CARGO_HOME DBUS_SESSION_BUS_ADDRESS DISPLAY GOBIN GOPATH PERL5LIB PERL5OPT PERLPREFIX PERL_CORE PERL_MB_OPT PERL_MM_OPT XAUTHORITY XDG_CACHE_HOME XDG_CONFIG_HOME XDG_DATA_HOME XDG_RUNTIME_DIR XDG_STATE_HOME"
FCFLAGS="-march=native -O2 -pipe"
FEATURES="assume-digests binpkg-docompress binpkg-dostrip binpkg-logs binpkg-multi-instance buildpkg-live config-protect-if-modified distlocks ebuild-locks fixlafiles ipc-sandbox merge-sync multilib-strict network-sandbox news parallel-fetch pid-sandbox preserve-libs protect-owned qa-unresolved-soname-deps sandbox sfperms strict unknown-features-warn unmerge-logs unmerge-orphans userfetch userpriv usersandbox usersync xattr"
FFLAGS="-march=native -O2 -pipe"
GENTOO_MIRRORS="https://mirror.leaseweb.com/gentoo/"
LANG="en_US.utf8"
LDFLAGS="-Wl,-O1 -Wl,--as-needed"
MAKEOPTS="-j4"
PKGDIR="/var/cache/binpkgs"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --omit-dir-times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages --exclude=/.git"
PORTAGE_TMPDIR="/var/tmp"
SHELL="/bin/bash"
USE="X aac acl acpi alsa alsa-plugin amd64 bluetooth branding bzip2 cairo cli crypt dbus dri dts elogind encode exif flac fortran gdbm gif gpm gtk gui iconv icu ipv6 jpeg lcms libglvnd libnotify libtirpc mad minizip mng mp3 mp4 mpeg multilib native-headset ncurses network-manager nls nptl ofono-headset ogg opengl openmp opus pam pango pcre pdf png policykit ppds pulseaudio qt5 readline sdl seccomp spell split-usr ssl startup-notification svg test-rust tiff truetype udev udisks unicode upower usb vaapi vorbis wxwidgets x264 xattr xcb xml xv xvid zlib" ABI_X86="64" ADA_TARGET="gnat_2020" APACHE2_MODULES="authn_core authz_core socache_shmcb unixd actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="karbon sheets words" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" CPU_FLAGS_X86="mmx mmxext sse sse2" ELIBC="glibc" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock greis isync itrax mtk3301 nmea ntrip navcom oceanserver oldstyle oncore rtcm104v2 rtcm104v3 sirf skytraq superstar2 timing tsip tripmate tnt ublox ubx" INPUT_DEVICES="synaptics libinput" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LIBREOFFICE_EXTENSIONS="presenter-console presenter-minimizer" LUA_SINGLE_TARGET="lua5-1" LUA_TARGETS="lua5-1" OFFICE_IMPLEMENTATION="libreoffice" PHP_TARGETS="php7-4 php8-0" POSTGRES_TARGETS="postgres12 postgres13" PYTHON_SINGLE_TARGET="python3_10" PYTHON_TARGETS="python3_10" RUBY_TARGETS="ruby27" USERLAND="GNU" VIDEO_CARDS="intel" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq proto steal rawnat logmark ipmark dhcpmac delude chaos account"
Unset:  ADDR2LINE, AR, ARFLAGS, AS, ASFLAGS, CC, CCLD, CONFIG_SHELL, CPP, CPPFLAGS, CTARGET, CXX, CXXFILT, ELFEDIT, EMERGE_DEFAULT_OPTS, EXTRA_ECONF, F77FLAGS, FC, GCOV, GPROF, INSTALL_MASK, LC_ALL, LD, LEX, LFLAGS, LIBTOOL, LINGUAS, MAKE, MAKEFLAGS, NM, OBJCOPY, OBJDUMP, PORTAGE_BINHOST, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS, RANLIB, READELF, RUSTFLAGS, SIZE, STRINGS, STRIP, YACC, YFLAGS

=================================================================
                        Package Settings
=================================================================

app-shells/bash-5.1_p16-r2::gentoo was built with the following:
USE="net nls (readline) -afs -bashlogger -examples -mem-scramble -plugins -verify-sig" ABI_X86="(64)"
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Tue Nov 15, 2022 10:20 am    Post subject: Reply with quote

Genone wrote:
Do you get the same error when you run the script with bash -n (that's just parsing, not executing it)?


No, it runs without finishes fine without output
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9647
Location: beyond the rim

PostPosted: Tue Nov 15, 2022 10:44 am    Post subject: Reply with quote

jasu wrote:
Genone wrote:
Do you get the same error when you run the script with bash -n (that's just parsing, not executing it)?


No, it runs without finishes fine without output

That would indicate that the issue isn't with the script or bash parsing it, but the "source" command doing something unexpected. What happens if you run
Code:
. env/bin/activate

(should do the same as "source")
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Tue Nov 15, 2022 11:50 am    Post subject: Reply with quote

Yeah, makes sense.
It produces the same error about the syntax as the source command
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9647
Location: beyond the rim

PostPosted: Tue Nov 15, 2022 1:06 pm    Post subject: Reply with quote

Ok, that makes it unlikely that somehow accidentally "source" got modified/redirected.

Well, as you have another system that works the most obvious thing to do is to check what differences there are between the systems. Mainly in any bash-related files within /etc/ and $HOME.
Or try to create a new user account and see if the problem can be reproduced there (if not the issue is with the bash configuration in your original account).

Pretty weird issue, but wouldn't be surprised if it has a facepalm solution.
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Tue Nov 15, 2022 1:21 pm    Post subject: Reply with quote

Your suggestion about facepalm solution made a light bulb turn on in my head. Disabled .bashrc and it works perfectly now. I'll debug later what was the actual cause for this but at least the problem got solved. Thanks to everyone helping with this one!
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23671

PostPosted: Tue Nov 15, 2022 1:23 pm    Post subject: Reply with quote

If you share the broken .bashrc, someone might be able to point you in the right direction.
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Tue Nov 15, 2022 2:49 pm    Post subject: Reply with quote

The contents of the broken .bashrc are:
Code:

# /etc/skel/.bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output.  So make sure this doesn't display
# anything or bad things will happen !


# Test for an interactive shell.  There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
   # Shell is non-interactive.  Be done now!
   return
fi



if [ -e $HOME/.bash_aliases ]; then
    source $HOME/.bash_aliases
fi



########################
#                      #
#       ENV VARS       #
#                      #
########################

EDITOR='/usr/bin/vim'
PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/.local/share/go/bin:$PATH"
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23671

PostPosted: Tue Nov 15, 2022 3:20 pm    Post subject: Reply with quote

All that looks relatively safe (though unchecked $PATH concatenation is technically wrong) - but it also sources in .bash_aliases, which is not shown and could have something weird in it.
Back to top
View user's profile Send private message
jasu
n00b
n00b


Joined: 23 May 2022
Posts: 61

PostPosted: Wed Nov 16, 2022 7:35 am    Post subject: Reply with quote

Ah good to know. The .bash_aliases doesn't have anything special in it.

Code:

alias ls='ls --color=auto -lhF'
alias mutt='neomutt'
alias tp='trash-put'
alias steam='flatpak run com.valvesoftware.Steam'
alias bt='bluetoothctl'
alias halfscreen='xrandr --output DisplayPort-2 --mode 2560x1440'
alias fullscreen='xrandr --output DisplayPort-2 --mode 5120x1440'
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9647
Location: beyond the rim

PostPosted: Wed Nov 16, 2022 1:50 pm    Post subject: Reply with quote

If the .bashrc would be broken you'd have problems in all shell sessions. There is apparently just some very weird interaction going on between that and the source command used for the venv script (can't spot anything suspicious though).
Would probably still create a new account to verify that the issue is isolated to the existing account, and then transfer the content of .bashrc and .bash_aliases piece by piece to narrow down what is triggering the issue.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum