Specify Python version in virtualenv - python

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.

Related

PyCharm doesn't recognize packages installed via Terminal when environment is used in PyCharm in Pop_OS Linux

I'm using Pop_OS! Linux. Whenever I make a Python Virtual Environment using mkvirtualenv and then pip install packages into it, that environment does not work when I reference it inside pycharm. I set up virtual environments using virtualenv and virtualenvwrapper.
The workaround I've found is to build any virtual environment you need to use inside of PyCharm inside of pycharm, or using the terminal inside of PyCharm. I'm annoyed it doesn't seem compatible with my native terminal, and can't figure out why. I even get slightly different python versions by running python --version both inside the PyCharm terminal and in the native linux terminal, while inside the same virtual environment! It's like Pycharm isn't reading the same information out of the python environment directory as my terminal is.
What is going on here, and how do I harmonize PyCharm with the rest of my linux machine? I'm also new to Pop_OS! Trying it out for the first time is an adventure.
IDK if that will work, but maybe if you install your packages via the "Python packages" tab in PyCharm (down left -> add package next to the research bar that appeared), PyCharm will see them while it installed them itself.

Self-contained Python environment for Mac OS

I need to make a fully self-contained Python environment for Mac, which can be transferred between different Macs running OSX and function independently of any other Python installations (or lack thereof).
In the enviroments I have created (using venv), I find that the /bin/python file and others are in fact aliases. When I replace these aliases with the original executables, the enviroment still does not work on other computers due references to files within a Python installation, such as
Library not loaded: /Library/Frameworks/Python.framework/Versions/3.9/Python
Is there a way to make the environment fully self-contained?
I would guess so, since tools such as pyinstaller and cx_freeze exist to make executables out of Python applications. So these must contain standalone environments somehow. But for my purposes, it is the enviroment that I need. Not an executable as these tools provide.
You can try using a virtual environment. Python comes with venv which allows you to create virtual environments. You could consider them self-contained, since all the necessary scripts for running Python are in the virtual environment. This goes with the package dependencies as well. You can have a variety of packages installed on your computer, but the virtual environment will have none of them installed (unless, of course, you install them yourself).
To create a virtual environment, run:
$ py -3.9 -m venv virtualenv
You can replace 3.9 with whichever Python version you are using and you can replace virtualenv with whatever you want to name your virtual environment.
To activate the virtual environment, you would run:
$ source virtualenv/Scripts/activate
And to deactivate it, you would run:
$ deactivate

VS Code Using Python 2.7.10 instead of Python 3.7.6 after activating virtual environment

I have had trouble for months using packages and libraries in VS Code. Everytime I create and activate a new python virtual environment in VS Code, it defaults back to using the MacOS pre-installed python 2.7.10. A picture below shows my issue. I would like to see "/usr/local/bin/python3" when I type the command "python --version", instead I get "/usr/bin/python". Does anyone know how to fix this? Thanks.
Picture of problem
I'm not a MacOS user and can't see your picture due to my network, but you can create the virtual environment with a specific version of Python.
On Windows this is:
python3.8 -m venv envname
If you are creating a virtual environment by using python -m venv envname the issue is the default interpreter on your PC is set to Python 2. A quick search shows you might be able to set a different global version on a Mac with something like pyenv global 3.8.6. I'm not sure if this would have other implications for you.
Please try to open a new VS Code terminal to let it reload the currently selected python environment. (Terminal; New Terminal; shortcut keys: Ctrl+Shift+`)
For the global python environment in VS Code, even though we have switched the python environment, sometimes its internal terminal still uses the first python of the python environment variable. Therefore, it is recommended that you move the python you need to use to the first: (Please reopen VS Code after setting.)

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

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

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!

Categories