How do I activate venv in each new terminal in a Dev Container? - python-venv

I'm trying to create a devcontainer.json that will create a Python virtual environment and activate it in the terminal immediately.
I use postCreateCommand to create the environment, and that seems to work:
"postCreateCommand": "python3 -m venv .venv",
But I haven't figured out how to get the terminal to always activate it. I've tried adding this setting:
"settings": {
"python.terminal.activateEnvInCurrentTerminal": true
}
As well as post*Commands:
"postAttachCommand": ". .venv/bin/activate",
"postStartCommand": ". .venv/bin/activate",
"postCreateCommand": ". .venv/bin/activate",
They don't seem to reliably activate it. I think postAttachCommand worked sometimes, but not always.

Related

How to create a Linux GUI app short cut for WSL2 on Windows10?

I have properly installed and setup WSL2. It works fine.
I also setup X11 forwarding and X server (VcXsrv). I can launch GUI apps such like konsole or gvim or even google-chrome from a bash shell.
Now I want to launch konsole by simply double clicking a short cut on the desktop without launching the bash command mode terminal. How should I do it?
I tried running this in cmd:
> wsl /usr/bin/konsole
and it reports:
qt.qpa.xcb: could not connect to display
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.
I'm guessing it is because some X11 forwarding configurations were not properly setup, so I created a k.sh as follows:
#!/usr/bin/bash
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
export LIBGL_ALWAYS_INDIRECT=1
/usr/bin/konsole &
The first two lines were the X11 settings in my .bashrc, the last line launches konsole.
It works fine under bash environment; but when I ran
wsl k.sh
from windows cmd environment, it silently quitted without launching the konsole.
I'm out of ideas. What should I do to directly launch konsole or other Linux GUI apps under windows without having to getting into bash?
Thanks in advance.
You are asking about two different command-lines, and while the failures in running them via the wsl command have the same root-cause, the underlying failures are likely slightly different.
In both cases, the wsl <command> invocation results in a non-login, non-interactive shell where the command simply "runs and exits".
Since the shell is non-login/non-interactive, your startup files (such as ~/.bashrc and ~/.bash_profile, among others) are not being processed.
When you run:
wsl /usr/bin/konsole
... the DISPLAY variable is not set, since, as you said, you normally set it in your ~/.bashrc.
Try using:
wsl -e bash -lic "/usr/bin/konsole"
That will force bash to run as a login (-l), interactive (-i) shell. The DISPLAY should be set correctly, and it should run konsole.
Note that the quotes probably aren't necessary in this case, but are useful for delineating the commands you are passing to bash. More complicated command-lines can be passed in via the quotes.
As for:
wsl k.sh
That's likely a similar problem. You are doing the right thing by setting DISPLAY in your script, but I notice that you aren't using a fully-qualified path it. This would normally work, of course, if your script is in a directory on the $PATH.
But I'm guessing that you might add that directory to the $PATH in your startup config, which means (again) that it isn't being set in this non-login, non-interactive shell.
As before, try:
wsl -e bash -lic "k.sh"`
You could also use a fully-qualified path, of course.
And, I'm fairly sure you are going to run into an issue with trying to put konsole in the background via the script. When WSL exits, and the bash shell process ends, the child konsole process will terminate as well.
You could get around this with a nohup in the script, but then you also need to redirect the stderr. It's probably easiest just to move the & from the script itself to the command-line. Change your k.sh to:
#!/usr/bin/bash
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
export LIBGL_ALWAYS_INDIRECT=1
/usr/bin/konsole
Then run it with:
wsl -e bash -lic "k.sh &"`
Finally, a side note that when and if you can upgrade to Windows 11, it will automatically create Windows Start Menu entries for any Linux GUI app you install that creates a .desktop file. You can manually create .desktop files to have WSL create Start menu items for most applications.
For reference, in Windows 11 it's easier. To run a GUI application without a terminal window popping up, you just need to call wslg.exe instead of wsl.exe.
So, for example:
target: C:\Windows\System32\wslg.exe konsole
start in: C:\WINDOWS\system32
shortcut key: None
comment: Konsole
This tutorial shows how to install VcXsrv and and edit .bashrc to ensure that the "DISPLAY env var is updated on every restart".
DISPLAY env var needs to be dynamic setting.
I've used it successfully with WSL2 on Windows10 Version 21H2 (OS build 19044.2130) to run Chrome, Edge, and thunar. I'm using the Ubuntu 20.04 Linux distro.
To edit .bashrc follow these instructions.

