Cannot execute virtualenv commands when using Python 2.7 - python

I am trying to set up a virtual environment for a Django project, using Python 2.7. Both Python 2.7 and 3.6 are installed on my Mac.
I run the command, specifying the path to Python 2.7 virtualenv --python=/usr/bin/python2.7 venv and everything seems to work fine. However, when I run venv source/bin/activate it tells me venv: command not found.
Something is broken. I have read similar answers on Stack Overflow but nothing seems to be working. I have installed and uninstalled virtualenv with pip, tried running with superuser etc.

I guess you need source venv/bin/activate. source is a command, venv is the directory your virtualenv is located in.

Related

Python versions are not changing despite activating virtual environment in WSL2

Background:
In WSL2 (ubuntu 20.04) I created a python virtual environment inside a directory. Using the command python3 -m venv venv my system's python version was set to python3.11 (after downloading) via sudo update-alternatives --config python3 and then choosing the version. I noticed I was having some errors of missing modules when I started WSL2 (happening after a computer restart), I read this was because I was using a different python version than the one ubuntu 20.04 came with so I switched back to 3.8 via the config menu as before. I am also using VS code that's connected to my WSL2.
These are some of the contents of my venv directory: venv/bin/python venv/bin/python3 venv/bin/python3.11 venv/bin/pip venv/bin/pip3
Question:
After activating my virutal env via source venv/bin/activate, when I do python3 --version I still get a version of 3.8.10 despite creating the virtual environment with 3.11. I was able to get the interpretor set to 3.11 on VS code.I know I was in the virtual environment since my command prompt had (venv) in front. I went into the python console while in the virtual env and did import sys and sys.path this was my output ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload']. Why isn't the python version changing, am I misunderstanding something or did I not do something correctly? Seems like pip isn't working either but works when I switch my system python to 3.11 (I tried installing it on 3.8 but it said it was already installed).
Solved:
Answered below, just re-created the virtual env while making sure my system python version was 3.11 (may have been some mixup earlier).
By changing the selection in sudo update-alternatives --config python3 you change the selected python version also for the chosen vitrual environment (at least when using venv, it might be different with other tools).
That can cause issues, because when creating a new virtual environment envname using venv from a specific python version xx.xx, a directory named pythonxx.xx is created in /envname/lib/, and inside it a directory named site-packages that contains the packages installed by the pip of this specific environment.
So changing back to the original python version of the environment through sudo update-alternatives --config python3 should solve the issue, and probably the errors of missing modules are due to the incompatibility of the current selected python version with the original version which you installed the virtual environment from.
Personally, to avoid confusing, I name my virtual environments with the python version as a suffix, e.g envname_py3.11.1. But there might be a better method which I am not aware of.
I deleted my venv directory and recreated my virtual environment while on python3.11. This has resolved my issue.

Django virtual env issues with Python and Atom

I've been all over the site trying to find a solution for a problem i am running into with my virtual environment and the python version being used.
I have installed mini conda
I am on a Mac
I have run the conda create myDjangoEnv python=3.6 conda commmand, and i have activated the env with the conda activate myDjangoEnv command.
I have confirmed that django is installed in the venv and the version is 3.8.4
The problem I am running into is when trying to execute the command python manage.py runserver, I first get the from exc error. When checking python it says it is running 2.7 version in the atom terminal, however when i check my virenv in my normal terminal the python version returns with 3.6.
In the atom terminal, when i execute python3 manage.py runserver, the from exc error goes away but then I run into the ImportError: Django package cannot be found.
Has anyone run into this issue specifically with the Atom server? Is there something I am doing wrong when creating the projects in atom that is tripping the virtual environment and resetting the python version to 2.7?
Are you using the package "script" in Atom?
That issue may be related to you having multiple version of python installed and your terminal is pointing at the wrong one there. I recommend you switch to something like vscode, where you can ctrl shift P and select manually the venv and python interpreter used for the workspace and have your terminal there.

problems creating virtualenv when installing two different versions of Python

I have installed Python 2.7, pip and virtualenv on my computer and I have created a virtual environment with this same version of Python. I installed some packages in this new virtual environment too.
Later, I decided to install Python 3.6 on the same computer to create a virtual environment that works with version 3.6. I downloaded Python 3.6 and installed it by selecting Add Python 3.6 to PATH and Install for All users and I selected the option to automatically install pip. As I had previously installed virtualenv, I have not had to reinstall it. Should I?
The problem is that, once installed, when creating a virtual environment I've got an error.
As I already had a version of Python installed I have specified that I want to use Python 3.6 in the virtual environment that I am creating:
virtualenv env2_py36 -p c: \ Python36 \ python.exe
The error already says that this usually happens when you install more than one version of Python on the same computer and warns that it is necessary to check the option Install for All Users to work (as I did) and also recommends placing the appropiate PythonXX.dll file in the Scripts folder of the virtual environment. However, I see that there is already a python36.dll file there.
Could anybody tell me what should I do to fix this problem?
On the other hand, I had previously created another virtual environment using Python 2.7. I use PyScripter to run my scripts but I don't know how to connect PyScripter to my virtual environment. It's still connected to the Python2.7's system installation.
This works for me:
virtualenv --python=python3.6 yourenvname
(3.6 is the current latest version of Python, but you can use whichever version you'd like.)

Issues setting up Virtualenv and Virtualenvwrapper

I'm trying to install virtualenv and virtualenvwrapper so that I can do some django work.
I'm not exactly sure where the issue is stemming from. I currently have installed Jupyter Notebook and installed a lot of python files though it (python 3+ I believe), so when I did pip install virtualevnwrapper, the location of the shell file was in C:/Users/'Andy Renz'/Anaconda3/Scripts/virtualenvwrapper.sh. I account for this in changing by .bashrc file, by including:
export WORKON_HOME=$HOME/.virtualenvs
source C:/Users/'Andy Renz'/Anaconda3/Scripts/virtualenvwrapper.sh
When I run source ~/.bashrc in my shell, I get the following:
bash: /usr/bin/python: No such file or directory
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could no import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and the PATH is
set properly.
I think this means python isn't where it is supposed to be. Virtualenv references python 2+ I believe which leads me to believe my python 2 is somewhere odd. I do have it, not downloaded from Jupyter. How do I account for this and proceed forward?
The issue is you're trying to install the default virtualenvwrapper (for Linux) on a Windows machine. That's why it's trying to get Python from /usr/bin/python, a directory that does not exist in Windows.
Try virtualenvwrapper-win from https://pypi.python.org/pypi/virtualenvwrapper-win
Python is somehow either not installed or installed at different path maybe /usr/local/bin. Use which python or whereis python to check if python is indeed installed and path of installation. Then you can create a softlink to the python executable in your /usr/bin directory.

Does virtualenv isolate python itself + python packages, or just python packages?

I just installed virtualenv onto my ubuntu 12.10 laptop. Am I supposed to install python itself into this vritualenv as well as all future python packages into this virtualenv?
Or is python not included in virtualenv typically?
when I type in 'which python' at the bash command line, I get /usr/bin/python, and it's python 2.7.3. Should I install, say python 2.7.5 inside virtualenv?
Thanks!
Virtualenv installs python, but it's installed in the bin directory of the virtualenv you created. Therefore you need to run it with ./bin/python.
You can also "activate" the virtualenv by running
source bin/activate
Which will put the virtualenvs bin directory first in the path (and do some other trickery I think) which will make the virtualenvs Python the default Python, so you can start it with just python. But this is not necessary.

Categories