Python virtual environment not changing python3 command source - python

I'm running an aws cdk script from a python virtual environment whose cdk.json calls "app": "python3 app.py"
The problem is that the virtual environment is only changing "python" command source but not "python3" command source once activated, so the script won't use the dependencies installed at the virtual environment when I run cdk (which is calling "python3 app.py" instead of "python app.py")
(venv3) PS C:\Users\DavidSanchez\projects\ddna-event-router> Get-Command python
CommandType Name Version Source
----------- ---- ------- ------
Application python.exe 3.11.21... C:\Users\DavidSanchez\projects\ddna-event-router\venv3\Scripts\python.exe
(venv3) PS C:\Users\DavidSanchez\projects\ddna-event-router> Get-Command python3
CommandType Name Version Source
----------- ---- ------- ------
Application python3.exe 0.0.0.0 C:\Users\DavidSanchez\AppData\Local\Microsoft\WindowsApps\python3.exe
I must not change cdk.json code
I'm using a Windows environment
Tried to create the virtual environment using python3 command but it won't work either
PS C:\Users\DavidSanchez\projects\ddna-event-router> python3 -m venv myvenv
PS C:\Users\DavidSanchez\projects\ddna-event-router> .\myvenv\Scripts\activate
(myvenv) PS C:\Users\DavidSanchez\projects\ddna-event-router> Get-Command python
CommandType Name Version Source
----------- ---- ------- ------
Application python.exe 3.11.21... C:\Users\DavidSanchez\projects\ddna-event-router\myvenv\Scripts\python.exe
(myvenv) PS C:\Users\DavidSanchez\projects\ddna-event-router> Get-Command python3
CommandType Name Version Source
----------- ---- ------- ------
Application python3.exe 0.0.0.0 C:\Users\DavidSanchez\AppData\Local\Microsoft\WindowsApps\python3.exe
Is there any way I can fix python3 command behaviour so it points to my virtual environment python.exe?

Related

Python 3.8 or 3.6 | Unable to set the virtual environment | Linux Redhat 7 | VScode 144.1

I tried to setup virtual env under my own directory.
Steps followed as below:
Go to home directory (/home/hari/pyprojects)
Create a folder mkdir testenv and change directory to testenv
Created a virual envirnoment - python3.8 -m venv /home/hari/pyprojects/testenv
activate virtual env - source ./bin/activate
Enter which version
It shows below output when I do "which version".
/usr/bin/which: no version in (/home/hari/pyprojects/testenv/bin:/home/hari/.vscode-server/bin/a9f8623ec050e5f0b44cc8ce8204a1455884749f/bin:/home/hari/.vscode-server/bin/a9f8623ec050e5f0b44cc8ce8204a1455884749f/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/hari/.local/bin:/home/hari/bin)
Why it does not show that is running python 3.8 in the local directory. What do I have to do to run python in my virtual folder. When I create a python script, it fails to recongnize a package when imported.
Please help!
Regards,

How do I activate a conda environment on linux through a python script?

I need to make the statement: "conda activate [environment name]" work inside of a python script. As far as I can tell, it seems conda activate [env name] only works when writing directly to the terminal on Linux. I've tried os.system("conda activate [env name]") and even subprocess.run("conda activate [env name]"), but I always get this error after running the python script:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
I've also tried activating my conda environment beforehand, then running a python script that has "conda deactivate" and that does not work either. Anyone know a solution to this? Is it only possible to have the environment activated only for the duration of the script, and not have it turn on the environment in the terminal after it is ran?
For reference I have conda 4.11.0 and python 3.8.12.
You can circumvent this by creating a bash file with the following commands:
#!/bin/bash
echo "Activating environment"
conda init <SHELL_NAME>
echo "Environment Activated"
echo "Starting Python script"
python3 /path/to/script.py
echo "I'm done!"

Setting up Beeware: I cannot create my JAR support file within my activated venv