How to change default directory in Windows Subsystem for Linux

I am setting up my development environment, so I just installed Windows Subsystem for Linux and it always seems to open a fresh terminal in my Windows home directory - /mnt/c/Users/dl and I'm trying to make it default to the linux home directory - /home/dl.
I checked to see what the home directory is in the Linux subsystem in /etc/passwd and it is correctly set:
dl:x:1000:1000:,,,:/home/dl:/bin/bash
Then I came across this solution, but it doesn't seem to have any affect:
// Set starting directory
"startingDirectory": "\\\\wsl$\\Ubuntu\\home\\dl\\"
I know I can just run cd ~ in my dot files (which is what I'm currently using), but I'm looking for a way where /home/dl is just the default and cd ~ isn't needed. Is this possible?
You should only change the startingDirectory for WSL (Ubuntu in this case) terminal sessions.
Open settings.json via CTRL+SHIFT+,
Make sure you are modifying startingDirectory under profiles/list/name: "Ubuntu"
Example below (the slashes need to be escaped):
....
{
"guid": "{2c4de342-xxx-xxx-xxx-2309a097f518}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "\\\\wsl$\\Ubuntu\\home\\windows_username_in_lower_case"
},
....
Documentation about startingDirectory including default values and expected values.
Inside settings.json you will also find an explanation of the json schema which is here
If you need to know how or where to edit Windows Terminal settings/preferences: https://learn.microsoft.com/en-us/windows/terminal/get-started
In Windows 10 21H2 or later and Windows 11, it's now much simpler. According to the Microsoft Doc:
On newer versions of Windows, startingDirectory can accept Linux-style paths.
That means you can simply use:
"startingDirectory": "/home/yourusername"
No need for any prefixes for Windows directory structure, nor escaped backslashes. Just plain old Linux forward-slash notation.
This works in both WSL1 and WSL2.
Note: I tried to use "~" and it failed. There may be some way to use {$USERPROFILE}, but haven't tried it.
Changing the home directory with WSL is done the same way as in Linux:
Enter bash
Type the command sudo vim /etc/passwd
Find your account's line, which might look like:
shadyar:x:1000:1000:"",,,:/home/shadyar:/bin/bash
Change the home directory, which above is /home/shadyar, to the new directory, using WSL
note: If you want to set Windows directory as home directory, you need to prepend it with /mnt/, like /mnt/c for C:/, /mnt/d for D:/, etc
Save the file and exit vim by typing :wq and press Enter
Exit bash and re-launch it
To test, use the commands:
cd ~
pwd
The other answers here (especially the latest one from #TomBoland) are great for starting in an arbitrary directory, but the example in your question was to start in your home directory. The easiest way to do that is simply to create or change the "commandline" property to wsl ~. This is an undocumented flag to wsl.exe, and it must be the first argument (e.g. wsl ~ -u root).
Current and Recent Windows Terminal Releases
Since Windows Terminal now has a GUI for Settings, you can just edit your profile to point to wsl ~ in the ->General->Command Line setting.
Older Windows Terminal Releases, or if you want to edit manually
If you are editing your settings.json directly (currently found in %userprofile%\AppData\Local\Packages\MicrosoftWindowsTerminal...\LocalState\settings.json, but this may change) ...
Remove the "source" attribute and replace it with "commandline":
"guid": "{2d5ef231-38b7-51cf-b940-2309a097f644}",
"hidden": false,
"name": "Ubuntu",
//"source": "Windows.Terminal.Wsl",
"commandline": "wsl ~",
"startingDirectory": "//wsl$/Ubuntu/",
"tabTitle": "Ubuntu"
Also, for the fun of it, here's an alternative (hacky) way to open WSL to ~/$HOME (without hardcoding as with the other answers). This is absolutely not needed since it's much easier to use wsl ~, but:
wsl -e sh -c 'cd $HOME; exec $SHELL'
This starts up sh, changes the directory to $HOME, and then exec's your $SHELL to replace the sh.
Should you use Windows Terminal with WSL, then the simplest solution is to configure the starting directory via the Settings menu:
and then:
startingDirectory Should be a windows path, not a nix path. Try D:\Folder\SubFolder instead
refer this link,worked for me
github
I tried many things here and none worked but I finally found a workaround.
After opening your ubuntu, you can set the default path by editing your .bashrc file.
I personally wanted to change it from the default /home/${my_username} to my current user directory (like command prompt C:/users/${my_username}), so I just ran this in my Ubuntu terminal
echo 'cd "../../mnt/c/users/${my_username}"' >> $HOME/.bashrc
Step 1: Open windows command prompt and type "bash"
or open Linux app directly .
Step 2: Type a route which is something like this : /mnt/c/Users/HP/..(You can enter your desired directory here).
For example : /mnt/c/Users/HP/Documents , and by this you will get inside Documents.
For WSL2 Ubuntu the syntax should now match the following example in the json:
"guid": "{2d5ef231-38b7-51cf-b940-2309a097f644}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "//wsl$/Ubuntu/",
"tabTitle": "Ubuntu"
To start in /: "startingDirectory": "//wsl$/Ubuntu/",
To start in /root: "startingDirectory": "//wsl$/Ubuntu/root/",
To start in /home: "startingDirectory": "//wsl$/Ubuntu/home/",
No need to do any of that, just open up the profile for Ubuntu under settings, then update the Command line to add the following option
C:\Windows\system32\wsl.exe -d Ubuntu --cd ~

ConEmu + WSL: Open new console in current tab directory

I'm using WSL and ConEmu build 180506. I'm trying to setup a task in ConEmu to use the current directory of the active tab when opening a new console but I cannot get it to work.
What I did is to setup the task {Bash: bash} using the instructions on this page
setting the task command as :
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -C~ -cur_console:pm:/mnt
Then following the instruction on this page, I added to my .bashrc
if [[ -n "${ConEmuPID}" ]]; then
PS1="$PS1\[\e]9;9;\"\w\"\007\e]9;12\007\]"
fi
and finally setup a shortcut using the macro :
Shell("new_console", "{bash}", "", "%CD%")
But it always open the new console in the default directory ('/home/[username]').
I don't understand what I'm not doing right.
I also noticed that a lot of environment variables listed here are not set. Basically, only $ConEmuPID and $ConEmuBuild seem to be set.
Any help would be appreciated.
GuiMacro Shell was intended to run certain commands, not tasks.
You think you may try to run macro Task("{bash}","%CD%")
Or set your {bash} task parameters to -dir %CD% and just set hotkey for your task.
Of course both methods require working CD acquisition from shell. Seems like it's OK in your case - %d shows proper folder.
I found the answer:
Shell("new_console:I", "bash.exe", "", "%CD%")
The readme is actually pretty good: https://github.com/cmderdev/cmder/blob/master/README.md

How do you make Chromium command line switches on a Chromebook?

I recently saw that ChromeOS added the functionality to do split screen windows in tablet mode in the most recent dev releases. So I put my Chromebook R11 in dev mode for the first time and updated to version 62.
The flag is one of the many on this list https://peter.sh/experiments/chromium-command-line-switches/
The only resources for actually executing these switches was http://www.chromium.org/for-testers/command-line-flags
So I tried following the steps. I went to the crosh shell with Ctrl-Alt-T. Then I typed "shell". Then "sudo su". Then I tried to modify with "sudo vim /etc/chrome_dev.conf", but it was readonly so it didn't save.
So I visited here www dot chromium dot org/chromium-os/poking-around-your-chrome-os-device and followed the steps to making changes to the filesystem and disabling rootfs verification. But the command it told me to enter just gave me an error: "make_dev_ssd.sh: ERROR: IMAGE /dev/mmcblk0 IS NOT MODIFIED."
I'm running out of ideas and resources here..
make_dev_ssd.sh is how you disable rootfs verification and modify files in the rootfs. If that's not working, that might be a bug in that script that should be reported & fixed upstream (e.g. https://crbug.com/new).
That said, are you sure you need to pass a command line flag ? Look at chrome://flags and see if the feature you want to access is available there. Many command line flag is also available on that page.
Try this:
sudo su
cp /etc/chrome_dev.conf /usr/local/
mount --bind /usr/local/chrome_dev.conf /etc/chrome_dev.conf
echo "--arc-availability=officially-supported " >> /etc/chrome_dev.conf

Vagrant stuck in "Waiting for VM to Boot"

I want to preface this question by mentioning that I have indeed looked over most if not all vagrant "Waiting for VM to Boot" troubleshooting threads:
Things I've tried include:
vagrant failed to connect VM
https://superuser.com/questions/342473/vagrant-ssh-fails-with-virtualbox
https://github.com/mitchellh/vagrant/issues/410
http://vagrant.wikia.com/wiki/Usage
http://scotch.io/tutorials/get-vagrant-up-and-running-in-no-time
And more.
Here's how I setup my Vagrant:
Note: We are using Vagrant 1.2.2 since we do not at the moment have time to change configs to newer versions. I am also using VirtualBox 4.2.26.
My office has an /official/ folder which includes things such as Vagrantfile inside. Inside my Vagrantfile are these custom settings:
config.vm.box = "my_box"
config.ssh.private_key_path = "~/.ssh/github_rsa"
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
config.ssh.max_tries = 300
config.vm.provision :shell, :inline => "/etc/init.d/networking restart"
I installed our custom box (called package.box) via vagrant box add my_box absolute_path/package.box which went without a hitch.
Running vagrant up, I would look at the "preview" of the VirtualBox, and it would simply be stuck at the login page. My Terminal would also only say: Waiting for VM to boot. This can take a few minutes. As far as I know, this is an SSH issue. Or my private key issues, though in my Vagrantfile I explicitly pointed to my private key location.
Interesting Notes:
Running dhclient within the VirtualBox GUI, it says command no found. Running sudo dhclient eth0 was one of the suggested fixes.
This fix: https://superuser.com/a/343775/298915 of "modify the /etc/rc.local file to include the line sh /etc/init.d/networking restart just before exit 0." did nothing to fix the issue.
Conclusion:
Having tried to re-install everything thinking I messed up a file, it did not seem to ameliorate the issue. I am unable to work with this issue. Could someone give me some insight?
So after around twelve hours of dejected troubleshooting, I was able to (finally) get the VM to boot.
Setup your private/public keys using the link provided. My box is a Debian Linux 3.2.0-4-amd64, so instead of /root/.ssh/id_rsa.pub, you have to use /home/vagrant/.ssh/id_rsa.pub (and the respective id_rsa path for the private key).
Note: make sure your files have the right permissions. Check using ls -l path, and change using chmod. Your machine may not have /home/vagrant/.ssh/authorized_keys, so generate that file with touch /home/vagrant/.ssh/authorized_keys.
Boot your VM using the VirtualBox GUI using (through either Vagrantfile boot-GUI command, or starting your VM using VirtualBox). Login using vagrant and vagrant when prompted.
Within the GUI, manually start dhclient using sudo dhclient eth0 -v. Why is it off by default? I have no idea. I found out that it was off when I tried to wget the private/public keys in the tutorial above, but was unable to.
Go to your local machine's command line and reload vagrant using vagrant reload. It should boot, and no longer hang at "Waiting for VM to Boot."
This worked for me. Though it may be different for other machines, for whatever reason Vagrant likes to break.
Suggestion: can this be saved as a script so we don't need to manually do this everytime?
EDIT: Update to the latest version of Vagrant, and you will never see this issue again. About time, huh?