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.
Related
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.
I somehow changed the virtual environment I was using with a Django project when making changes to Pycharm settings. Now my project is using Python 3.8 when it had been using 3.9 earlier and an app I had installed via pip is no longer recognized.
I get the following warning when entering the shell in the terminal:
Warning: Your Pipfile requires python_version 3.9,
but you are using 3.8.2 (/Users/w/.local/share/v/o/bin/python).
$ pipenv --rm and rebuilding the virtual environment may resolve the issue.
$ pipenv check will surely fail.
Is there a way to recover the previous virtual environment?
As long as you have not deleted the environment, it should still be saved and ready to be selected from the python interpreter selector.
Press Ctrl+Alt+S to open the project Settings/Preferences and go to Project | Python Interpreter. The previously added (virtual) environments should be there.
My virtual environment refuses to recognize my install of Django (strangely)
I'm on Windows Server, installed Python 3.7 to a directory (C:\Python37) which I have C:\Python37;C:\Python37\Scripts in my windows Path so when using Powershell or GitBash I can use the python command.
if I run where python it shows the default install
I CD into my django project directory and run:
python virtualenv venv and the venv directory is created
Then I run source venv/Scripts/activate and it activates appropriately.
When I run where python it shows the exe inside the venv directory - which is expected and appropriate.
I run pip install -r requirements.txt and all my requirements install appropriately. I confirm they are installed with pip freeze (all installed correctly)
Once I do that I go to run python manage.py collectstatic (no migrations are required in this particular instance) I get an error message that Django isn't installed.
To check this, with my virtualenv still activated I enter the shell (python)
If I do import django it also says Django is not installed.
I cannot figure out what's happening here - the python version appears to be correct, the correct virtualenv is activated - but it's still not seeing the properly installed Django installation.
Thoughts? Ideas?
For what it's worth - here is the solution and explanation:
Everything works as expected as was outlined in the question - the reason that the actual python command wasn't working had to do with a line in the .bashrc file.
There was an alias in the .bashrc file to set python to the command winpty C:\Python37\python
So when the command python manage.py collectstatic was getting ran - it was looking at the Python executable in the Python37 directory and not the virtualenvironment Python.
This was solved by simply running the appropriate Python (e.g.)
C:/my_project/venv/Scripts/python manage.py collectstatic
This forced it to use the virtualenvironment python to run the command so everything worked as expected.
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.
I created a new project in PyCharm and I made an environment file for the project. I want to use Jupyter Notebook with that project now and since Jupyter in PyCharm is not as great, I want to launch it from cmd. I am not able to figure out to activate the environment created in PyCharm from the command line. Can anyone help me with that?
find the location of environment from PyCharm's project's interpreter settings
From the terminal Run:
source /path/to/env-activate
Solution For Linux
try to use pyenv, once you install virtualenv with pyenv for example
cd ~/your_project_root_folder
pyenv install 3.6.1
pyenv virtualenv 3.6.1 your_venv_name
pyenv local your_venv_name
Then whatever shell command is launched (even from pyenv) you will have correct virtualenv and you don't need to change anything inside pycharm
PyENV
https://github.com/pyenv/pyenv-installer