I have pyenv installed in my environment and up to this weekend (when I installed 'Kivy') my pyenv/local setup has been working fine. But now when I go to my various python project directories, pyenv does not automatically activate the right python version properly.
E.g.
I create an environment using pyenv like this,
pyenv virtualenv 3.3.2 work
I make and go into a dir called work and have a .python-version file with the text work as the sole content.
Pyenv detects that my environment is work using this file but my python version is not python 3.3.2 instead it's 2.7.9.
For some reason, something happened, and all of my pyenv virtual environments use 2.7.9 as opposed to the python version they were created with.
When I run which python I get,
/opt/boxen/homebrew/bin/python
when I go to the pyenv version directory and run
$ cat pyvenv.cfg
home = /opt/boxen/pyenv/versions/3.3.2/bin
include-system-site-packages = false
version = 3.3.2
However, if I run pyenv activate I my python version switches to python 3.3.2 (or the appropriate version for a given env).
Question is, how do I get pyenv to auto activate the environment's python version as it did before (before I did something to break it).
It sounds like, because of which python not saying it's the shim, you don't have the bin/shims path first in your PATH envvar. Add these lines to your shell startup script, and make sure they're at the end, after any other path manipulations.
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
The eval line does some additional shell monkeying I think to add the .pyenv/shims directory...check that with an echo $PATH maybe.
Related
I use vevn with python 3.6.8, pip 18.1.
I set powershell executionPolicy RemoteSigned,
create virtual environment with venv, refed to venv official site : (https://docs.python.org/3.6/library/venv.html)
and also activated according to powershell, used .<venv>\Scripts\activate.ps1
But when I execute 'pip list' command, it shows all packages installed in global, not in venv.
And when execute python - import sys - sys.prefix, to check python directory,
I can find, powershell venv uses environmental variables python.
Of course, run it on cmd in same way, it works correctly.
pip list shows
The pip list shows only packages installed in venv, and sys.prefix is python in venv directory.
Does anyone know why it works like this?
Powershell works correctly like cmd
Maybe you activated the "--system-site-packages" switch by accident when installing the virtual env. This switch makes all python packages in the global system available to the virtual env.
Also check that after activating the environment your powershell indicates that you are actually using the virtual env. This is indicated by (env_name) at the beginning of the new power shell line.
So here is my story:
As we know macos comes with pyhton 2.7 preinstalled. Years ago, I installed python 3.7.0 and all going good. I started working with Tensorflow and during that time tensorflow wasn't compatible with 3.7.0 so switch the version to 3.6.6 somehow :). After some time i installed anaconda and it installed 3.7.3.
At this point:
When i open my python IDLE shell, it runs on 3.6.6
anaconda Jupiter notebook shows 3.7.3.
Then i learned to create virtual environments through terminal and i use following code to create virtual environment:
pip install virtualenv
virtualenv project_env
And after i activate the virtual environment the python version it shows is 3.7.3.
Then recently i changed the terminal shell to zsh and now everything is kind of messed up. I'm
Terminal can't find conda commands. How to fix that?
Now i can't create virtual environment through virtualenv project_env. It says command not found. Why? It use to create virtual environments with python 3.7.3.
Now to create virtual environment i use python3 -m venv project_env this creates the virtual environment with python 3.6.6. If i use python3.7 -m venv project_env it creates environment with python 3.7.0.
I can't find a way to create virtual environments with python 3.7.3.
I need help with above troubles caused after installing zsh and help with accessing python versions wherever i need. How do i update python version 3.7.0 to latest or any specific version like 3.7.3. Hows python 3.8.4 to use, i mean like i had trouble importing tensorflow in python 3.7.0.
I hope you will understand my trouble and searched a lot for solutions. I'm so confused right now, couldn't even figure out the title of my problem.
Based on the fact your shell can't find conda now, you should look at your "path" environment variable. Whenever you change from one shell to another, you need to look at and possible create or modify the files the shell uses upon startup. These files setup your environment (PATH, PYTHONPATH, etc), create aliases, etc. You may need to create the .zshrc, .zprofile, .zlogin etc files and add the same settings as your previous shell's startup files.
Example:
If you used the bash shell previously you would need to modify the .zshrc file to include your settings in the .bashrc file. Syntax may be different, so a straight copy may or may not work.
Try this as a quick temporary fix (may or may not work, depending on your current ~/.profile settings:
Create a ~/.zprofile file with this code inside:
emulate sh
. ~/.profile
emulate zsh
references:
https://superuser.com/questions/187639/zsh-not-hitting-profile
http://zsh.sourceforge.net/Intro/intro_3.html
i installed pyenv and switched to python 3.6.9 (using pyenv global 3.6.9). How do i go back to my system python? Running pyenv global system didnt work
Your system Python might be /usr/bin/python or /usr/bin/python3. You have a couple options:
Execute that Python interpreter directly:
/usr/bin/python --version
If you want to run it from a script and you're on a *nix machine, put
#!/usr/bin/python
at the top of the file, then give it execute permissions (chmod +x my-script.py) and run it directly: ./my-script.py.
Turn off pyenv's path hacks. This could mean removing the eval "$(pyenv init -)" from your ~/.bashrc or ~/.bash_profile and loading a new shell.
Use the pyenv register plugin - https://github.com/doloopwhile/pyenv-register (or use/build something similar). Here's a portion of the README
Installation:
git clone https://github.com/doloopwhile/pyenv-register.git $(pyenv root)/plugins/pyenv-register # clone plugin
exec "$SHELL" # reload shell
Usage:
pyenv register /usr/bin/python
pyenv versions
You can "unset" the local pyenv version for your project using the following command:
pyenv local --unset
pyenv sets the python used according to ~/.pyenv/version. For a temporary fix, you can write system in it. Afterwards, you'll need to fiddle through your ~/.*rc files and make sure eval "$(pyenv init -)" is called after any changes to PATH made by other programs (such as zsh).
Using Python 3.7.0 on Mac. Trying to use venv module that was added post python 3.4.
I setup my virtual env using python3 venv -m path/to/my/dir - my question is do I need to activate this virtual env to use?
The documentation seem to imply I don't need to?
You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically.
If I don't have to activate, what is the benefit of prepending venv to binary directory? Wouldn't this have to happen regardless for a venv to work?
Activating the virtualenv gives you convenience. It is never required.
Even for scripts that are configured to run with #!/usr/bin/env python, (which looks up the python executable on your path), you could manually update the PATH environment variable:
$ PATH="/path/to/venv/bin" some_script
Activating makes the PATH update stick until you deactivate again, and that can be convenient.
For example, I regularly have several virtualenvs in use at any one time. Some of them are there only to install some command-line tools I have symlinked into my ~/bin/ directory, another is home to a Jupyter notebook, and 2 more are used to quickly verify code snippets in different Python versions with access to 3rd-party libraries. I don't activate any of those.
When you don’t activate a virtualenv, all that happens is that your terminal PATH variable is not updated to put the bin directory of the virtualenv first, so when you enter python or pip or other script without any path into the terminal, the shell will find a different Python binary to run. You can always use any of the commands in the virtualenv bin/ directory by giving the full path to that command.
I installed miniconda for some software I need to run. It worked great, but it made all of the other web related stuff I have set up through mac's default python environment stop working. What I would like to have is the mac python environment as the default and conda only when I need to run this specific software. So I would need to know #1 - how do I modify .bash_profile to allow me to run both environments, and #2 - the command I need to switch between environments (if there is one).
My bash profile looks like:
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
# added by Miniconda3 4.3.21 installer
# export PATH="/Users/mikeheavers/miniconda3/bin:$PATH"
(I have commented out the conda path for now)
Thanks!
Have you considered using Python's Virtual env?
This allows you to have a completely separate Python installations without causing conflicts with your main python in your path. This sounds ideal for your development needs.
You would need to "activate" the virtualenv prior to starting up miniconda, which will adjust your environmental variables such that the virtualenv python, and it's libraries will be used.
(copying from the link)
This will result in a virtual python installation
$ pip install virtualenv
$ cd my_project_folder
$ virtualenv my_project
$ source my_project/bin/activate
$ # Do stuff, like install from pip
$ deactivate # This will turn off the virtual python in your path
you can use this interpreter in your bashrc too (Check out the link for a more in depth introduction)
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7