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.
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'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.
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.)
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
I have set up a python virtual environment with pyenv on Linux Now I would like to create a Django project in PyDev with one of these virtual environments. However, I cannot figure out how to locate my virtual environment, since running which python in the virtual environment only gives me the generic /home/rbu/.pyenv/shims/python.
First find your virtualenv directory with
pyenv prefix <venv-name>
The python executable of the virtualenv should be <path>/<to>/<venv>/bin/python.
Now set up a new interpreter in Eclipse Preferences>PyDev>Interpreters>Python Interpreter using the location of the executable and a adequate name.
After that you can start a new Django Project via File>New>Project>Pydev>PyDev Django Project. Choose your predefined interpreter.
The Django Project should now work inside the virtualenv. For installing new packages it is probably easiest to just activate the virtualenv in the terminal with
pyenv activate <venv-name> and pip install the package.