I've been trying to set up my work environment so I can attempt the Beeware tutorial.
https://voc.readthedocs.io/en/latest/background/install.html
I'm following the steps of this installation tutorial using Bash Shell in Windows 8 with Python 3.7. I installed all the relevant software and configured the environmental variables etc.
The problem I encounter happens when I try to create the JAR support file inside my activated venv:
$ . env/Scripts/activate
(env) $ cd voc
(env) $ pip install -e .
(env) $ ant java ``` -- at this line of code the shell appears to be endlessly stuck in operation as follows:
(env) $ ant java
Buildfile: C:\Users\XXXX\tutorial\voc\build.xml
java:
compile:
compile_stdlib:
[exec] Cloning into 'ouroboros'...
[exec] Updating files: 17% (340/1941)
I've left this running for more than an hour and it shows no sign of progress.
I'm pretty sure that running the script through the venv is causing the problem because when I don't use any venv the process runs successfully in just over a minute.**
When I input the cd command, the processes don't appear to change over time either.
I've also tried disabling my windows defender to improve CPU performance with no luck.
Any suggestions are what I can do to get this working in the Venv?
Thanks in advance
You might want to try with a different environment e.g. on a docker container.
Here is a report of a try with Ubuntu 18.04 LTS as of 2019-11-29. I followed https://voc.readthedocs.io/en/latest/background/install.html
Python 3.7 is used instead of the default 3.6.
python --version
Python 3.7.5
Java 1.8
javac -version
javac 1.8.0_25
ant as installed by Ubuntu package manager:
ant -version
Apache Ant(TM) version 1.10.5 compiled on March 28 2019
I had to install the python 3.7 venv see How to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?
sudo apt-get install python3.7-venv
cloning the repository
git clone https://github.com/beeware/voc.git
activating the virtual environment
python3 -m venv env
$ . env/bin/activate
$ cd voc
$ pip install -e .
and now finally running the ant command that gave you trouble:
ant java
Buildfile: /hd/sengo/home/wf/source/python/beeware/voc/build.xml
java:
compile:
[mkdir] Created dir: /hd/sengo/home/wf/source/python/beeware/voc/build/java
[javac] Compiling 140 source files to /hd/sengo/home/wf/source/python/beeware/voc/build/java
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
compile_stdlib:
[exec] Cloning into 'ouroboros'...
[exec] Cloning Ouroboros...
[exec] Compiling 95 python modules...
[exec] ...............................................................................................
[exec] Built 95 modules
dist:
[mkdir] Created dir: /hd/sengo/home/wf/source/python/beeware/voc/dist
[jar] Building jar: /hd/sengo/home/wf/source/python/beeware/voc/dist/Python-3.7-Java-support.b7.jar
[copy] Copying 1 file to /hd/sengo/home/wf/source/python/beeware/voc/dist
compile:
compile-testdaemon:
[mkdir] Created dir: /hd/sengo/home/wf/source/python/beeware/voc/build/java-testdaemon
[javac] Compiling 2 source files to /hd/sengo/home/wf/source/python/beeware/voc/build/java-testdaemon
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
dist-testdaemon:
[jar] Building jar: /hd/sengo/home/wf/source/python/beeware/voc/dist/Python-3.7-Java-testdaemon.b7.jar
[copy] Copying 1 file to /hd/sengo/home/wf/source/python/beeware/voc/dist
BUILD SUCCESSFUL
Total time: 56 seconds

Virtualenv Wrapper is installing both python version in the virtual environments

The major issue is i only want python3.0+ version of python in my virtualenvironment. But when i create a virtualenvironment with mkvirtualenv then both python2 and python3 gets install inside my virtualenvironments.
Images:
Bashrc Settings :
virtualenv settings :
When you type pythonTab bash shows all pythons in the $PATH — some from /usr/bin/, some from $VIRTUAL_ENV/bin/. virtualenv doesn't separate bash from the system, it only protects one version of Python from the global site-packages/ directory.
If you've configured locate you can see all your pythons using command
locate bin/python
To limit the list for those in $PATH:
for p in ${PATH//:/ }; do
ls $p/python* 2>/dev/null
done

Allow use of system python in conda env?

Is there a way to force conda to use the system version of python (along with all of the system libraries) in a given env?
I have conda enabled by default in my shell, which can get a bit annoying, because if I try to run a system python app, it gets a different version of python to what it is expecting (python still defaults to 2.7 on *buntu), and often won't run. I would like the root env of conda to just be a redirect to the system python install.
You need to edit all user shell run commands such as your .bashrc file to prepend the bin directory of anaconda to path export PATH=~/anaconda/bin:$PATH, while in your root run commands append export PATH=$PATH:~/anaconda/bin. In both cases you will have access to the conda command. You can check which python will be run by typing $env python --version. You can also check which other versions would be available and their order of priority (if the other is removed) by using $type -a python. Of course ensure your executable python files have #!/usr/bin/env python and not some other direct route to a python executable. For further info Google BASH Shell look up queries like http://www.cyberciti.biz/tips/an-example-how-shell-understand-which-program-to-run-part-ii.html.
Simply removing the python symlink from ~/miniconda3/bin/ appears to do the job.
$ which python
/home/naught101/miniconda3/bin/python
$ rm /home/naught101/miniconda3/bin/python
$ which python
/usr/bin/python
$ source activate science
discarding /home/naught101/miniconda3/bin from PATH
prepending /home/naught101/miniconda3/envs/science/bin to PATH
(science)$ which python
/home/naught101/miniconda3/envs/science/bin/python
(science)$ source deactivate
discarding /home/naught101/miniconda3/envs/science/bin from PATH
$ which python
/usr/bin/python
So far, this doesn't seem to have caused me any problems. Unfortunately the same doesn't work for ~/miniconda/bin/python3, because conda requires it when switching to other envs that use the same python version. However, that one hasn't caused as many problems in the first place.
If this does cause problems, it's easy enough to undo, just cd ~/miniconda/bin/; ln -s python3 python (or what ever version of python you're using in your conda root env). You may need to activate/deactivate an env to get that version of python back on your PATH.
If you are in (base) or another environment, use conda deactivate, this will exit conda's environment and place you back into the OS environment:
on MacOS
(base) user$ python --version
Python 3.9.12
(base) user$ conda deactivate
user$ python --version
Python 2.7.16
on Linux (with no base python installed)
(base) user$ python --version
Python 3.9.15
(base) user$ conda deactivate
user$ python --version
-bash: python: command not found

Categories