Activating virtual environment in Pycharm python console
已回答
I am trying to use an existing virtual environment in PyCharm: I picked the python in the virtual environment as the project python interpreter, but in the console and terminal the active environment is still the base environment.
How can I activate the same environment in the console and terminal?
Thank you!
请先登录再写评论。
Hi Elena, PyCharm should activate the virtualenv from Settings | Project ... | Project Interpreter in the terminal. In the console the interpreter is usually the same, but you can change it in Settings | Build, Execution, Deployment | Console | Python Console. If this is not the case please provide a screenshot of the project interpreter window and specify how did you check if the same one was used in the terminal/console.
Thank you for your message!
Thank you. I think the problem is caused by PY-23417, PyCharm doesn't activate conda venv correctly.
Thanks!
I see.. Anything I could do to circumvent this?
Thanks!!
Elena
The only workaround I have in mind is to manually reactivate the venv:
This I guess will work in the terminal, but would it also work in the console?
Thank you!
Could you please start the console, execute the following code and provide the output here?
```
```
Here is the output:
{'os.environ': {'BASH_FUNC_module%%': '() { eval `/usr/bin/modulecmd bash $*`\n}',
'CLUTTER_IM_MODULE': 'xim',
'COMPIZ_BIN_PATH': ['/usr/bin/'],
'COMPIZ_CONFIG_PROFILE': 'ubuntu',
'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-EUns5WUSji',
'DEFAULTS_PATH': ['/usr/share/gconf/ubuntu.default.path'],
'DESKTOP_SESSION': 'ubuntu',
'DISPLAY': ':0',
'GDMSESSION': 'ubuntu',
'GDM_LANG': 'en_GB',
'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated',
'GNOME_KEYRING_CONTROL': '',
'GNOME_KEYRING_PID': '',
'GPG_AGENT_INFO': '/nfs/nhome/live/elena/.gnupg/S.gpg-agent:0:1',
'GTK2_MODULES': 'overlay-scrollbar',
'GTK_IM_MODULE': 'ibus',
'GTK_MODULES': 'gail:atk-bridge:unity-gtk-module',
'HOME': '/nfs/nhome/live/elena',
'IM_CONFIG_PHASE': '1',
'INSTANCE': '',
'IPYTHONENABLE': 'True',
'JOB': 'unity-settings-daemon',
'LANG': 'en_GB.UTF-8',
'LANGUAGE': 'en_GB:en',
'LD_LIBRARY_PATH': ['.', ''],
'LOADEDMODULES': '',
'LOGNAME': 'elena',
'MANDATORY_PATH': ['/usr/share/gconf/ubuntu.mandatory.path'],
'MODULEPATH': ['/etc/environment-modules/modules',
'/usr/share/modules/versions',
'/usr/Modules/$MODULE_VERSION/modulefiles',
'/usr/share/modules/modulefiles',
'/opt/Modules/modulefiles'],
'MODULESHOME': '/usr/share/modules',
'MODULE_VERSION': '3.2.10',
'MODULE_VERSION_STACK': '3.2.10',
'NLSPATH': ['/usr/dt/lib/nls/msg/%L/%N.cat'],
'PATH': ['/nfs/nhome/live/elena/anaconda/bin',
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
'/usr/games',
'/usr/local/games',
'/snap/bin'],
'PWD': '/nfs/nhome/live/elena/pycharm-community-2016.2.3/bin',
'PYCHARM_HOSTED': '1',
'PYTHONDONTWRITEBYTECODE': '1',
'PYTHONIOENCODING': 'UTF-8',
'PYTHONPATH': ['/nfs/nhome/live/elena/attrib_experiment_analysis/preliminary_analysis',
'/nfs/nhome/live/elena/pycharm-community-2016.2.3/helpers/pydev'],
'PYTHONUNBUFFERED': '1',
'QT4_IM_MODULE': 'xim',
'QT_ACCESSIBILITY': '1',
'QT_IM_MODULE': 'ibus',
'QT_LINUX_ACCESSIBILITY_ALWAYS_ON': '1',
'QT_QPA_PLATFORMTHEME': 'appmenu-qt5',
'SESSION': 'ubuntu',
'SESSIONTYPE': 'gnome-session',
'SESSION_MANAGER': 'local/u236d:@/tmp/.ICE-unix/10081,unix/u236d:/tmp/.ICE-unix/10081',
'SHELL': '/bin/bash',
'SHLVL': '2',
'SSH_AUTH_SOCK': '/run/user/5302/keyring/ssh',
'TERM': 'emacs',
'UPSTART_EVENTS': 'xsession started',
'UPSTART_INSTANCE': '',
'UPSTART_JOB': 'unity7',
'UPSTART_SESSION': 'unix:abstract=/com/ubuntu/upstart-session/5302/9848',
'USER': 'elena',
'VTE_VERSION': '4205',
'WINDOWID': '48234506',
'XAUTHORITY': '/nfs/nhome/live/elena/.Xauthority',
'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg',
'XDG_CURRENT_DESKTOP': 'Unity',
'XDG_DATA_DIRS': '/usr/share/ubuntu:/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop:/var/lib/snapd/desktop',
'XDG_GREETER_DATA_DIR': '/var/lib/lightdm-data/elena',
'XDG_MENU_PREFIX': 'gnome-',
'XDG_RUNTIME_DIR': '/run/user/5302',
'XDG_SEAT': 'seat0',
'XDG_SEAT_PATH': ['/org/freedesktop/DisplayManager/Seat0'],
'XDG_SESSION_DESKTOP': 'ubuntu',
'XDG_SESSION_ID': 'c2',
'XDG_SESSION_PATH': ['/org/freedesktop/DisplayManager/Session0'],
'XDG_SESSION_TYPE': 'x11',
'XDG_VTNR': '7',
'XFILESEARCHPATH': ['/usr/dt/app-defaults/%L/Dt'],
'XMODIFIERS': '@im=ibus',
'_': './../jre/jre/bin/java'},
'pkg_resources.working_set': [wsgiref 0.1.2 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7),
Python 2.7.15 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-dynload),
six 1.11.0 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
pytz 2018.3 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
python-dateutil 2.7.2 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
permutation-test 0.18 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
pandas 0.22.0 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
wheel 0.31.1 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages),
setuptools 40.2.0 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages),
pip 10.0.1 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages),
certifi 2018.8.24 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages)],
'sys.path': ['/nfs/nhome/live/elena/pycharm-community-2016.2.3/helpers/pydev',
'/nfs/nhome/live/elena/attrib_experiment_analysis/preliminary_analysis',
'/nfs/nhome/live/elena/pycharm-community-2016.2.3/helpers/pydev',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python27.zip',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/plat-linux2',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-tk',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-old',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-dynload',
'/nfs/nhome/live/elena/.local/lib/python2.7/site-packages',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages',
'/nfs/nhome/live/elena/attrib_experiment_analysis/preliminary_analysis'],
'sys.prefix': '/nfs/nhome/live/elena/anaconda/envs/without_stan',
'sys.version_info': sys.version_info(major=2, minor=7, micro=15, releaselevel='final', serial=0)}
You can activate the console through terminal via "python3" command (exit with "exit()" command). You can then import modules, that are inside the virtual environment. The Pycharm "python console" instrument seems working for everything else, besides importing modules from inside the virtualenv.
Please try 2019.1 EAP build, the original problem with conda venv activation in the terminl should be fixed there (PY-23417).
Hello,
I'm having same predicament. The same problem occurs if I use "Run". The virtual environment doesn't get activated.
I just downloaded and tried the 2019.1 EAP but I still have the same problem.
@Pdallair
Can you try the workaround from https://youtrack.jetbrains.com/issue/PY-27234 and tell us if it's working? :
import osin the system terminal using the same interpreterprint(os.environ['PATH'])PATHenvironment variable to your Run/Debug ConfigurationWe have come up with a similar work around here. We added the library paths to our project interpreter Paths.
I'm relieved that there are workarounds for library paths but you should still know that this isn't a fix.
The virtual environment's activation script is not ran and that is troublesome for some members of the python community. The basic introduction to python virtual environments instructs us to activate them. It seems rational to expect our IDE to do the same.
When I have a virtual environment selected as project interpreter, the Terminal has no trouble activating it. I expect the Python Console and Run Configurations to do the same but surprisingly that is not the case.
I'm not familiar with the code base IntelliJ developers have to deal with but this seems like a reasonable requirement amendment (or bug fix) to consider.
Anyways, I really like PyCharm overall, it has been making our development more efficient.
Thanks for responding, I guess I'm just as disappointed the "fix" wasn't in the 2019.1 EAP I downloaded. Hope it makes it by release date. Otherwise, please consider it for the subsequent release.
Well, this isn't the final fix that I'm offering. I just wanted you to try the workaround to see if it helps and confirm this is the environment activation issue.
We are aware of this issue and are working to provide a proper fix for the stable release.
Hi Pdallair, could you please clarify, do you manually edit the venv activation script to add those paths to `PYTHONPATH`? The original issue is about `conda`, and as far as I can see you are not using it so this is a separate problem.
Pavel,
You are right, I have edited the virtual environment installation's activation script to modify the PYTHONPATH.
You are right, I am not using Conda. The symptoms are similar in abstract but I suppose it is, in that case, a separate problem. In that regard, should I open another request or create another poste?
Would be great if you can submit a ticket to our bug tracker: https://youtrack.jetbrains.com/issues with details about your activation script changes. Are those simple `export PYTHONPATH=...` in `activate`?
Alright, I've created the issue PY-34482. Sorry about the drama over here. :P
To answer your final question. Yes it is that simple (see my pastebin of the activation script).
I was struggling to activate my virtual environment, I got this idea online and decided to apply it, just copy and paste your path to the activation script the .bat script is
C:\Users\dalmas.DESKTOP-DOR8V5M\Desktop\Dalmo\venv\Scripts\activate.bat
It worked and I was able to run server through
python manage.py run server
Hello. The problem of python console launched not in venv happens to this day. The problem seems to be with the way, pycharm starts it:
This causes problems with pyspark. Because it spawns child processes outside venv and gives error:
Python console should FIRST activate venv and then launch ipython.
Is there a way I can configure it in PyCharm?
PyCharm doesn't support virtual environment activation with remote interpreters yet. Please vote for the corresponding feature requests in our issue tracker https://youtrack.jetbrains.com/issue/PY-29551, https://youtrack.jetbrains.com/issue/PY-35978
it's so easy to activate virtual env from pycharm
go to the terminal of your pycharm and write this commands:
1-cd venv/Scripts
2- activate (or activate.bat)
3- cd .. (there is a space between ' cd ' and ' .. '
4- cd ..
5- be sure to go out of the virtual environment. [ in my case --> (venv) C:\Users\admin\PycharmProjects\pythonProject1> ]
6-be sure to install a virtual environment before using this commands
Good Luck.