Run a virtual environment globally? - virtualenv

Is there any way to make workon global? For example; I open terminal and type workon myenvname --global, then I open another terminal window and type something like python myscript.py and it will run it under the myenvname environment?
Also I can then open Sublime Text IDE and create a python script, then press CTRL+b and the python script will run in the myenvname environment.
Is such a thing possible?

No. Virtual environments must be activated in every shell (i.e., every terminal) separately.
There is no magic in Python virtual environments. Their activation just sets a few environment variables; the most important is PATH so that the current shell finds python and pip. Then python being run from a virtual env detects it and sets sys.path accordingly.
To some extent you can do a trick without activation: run python from a virtual env:
/path/to/venv/bin/python myscript.py

Hope it helps someone
You may not be able to run it globally but if you want to run a particular python script without entering into the virtualenv. Here's a work around.
Considering you are using this on linux.
Say you have a virtualenv called myenvname
You would like to run a particular python script myscript.py inside this particular myenvname without even using terminal by just double click on an icon.
Create a shell script myshellscript.sh
#!/bin/bash
# open the virtual environment
source /home/usr_name/.virtualenvs/myenvname
# location to the python script you want to run
# python/python3 depending on the version you are using
python location/to/your/python/script/myscript.py
Give permission for myshellscript.sh to be an executable
chmod +x myshellscript.sh
Create a .desktop file inside /usr/share/applications/
sudo nano /usr/share/applications/myscript.desktop
copy paste the block of code and make the changes accordingly
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=shellscript file to be run along with its path
Path=directory where the file is located
Name=myscript
Comment=comment here
Icon=icon path here
then give permission to the myscript.desktop
chmod +x /usr/share/applications/myscript.desktop
You are done.
Just goto /usr/share/applications/ and double click on myscript icon, you got your myscript.py running

Related

Running a Python file on command prompt launches vscode instead

I'm trying to launch a python script on windows command prompt, but instead of it doing it, it opens the script on vscode instead. I searched the options in vscode and didn't find anything. How can I stop this behavior?
SOLVED
I simply changed the default program for .py files
You need to change the default application for .py files to python.exe.
This will instruct Windows to pass the filename of the python script you're trying to open to python.exe, which will execute it.
python my_script.py
As a bonus, if you change the PATHEXT environment variable to include .py extension. This will allow you to run a python script without prefixing it with python command when working in a shell.
./my_script.py
Python installer does this by default, but if it isn't there, you can add it.

How to use Python3 on the VScode terminal?

Is there a way to force VS Code to use only python3? It always defaults to python2.7 no matter what I try. I've tried selecting the correct interpreter as python3.7. When I open up terminal, it immediately uses python2.7, In the settings it is pointing at 3.7, but the built in terminal which is nice, always defaults to 2.7.
First, understand that the integrated terminal of VSCode, by default, uses the same environment as the Terminal app on Mac.
The shell used defaults to $SHELL on Linux and macOS, PowerShell on
Windows 10 and cmd.exe on earlier versions of Windows. These can be
overridden manually by setting terminal.integrated.shell.* in user
settings.
The default $SHELL on Mac is /bin/bash which uses python for Python2.7. So VS Code will just use the same python to mean Python2.7. When you open a bash shell, it will load your ~/.bash_profile to apply custom aliases and other configurations you added into it.
One solution to your problem is edit your ~/.bash_profile to alias python to python3. But I do not recommend this because this affects all your bash sessions, even those outside of VS Code. This can lead to nasty side effects when you run scripts that need python to be the system Python2.7.
You can instead configure VSCode to load its own aliases, for its own integrated terminal. First, create a file named vscode.bash_profile in your home directory:
$ cat ~/vscode.bash_profile
alias python=$(which python3)
On my env, python3 is Python3.7. You can set it to the what's applicable on your env (ex. maybe python3.7). Then, in VS Code, look for the Terminal shell args setting:
and then open your settings.json and add these lines:
"terminal.integrated.shellArgs.osx": [
"--init-file",
"~/vscode.bash_profile",
]
Finally, restart VS Code. The next time you open the VS Code terminal, python should now be using your Python 3 installation. This should not affect your bash session outside of VS Code.
Note that, if you have some custom settings from the default ~/.bash_profile, you may want to copy it over to your ~/vscode.bash_profile, so that you can still use it on VS Code (ex. changes to PATH, git-completion scripts..).

Is there a venv-specific equivalent of virtualenvwrapper's 'postactivate'

After starting my virtualenv, I would like to run a script that depends on some specifics of the environment (e.g. bash completions for some tools installed there). Is there a way to do this on a per-venv basis — effectively a local version of postactivate?
AFAIU postactivate is sourced at workon time. You don't automatically activate a virtualenv, you source bin/activate so you can add your code at the end of the bin/activate script.
To emulate predeactivate edit the same bin/activate script, function deactivate.

Npm command not recognized (ubuntu)

After installing java 8 openjdk and exporting the variable via export I ran the following command:
source /etc/enviorment
I after that when I try to run npm run tests-e2e, it says the command is not recognized.
Executing source /etc/environment has reset your shell's environment variables back to their default values, throwing away any customisations that were applied by your shell's startup scripts (/etc/profile, ~/.bash_profile and so on). In particular, it looks like you've lost the customised value of $PATH that carried the path to the directory where the npm command lives.
To recover, you can do any of:
execute exec bash -l to get a new shell that will execute the
startup scripts and therefore rebuild a properly customised $PATH variable
if you're using the GUI, close the terminal window and open a new one
log out and then log back in

Configure Eclipse to use bash login shell for Cygwin toolchain

I have a custom Makefile project in Eclipse and although the build does get run in a Cygwin shell... it does not seem to be a login shell (bash --login) as it doesn't set my environment variables like running cygwin.bat does.
Where in Eclipse can I change the shell command so that it will be a login shell?
What you actually aim with bash --login are your settings from /etc/profile.
Under UNIX you normally have only one login shell and so these settings are inherited by all other shells. Under Windows any Bash window is an isoloated login shell, which leads to missing environment settings when running Bash from tools that run bash simply as command processor.
I had a similar problem with Emacs compile feature. The best solution under Windows is to set the environment variable BASH_ENV to a script. Bash will execute this script when started without -i or --login, so that /etc/profile is not run. Hence the script will setup Bash for non-interactive, non-login shells.
Example:
BASH_ENV=%USERPROFILE%\.bash_env
as user environment variable. The least thing to do in this script is to set PATH as in /etc/profile:
PATH="/usr/local/bin:/usr/bin:${PATH}"
Check the path-settings in /etc/profile as it is created by Cygwin's setup.exe. You may also copy settings from ~/.bashrc or source this script.
Hope this helps.