When I enter my virtual environment (source django_venv/bin/activate), how do I make that environment transfer to apps run outside the terminal, such as Eclipse or even Idle? Even if I run Idle from the virtualenv terminal window command line (by typing idle), none of my pip installed frameworks are available within Idle, such as SQLAlchemy (which is found just fine when running a python script from within the virtual environment).
Tell Eclipse or Idle that the python interpreter is django_venv/bin/python instead of /usr/bin/python
If you want your virtualenv to be really permanent to your project, you could stuff the following two lines directly into your code:
activate_this = 'this_is_my_project/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
Related
I installed Anaconda (python 3) on a Windows 10 machine. Whilst import pdb works inside a script I cant use pdb <script> on the command line. I would have thought is should work right out of the box. If not, what is the right way to install it on an Anaconda environment? I would also like to use ipdb.
Problem
pdb package comes with Anaconda 3's Python installation, so when you run import pdb in the command line it successfully loads it.
Running pdb <script> from command line makes Windows look for a pdb.exe executable in any of the paths listed in your PATH environment variable.
As far as I know, pdb does not come with a script executable. ipdb does.
Solution
PDB:
Run python -m pdb myscript.py from an Anaconda terminal.
IPDB:
Install the ipdb package in your Anaconda Python environment.
Try running your script in the command line with ipdb <script>.
If the second step does not work for you, this is probably because the anaconda script folder does not appear in your PATH environment variable.
Press winkey
Search for Edit the system environment variables
Click on environment variables....
Look for a PATH variable in either your user or system variables. (If it doesn't exist, create a new variable).
Double click it and add a new path pointing to your anaconda Scripts folder (for me it's in %localappdata%\Continuum\anaconda3\Scripts).
And you're done.
I'm new to pycharm, virtualenv, linux and git.
I've recently begun a journey of using djangoto make webapps. Before I used sublime to make scripts however now a more complex project management system such as pycarm was needed. I actually wanted to get a linux VM and go down that road but was advised that windows python IDE such as pycharm would be suitable
I recently learnt the importance of dependencies and how to use virtualenv. However in this tutorial, under the 'How do I use my shiny new virtual environment?' it starts using commands such as:
ls env
and
which python
Neither of which my pycharm console would understand.
I could use a console emulator such cmder to use the commands but then I would remove the convience of using the IDE's integrated one.
Should I upgrade to a linux VM ? Or can I install a package that allows me to use such commands in PyCharm.
As a bonus question, what are the commands in that tutorial ? are they linux commands? when ever i see $ .... is that the linux console ?
You can accomplish this using Vagrant: https://www.vagrantup.com/
You can use Vagrant and VirtualBox to setup a Linux VM (distro of your choice) and then install all of your Python dependencies in the VM. Once you have that setup, you can tell PyCharm to use the Python interpreter in your VM by following these steps:
Open the project settings dialog box in PyCharm.
Expand Project: (your project name) on the left side.
Click on Project Interpreter.
Click on the cog icon on the upper right side of the window and select Add Remote.
Click on the Vagrant radio button.
In the Vagrant Instance Folder box, select the directory your Vagrantfile is located in.
In the Vagrant Host URL box, make sure ssh://vagrant#127.0.0.1:2222 is specified.
Click OK.
Since Vagrant is compatible with Windows this solution should work for you. I have done it successfully using macOS and it works great. Good luck!
You might find this tutorial useful: https://developer.rackspace.com/blog/a-tutorial-on-application-development-using-vagrant-with-the-pycharm-ide/
I got this to work on Windows 10 with Anaconda Prompt. This terminal which comes with Anaconda, creates a "base" environment with a linux-like virtual machine and your Windows file system (C:\\) mounted to /c, and has bash installed with common Unix commands like cd, ls, chmod, echo, cat, ... Running programs from bash with access to environment variables is much nicer than Windows Powershell etc.
Now to get your Terminal in Pycharm to use Anaconda Prompt instead of cmd.exe, I followed this answer. After installing Anaconda and/or Anaconda Prompt, right-click -> Open File Location -> right-click the shortcut -> Properties -> copy file path. Then use your file path instead.
Conda is great for package environment management. Learn more about it here. For Django + Conda specifically, read here. You can also use pip to install from Python package indexes, github repos, and requirements.txt files instead. Unless you know how Anaconda Prompt works, I don't recommend creating your own environments from scratch. What worked for me was:
(base) C:\Users\wassadamo> conda create -n mynewenvironment --copy base
...
(base) C:\Users\wassadamo> conda activate mynewenvironment
(mynewenvironment) C:\Users\wassadamo> ls
folderA folderB file.txt
Works!
Whenever I try running conda deactivate to leave the base environment, my bash commands would stop working. So clone base as above.
Another tip: if you want to run shell scripts from Terminal within PyCharm with Anaconda Prompt this way, then execute them (e.g. "run.sh") on command line with
bash run.sh
I tried putting this on the first line of my run.sh
#!/usr/bin/bash
And running it with
./run.sh
But this had the effect of running it in an external Anaconda Prompt instance (add sleep, or some user input command to force it to wait and see for yourself). Explicitly running my .sh files with bash had the desired effect of running them in the same shell as I started them in PyCharm Terminal configured with Anaconda Prompt.
Currently i have a virtualenv created with the virtualenvwrapper. In that virtualenv i installed the cx_Oracle extension with pip install cx_Oracle.
I have a python script using several commands from cx_Oracle like connect and such.
When running my script thought the activated env (python script.py) it works fine and produces no errors.
But when i try to run the same script in PyCharm 4 it does not work. I have the virtualenv as intrepeter selected. When running the script i get an error as follows:
/Users/pgerrits/.virtualenvs/siebelaudit/bin/python3.4 -u /Applications/PyCharm.app/Contents/helpers/pydev/pydev_run_in_console.py 64420 64421 /Users/pgerrits/PycharmProjects/SiebelAudit/Audit/Siebel Audit/scratchpad.py
Running /Users/pgerrits/PycharmProjects/SiebelAudit/Audit/Siebel Audit/scratchpad.py
PyDev console: starting.
ImportError: dlopen(/Users/pgerrits/.virtualenvs/siebelaudit/lib/python3.4/site-packages/cx_Oracle.so, 2): Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
Referenced from: /Users/pgerrits/.virtualenvs/siebelaudit/lib/python3.4/site-packages/cx_Oracle.so
Reason: image not found
When running the same script with the same command in the terminal with the env activated, i get no error.
I already tried the following:
- Added ENV variables for oracle_home, etc using a script
- added env variables using the pycharm env variables option
It is really annoying that i have to switch to my mac terminal for running and debugging. Has anyone a clue what could be the issue here?
I had to set the environment variables for ORACLE_HOME
DYLD_LIBRARY_PATH and LD_LIBRARY_PATH
and restart PyCharm to get cx_Oracle to work.
This is a known issue of PyCharm. The only way is to create virtualenv using PyCharm. If you create with virtualenvwrpper, there's a chance that PyCharm won't recognize it.
import os
import platform
if platform.system() == 'Darwin':
os.environ["ORACLE_HOME"] = '/opt/oracle/instantclient_11_2'
os.environ["DYLD_LIBRARY_PATH"] = '/opt/oracle/instantclient_11_2'
os.environ["LD_LIBRARY_PATH"] = '/opt/oracle/instantclient_11_2'
I have a VPS with system-wide installed python 2.5.
I installed python 2.7 to one of the user's home dir (using --prefix). added it to bashrc and bash_profile, exported python variable to env, and now when I type python in console python 2.7 is running.
But when I checked python version from my application (Django using with FastCGI) I still see that it is using 2.5.
In ps output I see python processes running for this account and apache processes runing with hosting-specific account. How can I switch this particular account to 2.7 without changing system-wide version?
Thanks!
One option is to use the python virtualenv tool to create a Python virtual environment that you can source in your .bashrc.
mike#tester:~$ virtualenv --python=/usr/bin/python3 $HOME/fcgi_python
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in fcgi_python/bin/python3
Also creating executable in fcgi_python/bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
mike#tester:~$ python --version
Python 2.7.5+
mike#tester:~$ source $HOME/fcgi_python/bin/activate
(fcgi_python)mike#tester:~$ python --version
Python 3.3.2+
In the example above you would replace the argument after --python= with the path to the Python interpreter installed in the user's home directory.
I had a call to python interpreter via env program in my fast cgi dispatch script. When I explicitly put path to 2.7 to the first line of the script it works as expected.
I've set PYTHONPATH in my /home/me/.bashrc and all worked ok from terminal, but when Apache w/ mod_wsgi starts my python scripts, it acts under sysem or dedicated user, which knows nothing of my .bashrc.
For this particular situation, I just used apache config (apache2.conf) to set python path for apache (WSGIPythonPath option).
I have installed virtualenv 1.10 on a Windows 7 machine with Python 2.7 on it.
I have created a virtual env called TESTENV. It is located in C:\
If I run C:\TESTENV\Scripts\activate and then type python followed by:
import sys
print sys.prefix
it outputs C:\TESTENV, as exprected.
But if I run D:\virtualenv_test.py (which is a script containing the two lines of code I used above) it outputs C:\Python27.
I tried adding a shebang line to the script but that doesn't work on Windows.
Why is the script not running in the context of the virtual environment?
virtualenv doesn't change the default application that starts a file.
When you installed Python, the installation associated .py and .pyw files with appropriate C:\Python27\python.exe and C:\Python27\pythonw.exe applications. So when you run (in your cmd) only D:\virtualenv_test.py, cmd looks into the registry and finds the application that is associated with .py files and then runs the file with that application. In this case that is the previously installed C:\Python27\python.exe.
virtualenv activate only adds that particular virtual environments python interpreter at the start of the PATH environment variable, so when you type python in cmd you will get the appropriate python.exe executed.
So the solution to your problem is to run your script as:
python D:\virtualenv_test.py
After you activated the environment.