how can I activate virtual environment in python shell - python

I have created a virtual environment (directory is env) and have installed some third party packages. I work with Mac OSX. How can I activate my venv in the python interactive shell? I tried
source env/bin/activate
and get the error message: SyntaxError: invalid syntax
I have seen some had posted this question 8 years ago but I haven't found what I was looking for...I have also checked the python documentation but it is not clear for me how to activate it
https://docs.python.org/3/tutorial/venv.html?highlight=virtual
Can anybody please help?

In your shell, normally python will point you to a default instance of python:
python
Python 2.7.14 (default, Sep 25 2017, 09:53:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
This is because python is located in /usr/local/bin which is part of your default PATH. Now, when you source activate <some_env>, you are modifying PATH, which you can actually see in the /path/to/venv/some_env/bin/activate file:
#!/bin/bash
...
if [[ $PATH == *"CONDA_PATH_PLACEHOLDER"* ]]; then
# If it did, replace it with our _NEW_PART
export PATH="$($_CONDA_PYTHON -c "import re; print(re.sub(r'CONDA_PATH_PLACEHOLDER', r'$_NEW_PART', '$PATH', 1))")"
else
#### HERE IS WHERE THAT PATH IS OVERWRITTEN
export PATH="$_NEW_PART:$PATH"
fi
# CONDA_DEFAULT_ENV is the shortest representation of how conda recognizes your env.
# It can be an env name, or a full path.
# Last date of change: 2016-06-21
# If the string contains / it's a path
if [[ "$#" == */* ]]; then
export CONDA_DEFAULT_ENV=$(get_abs_filename "$args")
else
export CONDA_DEFAULT_ENV="$args"
fi
...
esac
I'm using anaconda but the concept is the same, and ... indicates where I've snipped out parts of the script. It is effectively exporting the venv python executable path into PATH. So you won't be pointing to your venv python unless one of two conditions arises: a) you explicitly call that python executable or b) you source activate myenv and then call python.
So the correct workflow is:
source /path/to/myenv/bin/activate myenv
Then call
python
As an example, the first call to $PATH is before activating my conda env, the second is after:
➜ ~ echo $PATH
/Users/mm92400/bin:/usr/local/bin:/Users/mm92400/anaconda3/bin:/Users/mm92400/.cargo/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands
➜ ~ source activate py36
(py36) ➜ ~ echo $PATH
/Users/mm92400/anaconda3/envs/py36/bin:/Users/mm92400/bin:/usr/local/bin:/Users/mm92400/anaconda3/bin:/Users/mm92400/.cargo/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands
note how $PATH is different

Related

Not able to use conda python version in VSCode outside of workspace

I have problems to use the base conda python version when executing a python command outside of a script in a workspace in VSCode. This is despite the fact that I am specifying the python.pythonPath in the User Setting. The problem only happens in VSCode
First of all I am on a Mac with OS Big Sur (Version 11.6). I have miniconda installed in the following location:
/Users/username/opt/miniconda3
In .zshrc there is the conda initialization script
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/matthiaslenfers/opt/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/matthiaslenfers/opt/miniconda3/etc/profile.d/conda.sh" ]; then
. "/Users/matthiaslenfers/opt/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/matthiaslenfers/opt/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
When opening the terminal per default I am in the conda base environment.
(base) username#Matthiass-MBP ~ % which python
/Users/username/opt/miniconda3/bin/python
As you see outside of VScode in the terminal I don't have any problems using the conda base python version.
Inside of VSCode I opened a workspace and changed the settings to a specific conda
python version (and I also changed the general user setting)
User Setting
"python.pythonPath": "/Users/matthiaslenfers/opt/miniconda3/bin/python"
Workspace Setting
"python.pythonPath": "/Users/matthiaslenfers/opt/miniconda3/envs/example_project/bin/python"
When executing a line (import pandas as pd) from the file example.py in the workspace example_project (by highlighting it and then pressing ⇧ + Enter) I get the following result:
source activate example_project
/Users/username/opt/miniconda3/envs/example_project/bin/python
import pandas as pd
(base) username#Matthiass-MBP example_project % source activate example_project
(example_project) username#Matthiass-MBP example_project % /Users/username/opt/miniconda3/envs/example_project/bin/python
Python 3.7.11 (default, Jul 27 2021, 07:03:16)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
So the correct python version of my conda environment example_project is used automatically
When on the other hand I open a new terminal inside VSCode and check the python version and location I get the following result and the active python version is /usr/local/bin/python
(base) username#Matthiass-MBP example_project % which python
python --version
Python 2.7.15
(base) username#Matthiass-MBP example_project % which python
/usr/local/bin/python
Why is it not the Python version in /Users/username/opt/miniconda3/bin? It shows
(base) in the VSCode terminal, but another python version is executed
I am confused!
What's the value of the python interpreter you have selected. You can get it from the bottom-left on the VSCode.
After you selected the python interpreter, when you create a new terminal in the VSCode(shortcut of Ctrl+Shift+`), the python extension will help you to activate the environment automatically like this:
You can have a look at the official docs. It explained it clearly.

How do I initialize / switch to python3 from python2 in terminal?

I just created my first AWS EC2 instance. I used sudo yum install python3 -y to install Python3 but when I check the version via python --version it says Python 2.7.16. How do I switch versions?
You can either invoke python 3 with python3 directly from the terminal, or create an alias by adding the following line to your ~/.bashrc or ~/.bash_aliases file:
alias python=python3
More details and troubleshooting tips available in this related question.
There are a couple of ways to do this:
You could explicitly request python3, invoking it as-is, instead of just python, i.e.:
$ python3
Python 3.7.6 (default, Feb 26 2020, 20:54:15)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
As per #Nick Walsh's answer, you can create a shell alias(1) that just expands python to python3 by putting the following either into .profile, .bashrc, or even .bash_aliases:
alias python=python3
Granted python3 is in your PATH, this will work without a hitch, with the added benefit that this is a per user setting, meaning you won't be changing the system-wide python interpreter (since python remains pointing to /usr/bin/python2). If you'd like, you can opt for a system-wide alias as well by modifying /etc/profile or /etc/bashrc, adding the alias there.
You could replace the python symlink, linking it to python3 instead.
You can achieve this using ln(1) (pay close attention to the # vs. $ prompt, meaning you require root privileges to issue this command. Using sudo will suffice):
# ln -sf /usr/bin/python{3,}
I'm leveraging bash's string expansion features to avoid repetition. The command effectively expands to:
# ln -sf /usr/bin/python3 /usr/bin/python
This is probably recommended for the sake of portability (when it comes to scripting).
The latter alternative might work up until python gets updated, replacing the default python interpreter again with python2. #kichik pointed out the use of alternatives(8) to adequately (and truly persistently) configure your python interpreter.
As per this answer, you can issue the following commands to install and configure your default python interpreter:
# alternatives --install /usr/bin/python python /usr/bin/python2 50
# alternatives --install /usr/bin/python python /usr/bin/python3 60
# alternatives --config python
There are 2 programs which provide 'python'.
Selection Command
-----------------------------------------------
1 /usr/bin/python2
*+ 2 /usr/bin/python3
Enter to keep the current selection[+], or type selection number: 2
$ alternatives --display python
python - status is manual.
link currently points to /usr/bin/python3
/usr/bin/python2 - priority 50
/usr/bin/python3 - priority 60
Current `best' version is /usr/bin/python3.
$ python
Python 3.7.6 (default, Feb 26 2020, 20:54:15)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Setting Non-default-python-version in virtualenv [duplicate]

This question already has answers here:
Use different Python version with virtualenv
(41 answers)
Closed 6 years ago.
My default python version on my Debian 8.5 machine is 3.4.2. I want to use python 2.7 for only one project. I have tried running all of the following commands in the terminal one by one, :
virtualenv -p python2.7 env_dir
virtualenv -p python2 env_dir
virtualenv --python=python2.7 env_dir
And this is the file.py inside the env_dir:
print "Hello world from inside env_dir"
Obviously however the file.py still gets executed with python3.4.2, because i run:
python file.py
and i get:
File "file.py", line 1
print "Hello world from inside env_dir"
^
SyntaxError: Missing parentheses in call to 'print'
What is the problem, why it fails to run the code in python2.7?
Update
I have also tried:
virtualenv -p /usr/bin/python2.7 env_dir
source env_dir/bin/activate
(env_dir) amir#amir-debian:~/env_dir$ python file.py
File "file.py", line 1
print "Insid virtual env"
^
SyntaxError: Missing parentheses in call to 'print'
Running python -V after activating returns: Python 3.4.2
Update-2
Here is the process how i try to create my virtualenv and the outputs from terminal:
$ virtualenv --python=/usr/bin/python2.7 venv3
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/amir/Desktop/venv3/bin/python2.7
Also creating executable in /home/amir/Desktop/venv3/bin/python
Installing setuptools, pip, wheel...done.
$ source venv3/bin/activate
(venv3) amir#amir-debian:~/Desktop$ python -V
Python 3.4.2
(venv3) amir#amir-debian:~/Desktop$ python
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Why is the working version still 3.4.2 and not 2.7 as i gave as OPTION to vertualenv when creating venv3?
Check if you have python2 by doing python -v
if you do have it, you can do python2 file.py
There's also an answer from here
which is
virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>
You can specify the version of python to use by doing
$ virtualenv venv --python=/usr/bin/python2

Linux using wrong python version

I have a Linux drive with no root access. It has Python 2.4.2 installed in /usr/bin/python directory.
which python
/usr/bin/python
I installed Python 2.7.8 in my local folder:
cd /usr2/steve/bin
ls
python2.7
If I still run Python it starts older version:
python
Python 2.4.2 (#1, May 6 2011, 13:26:21)
[GCC 4.1.2 20070115 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I have added /usr2/steve/bin to the PATH:
echo $PATH
/usr2/steve/usr2/steve/bin:/usr2/steve/local/mnt/workspace/steve/Python-2.7.8:/usr2/steve/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
How do I force the system to use the newer Python version as opposed to the older one?
You only have a python2.7 binary in your /usr2/steve/bin folder. You can make a symlink, then just calling python should work:
cd /usr2/steve/bin
ln -s python2.7 python
make your life simple by creating an alias. Are you using BASH (hopefully you are)? If so this is the syntax
alias pythonsteve='/usr2/steve/bin/python2.7'
(drop the = sign if you are running CSH). Next, run to verify
$pythonsteve -c 'import sys; print sys.prefix'
/usr2/steve/bin
Put the alias command in your shell user start-up file. On typical BASH installs this is the file ~/.bashrc. Note, when you manually edit the ~/.bashrc file you must either log out/log in, or run source ~/.bashrc for the commands to execute.
If you want to use python 2.7 you can specify it by appending it after 'python'
which python2.7

Using Python with homebrew on OS X

I have been using Mac python for a while, and I decided to teach myself matplotlib, because I want to have the experience with some common modules. I hear from everyone that once you get into non-standard modules, it's best to use python threw homebrew, so you have access to pip and not easy_install. After running: $brew install python --with-brewed-openssl, $brew install python3 --with-brewed-openssl, and $pip install matplotlib, I go to the python shell with $python3. Once there, I run import matplotlib.pyplot as plt, and get the following:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'matplotlib'
>>>
I have a feeling I'm still using the mac python, and not the brewed python, and I have tried editing the path, but it isn't working, even though I am running $source ~/.bash_profile after every edit. Just in case I am editing the path wrong, I will post the file contents below:
PATH=/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
export MSF_DATABASE_CONFIG=/usr/local/share/metasploit-framework/database.yml
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
PATH=“/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:${PATH}”
export PATH
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
export PATH
export PATH=/usr/local/bin:/usr/local/sbin:${PATH}
$pip3 install matplotlib returns the following
RuntimeError: Broken toolchain: cannot link a simple C program
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /private/var/folders/sr/j0s763cj06v38c6btg6v7k7r0000gn/T/pip_build_ericmarkmartin/matplotlib
Storing debug log for failure in /Users/ericmarkmartin/.pip/pip.log
If more information is needed, please feel free to ask, and thank you so much in advance for the help!
pip usually installs modules for python2 if you have both python versions in your system, so for python3 you will probably need to use pip3.
For reference:
https://docs.python.org/3.4/installing/index.html
You can check where a command is coming from by using which python . In this case you are adding /opt/local/bin/ to your path, but then you are adding /Library/Frameworks/Python.framework/Versions/2.6/bin and /usr/local/bin/ before them (you are prepending in each case). $PATH stops on the first successful match, so you won't pick up the python from /opt/local/bin . Try moving that line to the end of your bashrc or adding another line prepending it to the front of $PATH.

Categories

Resources