Using virtualenv in Pycharm as Django IDE - python

Please help me on this. I am doing this on Fedora 16.
My virtual environment is /home/username/Desktop/.pythonenv
Now, notice the dot. Because it is a hidden directory, I had to make a symbolic link in order to for Pycharm to "enter" the directory.
sudo ln -s /home/yeukhon/Desktop/.pythonenv /home/yeukhon/Desktop/sypythonenv
Now I can use this virtual environment. However, two problems occurred and I am not sure if first one has anything to do with the second one.
A. Missing GTK and GST
Here are the two screenshots. The virtualenv python is missing GTK and GST... I have no idea why.
B. Django is not importable in this environment
I tried to Run my Django project under virtualenv (project1) but I can't. I can run the same project through Terminal.
[yeukhon#localhost ~]$ cd Desktop/djangoapp/project1
[yeukhon#localhost mysite]$ source /home/yeukhon/Desktop/.pythonenv/project1/bin/activate
(project1)[yeukhon#localhost mysite]$ python2.7 manage.py runserver
# I had to use python2.7 instead of python. That's the only problem with terminal here.
Question
How do I make it runable in PyCharm with my virtualenv Python interpreter?
Does #1 has anything to do with #2?
How do I make GTK and GST appear in my virtualenv?

First, no need to create a symlink, as the add interpreter dialog has a button 'show hidden files and directores', and you can manually type in the path too.
Each virtualenv is, since version 1.7, an isolated environment by default . That means you have to explicitly pass the --system-site-packages ENV if you want to automatically include the systems libraries (before 1.7 you had to pass the no-site-packages option if you wanted an isolated env)
So my first guess is that you simply haven't installed the libraries you are missing inside your virtualenv. Check the output of pip freeze after activating your virtualenv. If those libraries are missing, just install them with pip:
pip install django
Now, as you seem to using the non-default python for your virtualenvs, make sure you are not using the system one instead. You can also invoke it directly by using the one under the /bin directory of your virtualenv.

Related

How to move django project from ubuntu env to virtual env?

I have an existing django project which I have developed using python libraries installed in system and adding missing ones to the system. But now conflict has come for python-requests as system has 2.2 version but I need >2.5. Dont't want to uninstall and put newer one as it may break the OS. So now, I want to use virtual env and install packages there in complete isolation to that of OS.
I think the solution you're looking for is to download a different version of python without uninstalling your original, then start up virtualenv venv, but by passing in a path to the new python.exe file. Like this: virtualenv -p venv <path-to-executable-here>, then just do source bin\activate, as usual. This starts the virtual environment using the python executable you passed to virtualenv through the terminal.
Also, this might not be the only way, but, there's something called ModuleFinder which enables you to get a list of all the modules your script is importing--That is if you don't want to type them out manually, and you have extra modules installed (otherwise pip freeze > requirements.txt would do the job, and your new virtual environment would install all the packages in requirements.txt).

Pycharm doesn't have Django, or pyenv, or let me install python modules. Completely lost

I'm trying to work with python in PyCharm, and set up a virtualenv for my project. What I imagine should be the way this works is that I set up a project to pull from a repository. It's a Django project, so I enable Django support. Setting up a pyenv is good hygiene, so I set the interpreter to use a pyenv. PyCharm supposedly includes pyenv, so I don't need to install it from my os, and I was under the impression that either Django came with it as well, or I could install it from PyCharm.
Instead of any of that working the way it seems like it should and has been described, pyenv doesn't appear to be installed:
$ pyenv
No command 'pyenv' found, did you mean:
Command 'p7env' from package 'libnss3-tools' (main)
Command 'pyvenv' from package 'python3-venv' (universe)
pyenv: command not found
$ virtualenv
The program 'virtualenv' is currently not installed. You can install it by typing:
sudo apt-get install virtualenv
Of course, the only reason I'm even caring about that is that going to Settings --> Project --> Project Interpreter to install new modules doesn't work, either with or without a Pycharm-created virtualenv. I made a post to the Jetbrains forums here, but I'm not expecting a response, as they seem almost completely abandoned.
Then I thought I'd try enabling Django support, so that I could at least get that module working. But instead it says that Django isn't installed; In the Run/Debug configuration, at the bottom it says Django is not importable, the django icon has an x on it, and of course, every import django statement is underlined in red, indicating it can't be imported. My versions are:
Ubuntu 15.10
PyCharm 5.0.4
Python 2.7.10
So why does everything seem to be broken? What am I missing? If I have to install outside of pycharm, I don't really care, I'm just trying to set everything up the right way, so that nothing steps on anything else, and everything works as intended. Pycharm seemed to "volunteer" to handle everything, and I'm just trying to make that work. Using a pyenv seems to be what everyone recommends, and I'm just trying to do that. Any help at all would be appreciated.
To install Django on your virtualenv using pyenv please use the following command line:
pyenv activate <virtualenv_name>
pip install django
Or (for example if pyenv fails to activate your virtualenv due to some reason) it is possible to just execute pip using the full path:
<path_to_virtualenv_folder>/bin/pip install django
Then select the Python interpreter from your virtualenv as a project interpreter in PyCharm.
Normally PyCharm should be able to install packages, if it doesn't work please submit an issue to PyCharm tracker.

Confused about Python installations, module installation, and interpreter

So yesterday I had to create a virtualenv in order to be able to install Python modules that wouldn't install thanks to OS X El Capitan's new SIP. I thought I did everything right, but today I'm reaching a different conclusion. I hope I can be clear about it.
my python custom install is at myname/learnp/imdb_module, this is where I created it with virtualenv. Edit: I later moved it to myname/learnp/ayr2/imdb_module.
However, when I try to run the interpreter, it seems to always default to the Python that is in Library or something along these lines. I found out about this because a certain module that I managed to install in this custom python env wouldn't import, when I checked what modules I have, it wasn't the same as what I expected.
Furthermore, it seems that ALL other modules that I wanted to install on the CUSTOM virtualenv were installed on the main python env, and that I wasn't installing those modules on the custom env all along.
Excuse me, but I'm very confused right now.
I know how to create a virtual env
I know how to activate it (it appears to the right on Terminal line)
I don't know how to install modules to my virtual env
I don't know how to make the interpreter run from the virtual env so I can do python operations that are only possible by using custom env modules
Any advice is much appreciated!
Update:
Followed Will Hogan's answer for troubleshooting,and I think something weird is happening, quoting my comment to his answer:
HI, thanks for taking the time to answer. This is basically the way I understood this. However, let me attach a screenshot: http://i.imgur.com/DfpngJq.jpg . Am I right to assume something is wrong here? My prompt is changed with the virtualenv named "imdb_module", but when I type in which python it doesn't list ayr2/imdb_module/bin but rather a folder with the path usr/bin/python, which if I understand correctly is the "default" environment.
And not if this helps in any way, but echo $PATH when (imdv_module) appears to the right of the prompt, gives this (I redacted my name): /Users/REDACTEDNAME/learnp/imdb_module/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
While creating the virtualenv you should see it installing setuptools and pip:
$ virtualenv testvenv
New python executable in testvenv/bin/python2.7
Also creating executable in testvenv/bin/python
Installing setuptools, pip...done.
After ensuring the virtualenv is activated you should see your prompt change:
$ . ./testvenv/bin/activate
(testvenv)$
Now you can confirm the paths to python and pip, which should be in the virtualenv:
(testvenv)$ which python
/private/tmp/testvenv/bin/python
(testvenv)$ which pip
/private/tmp/testvenv/bin/pip
If you aren't seeing the python and pip locations as being under the virtualenv's directory, then the virtualenv has not been activated.
I would also ensure that, if you're executing the .py file directly (and not with "python foo.py"), that your shebang line uses:
#!/usr/bin/env python
Or even the full path to the virtualenv's python, e.g.:
#!/tmp/testvenv/bin/python
As opposed to, say:
#!/usr/bin/python
The first will search in the current environment, which will be set by the virtualenv activation. The second explicitly points to the virtualenv's `python'.

How to get two versions of Python to coexist within a virtualenv?

In order to not alter the default python installation, I have a virtualenv set up that automatically runs on startup. I've also set up my profile so that unless a virtualenv is running, pip will not run. I've been using python2.x up to now, and have now installed python3 via homebrew. When I attempt to install a new package via pip3, I get the following error: Could not find an activated virtualenv (required).. However, when I check for the existence of a virtual env by checking the $VIRTUAL_ENV variable, it tells me that a virtual env is in fact running: -bash: /Users/me/virtualenvs/r: is a directory. Do I need to create separate instances of virtualenvs for each version of python? Why will one of my versions of python recognize the virtualenv, while the other will not?
All your virtualenvs are stored in a directory -- /Users/me/virtualenvs in your case. If you go there, you'll notice that it executes totally separate python interpreters in each virtualenv. For example, this is a freshly created virtualenv:
I'd imagine that running pip3 while on the python2.7 virtualenv wouldn't work for that reason.
It seems that there are two different kinds of virtualenvs, one of which is compatible with python2 and one of which is compatible with python3. I'm not sure whether there is a way to get one that is compatible with both, but I went ahead and created a new virtualenv using python 3 (python3 -m venv myPython3Env), and have been able to successfully use that.

virtualenv and CLI tools

I'm aware of how to use virtualenv for isolating Python dependencies in a long-running script, like a Flask or Twisted app. But I've been sort of puzzled about how you're supposed to go about this for a script intended to be invoked from the command line.
Suppose I wanted to make a CLI tool for interacting with some API, perhaps using Click or docopt. Obviously you don't want to have to source venv/bin/activate every time you want to use this tool. But I'd assume that it's still best to still use virtualenv to keep a clean environment even beyond development.
Sorry for the newbie question, but...what are you supposed to do to package up a script so it can be cleanly used in this manner? (I'm more used to RubyGems, and am still figuring out Pip and VirtualEnv.)
In general, if a package you've installed in a virtual env that provides binary command line script, say in ~/.virtualenv/bin/ you can symlink it into ~/bin/ (or wherever on your path you'd like to put local scripts).
There are a couple projects that aim to solve this issue:
pipsi the pip script installer -- amounts to doing the virtual env creation and symlinking for you
pipx pip for executable binaries
An excellent article on virtualenv by Dabapps would make it clear for you:
http://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/
As for invoking it from a CLI script:
1. cd to your project root
2. env/bin/python your_main_file.py (Assuming your virtualenv is named env)
This way you don't need to source the virtualenv everytime.
Each virtualenv has it's own Python site_packages, builtin modles, and Python interpreter. So virtualenv is meant to be used at the project level, not at the "package by package" level. It isolates a collection of Python modules and possible dependencies. Each virtualenv has it's own location where pip will install packages. In theory, virtualenv should not be necessary, but in practice, it's nice to have a way to have different "environments" with different versions of Python modules and Python interpreters. I don't know if Ruby has something similar, that would allow you to have different "sets" of gems for different projects.
People that use straight virtualenv will add aliases to their .bashrc, for example:
alias workonawesomeproject="source ~/venv/awesomeproject/bin/activate"
They would activate the virtualenv with the alias
workonawesomeproject
To leave a virtualenv you use the command deactivate
An easier way to deal with virtualenvs is to use virtualenvwrapper
pip install virtualenvwrapper
Add these lines to your .bashrc (or other shell initialization file)
export WORKON_HOME=$HOME/venv # this directory is your choice
export PROJECT_HOME=$HOME/src # this directory is your choice
source /usr/local/bin/virtualenvwrapper.sh # leave this alone
If you just modified your .bashrc make sure to source it
source ~/.bashrc
Then to make a new virtualenv you simply run
mkvirtualenv awesomeproject
To use that virtualenv
workon awesomeproject
To deactivate that virtualenv
deactivate
Virtualenvwrapper Docs:
http://virtualenvwrapper.readthedocs.org/en/latest/install.html

Categories