Can a virtualenv created on Windows be used from WSL bash? - python

I'm using Windows Subsystem for Linux (WSL) for a test project (obeythetestinggoat) and had a ton of issues trying to get virtualenv to work from the shell or CMD to ensure I was using the latest Python version installed (3.6).
mkvirtualenv --python="C:\Program Files\Python36\python.exe" superlists
I can get both bash and CMD running the virtualenv, but both report different versions of Python.
CMD:
python -V
Python 3.6.0
Bash:
python -V
Python 2.7.6
python3 -V
Python 3.4.3
Since the venv was created with the CMD shell, I would think it would control the version used, and the Scripts folder shows python36.dll, so assume it should be 3.6.
So it seems like the Bash WSL environment is controlling what version is used, regardless of any ENV paths I set. I think I might ditch the Windows approach and go use my Ubuntu install, but really want to understand what is happening here, especially in regards to the virtualenv as it seems to negate the purpose of it all if the shell is only using what came with it and will not use the version supposedly in the virtual env. Any help would be appreciated!

Related

zsh: commands not working for pip, conda, python

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

Simulate python3 shell commands behavior in windows powershell

I work on an api that run python3 scripts like python3 <scriptname.py>. This api initially run on a linux system. Unfortunately I make the local development of it on a windows. Is there a way to simulate this behaviour so that when I run in powershell python3 -V it give me the version of python.
I have allready python 3 installed on my computer. I tried to add the path to my python.exe to the system variables with for variable the string python3. But after registering and restarting powershell and type python3 -V I still get the error that «python3» is not recognized as a command applet name.
Does anyone have any ideas?
Once python is in your path, you can define a PowerShell alias for python3.
PS > New-Alias python3 python
PS > python3 -V
python 3.6.5
In order to have this alias as a permanent one, refer to this question: Permanent PowerShell Aliases.
I found tricky solution.
Once python is in your path, go to python installed path and copy python.exe and named duplicated one as python3.exe

Installed Python 3.7 and tried changing that to the default however, changing $PATH and running python --version still shows 2.7

Sorry if the question is simple or I'm missing something obvious but I'm fairly new with console commands and Python.
As the title suggests, I installed Homebrew and subsequently the latest version of Python. I want to make this the default when running python --version in bash.
I tried changing the path in my bash_profile with the following line at the bottom:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
It seems that when I install new modules however, that they are for 2.7 and not 3. Checking pip --version going straight to the default:
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
I also tried running the following commands:
echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
this does edit my bash_profile again, but the line I wanted (and added manually) isn't the one that appears; instead the following is inserted (I have no idea why Applications/VMware is inserted at the end, it has nothing to do with Python or what I'm trying to do):
export PATH=/usr/local/bin:/usr/local/sbin:$PATHexport Path=/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware
Can anyone explain to me how I can make python3 the default. I don't want to get rid of 2.7 obviously because a lot of scripts for mac still depend on it. Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
You really don't want to change this.
First:
Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
Yes! Install pipenv, and set up an environment where 3.7 is the default, but leave the system default at 2.7.
More generally:
According to PEP 394, which isn't due to be reevaluated until 2020, if you don't have a virtual environment activated:
python3 runs Python 3.x
python2 runs Python 2.x
python usually shouldn't be used—but if you do use it, it should run Python 2.x.
Usually, you'll also have, e.g., python3.7 and python2.7 to run specific 3.x and 2.x versions, although this isn't covered by any standard.
Scripts like pip, and those installed by pip, should be installed with similar suffixes—but ideally, if you have multiple versions, you shouldn't run those scripts. Most of them are designed to be run as modules, so you can run python3 -m pip to run the pip for your 3.x.
For a few things, it may be handy to set up shortcuts. (For example, instead of running python3 -m ipython from the command line, I usually use an iTerm profile that uses that instead of bash as my start command. If you don't use either IPython or iTerm, you can make a Terminal.app profile that runs python3.)
Scripts installed as part of your platform's package manager are a whole more complicated story, but macOS doesn't have an official package manager, and Homebrew generally expects you to install Python libraries with pip, not brew, so you don't have to worry about that.
Programs should have an installer—whether they install via pip or something like py2app—that creates a proper shbang line, so they run with whichever Python version they were installed with, so you don't have to worry about them. But programs that come with your OS may use something like /usr/bin/env python, and they will expect that to run the version of Python that came with your OS.
For development purposes, you usually want to use virtualenv or pipenv or conda so that python runs the Python interpreter for whichever environment is currently activated.
So, don't try to change the fact that python runs Apple's Python 2.7. That's what it's supposed to do, and if you make it do otherwise, you could break things. Use python3, python3 -m pip, etc.; use #!/usr/bin/env python3 on your scripts (or, better, use setuptools to create entry-point scripts automatically); use virtual environments; just never run python, and everything will work properly.
You just need to run command
$which python
And then goto the path rename python to python2
Then run command
$which python3
To get the path of python3
Just link with command
sudo ln -s $python3path /usr/bin
/python
You need to instead $python3path with real path
Then you can run python3 with python command

How install and use another version python(python 2.7) on linux with the default python version is python 2.6

There is a default python version, namely python 2.6, on the GPU server with Linux OS. Now I want to install a new python version on the server from its source, namely python 2.7. I should not change the default python version since I am not the administrator and some reason. So what should I do?
You can install your new version of Python. It should be accessible with the python27 command (which may be a symbolic link).
Then you will just have to check that the python symbolic link still points to python26.
Doing this, python will keep on execute Python 2.6 while python27 will execute Python 2.7
You can use virtualenv, to execute your programm in an environment with python 2.7.
Install virtualenv and virtualenvwrapper (for comfotable use.)
mkvirtualenv -p <your-python-version> would then start a virtual environment where the desired python version is the default.
To build on Tryph's answer, you can install that new version to your home directory, then in a directory specified within your PATH (like in .bash_profile), you can point to that directory and within there create a sym-link that points to the new python.
For instance, if you have a bin folder in your home directory that is specified in the path
ln -s /bin/python ~/bin/python

Specify Python version in virtualenv

I am attempting to create a virtual environment that uses Python 2.7.11 (rather than Ubuntu's 2.7.6). I can successfully create the virtual environment using the following code:
virtualenv -p /home/stmist/opt/Python-2.7.11/bin/python2.7 ~/test
However, running the following code:
source ~/test/bin/activate
python -V
indicates that the virtual environment is running 2.7.6. Any suggestions?
Update:
When inside the virtual environment, "pip" modifies packages for 2.7.11, not 2.7.6. I can run Python 2.7.11 using /home/stmist/opt/Python-2.7.11/bin/python2.7. So, the virtual environment is usable, but not ideal.
After restarting my computer, the virtual environment is now using Python 2.7.11. I don't know whether this is due to the restart, or something else along the way. Thanks to everyone for your help.

Categories