Cannot use virtual environment created within Pycharm from outside of the IDE - python

I am running Pycharm 2019.3 on Linux Ubuntu 18.04 LTS. I have created a virtual environment for my Pycharm project from within the IDE. I am able to access the packages from the virtual environment when I run programs from within the IDE. However, if I activate the virtual environment created from within Pycharm from the terminal (using source venv/bin/activate, resulting in the (venv) prefix on the command line), the packages I added to the virtual environment can no longer be found. Why is this? The (venv) prefix makes me think that I have successfully activated the virtual environment. What can I do to use this virtual environment outside of Pycharm?

I think this is because pycharm has created a virtual environment in a different location. I think you can check both the locations with echo $VIRTUAL_ENV and check if they are the same.

Related

Virtual Environment in Terminal PyCharm not Working

Virtual Environment in Terminal PyCharm not Working. If run through PyCharm it works, but the terminal uses the base interpreter
I removed the virtual environment and reinstalled

Multiple activation of environment?

I made virtual environment by using venv module and activated it.
Before I activated it, (base) D:\Python\venv was shown in terminal. Since I only installed Anaconda and VSCode, I guess this (base) environment was from Anaconda3.
After I activated the virtual environment, it was added in front of (base) interpreter like this : (venv) (base) D:\Python\venv. Does this mean both environments are in activation? For check, when I typed pip list, it only showed a list of (venv) environment. Then why does (base) environment show? Is it okay not to deactivate (base) when I use (venv)? I'm confused.
Yes, you've created multiple virtual environments.
When you start out in the "virtual environments world"it can be quite confusing.
In fact, you can create virtual environments in various ways.
To create a virtual environment, you can use for example :
conda
venv
virtualenv
What you've done: you created a virtual environment inside another virtual environment.
First, you created a virtual environment with "venv", and then, inside it, you created a virtual environment with "conda".
In fact, "base" is the default virtual environment of conda:
when you install Anaconda (or miniconda), your python installation is always, by default, in a virtual environement, called "base".
In other words, when you install Anaconda (or miniconda), by default, a virtual environment, called "base", is created.
In my opinion, using a conda virtual environment is enough, no need of "venv".
I would say even more, using a conda virtual environment inside a "venv" virtual environment is too much.
You should uninstall your "env" virtual environment, and after that, just install Anaconda (or miniconda).
You can create other virtual environments than "base" with conda.
Check https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html for more details.

Do I have to place my Python project inside the conda environment directory?

I am learning Python and downloaded it with Anaconda in Ubuntu (learning Linux too). When I create a conda environment, it generates a directory with the name of the env like this: /home/user/anaconda3/envs/myenv1
When I activate the environment and launch my editor (VScode) I then select the interpreter from the active environment. When I save my .py file, do I have to save it inside the environment directory? That folder contains other folders and packages that are put there by conda. If I need a package from that environment and my project folder is not in the myenv1 directory, will it not work?
Until and unless you are inside the environment i.e. environment is activated, you'll have access to all packages installed in the conda environment. Run the python file inside the environment. There is no requirement of placing your project inside the environment.
Go to your project directory then open terminal or open terminal in VSCode then put this command
conda activate myenv1
If the environment is activated, it will be shown in left part of your bash prompt like
(myenv1) username: /path/to/project $
Then,
python my_script.py
No, you don't have to save any file in the virtual environment folder. Once the environment is activated, it acts like an independent environment. But remember you will have to install all the packages again which are not available in Conda.

Loading Python Virtualenv files created using PyCharm using command line

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

How to setup Django PyDev project with virtualenv created with pyenv

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.

Categories