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'.
Related
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
I've got myself in a pickle and would like some guidance before my laptop suffers GBH.
I have been using my Macbook for a few years without probs but when I got a new iMac at work I noticed everyone recommended useing homebrew for new Python installs (esp. on Mavericks).
Now my laptop has worked fine with the original Python. But I decided to try and do it the new homebrew way (its now Mavericks btw).
First I clear out Mavericks due to some other conflict then reinstall a fresh. Get nginx, php-fpm, mysql, etc working.
Now Python.
I can use pip to install packages. But when I try
workon myproject
I get:
/usr/bin/python: No module named virtualenvwrapper
/usr/bin/python: No module named virtualenvwrapper
But then it switches to that virtualenv anyway - but not the directory that the project is in!
When I run where python I get several entries:
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python
I've tried fiddling with my path but keep getting other errors that all seem to indicate my two Python installations are conflicting badly. Is there a simple solution or do I need to wipe out hombrew and start again from the beginning? Or just give up on homebrew and use OSX built in?
To clarify my current $PATH is:
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/go/bin:/Users/me/Development/Android/sdk/tools:/Users/me/Development/Android/sdk/platform-tools
My solution was an unusual one but here goes:
In my .zshrc file I had the following layout:
ZSH=$HOME/.oh-my-zsh
ZSH_THEME="steeef"
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
The problem was the Oh My ZSH plugin virtualenvwrapper was looking in the default python path location as the custom path had yet to be set later in the .zshrc file.
The solution therefore was to move the PATH declaration before the plugins like so:
ZSH_THEME="steeef"
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
Now the ZSH plugins are referencing the correct Python install and therefore the correct Python packages path.
Try putting your preferred version earlier in your PATH variable. So if you want to use /usr/local/bin/python, from the command line you could modify your PATH by export PATH=/usr/local/bin/python:$PATH. See here for more info: http://www.cyberciti.biz/faq/appleosx-bash-unix-change-set-path-environment-variable/
I am trying to create a Virtual Environment for my django site.
Command is below. but it says "You must provide a DEST_DIR" though I have given the directory name "SOWL".
virtualenv.py SOWL --no-site-packages
http://screencast.com/t/rej893zrg
I have spent half a day on this. I can't find anything on google search either.
Any help is much appreciated.
Thanks,
SHM
At first glance your command is out of order; the flags come first, directory last. However it doesn't seem to matter; the command, as you ran it, works for me on Mac and Windows.
I'd guess the problem lies with your installation of virtualenv. Virtualenv is also one of those tools that never seems to get updated. Make sure you have the latest. Install with pip (first choice) or if you need the actual virtualenv.py file, either way the instructions are here.
UPDATE Here's an install-Python-in-Windows guide. I know you already have Python, but this will make sure your PATH is setup and get you PIP and Virtualenv as well.
Install Python using the Windows installer
Add this to your PATH environment variable: C:\Python27\;C:\Python27\Scripts\
Get Distribute by downloading the distribute_setup.py file. (There is no Windows installer; the script does it all.)
Get PIP by downloading the get-pip.py file.
python distribute_setup.py
python get-pip.py
pip install virtualenv
Now you are set with virtualenv. However there's more — the virtualenvwrapper makes life easy. It's only for bash shells, but there's a Windows port here. There are installation instructions, tips and a rundown of the nifty new commands that the wrapper gives you.
pip install virtualenvwrapper-win
pyassoc
Create a new environment variable: WORKON_HOME=%USERPROFILE%\Envs
Now you can do this to create your virtualenv (and notice the prompt change to indicate which env you are in):
mkvirtualenv --distribute SOWL
>>>(SOWL) C:\Users\User>
I think your parameters are just out of order. Did you try
virtualenv.py --no-site-packages SOWL
I'm starting a new Python project, and I want to work with virtualenv to have a clean start. After setting one up though, I can still import old modules that I have not installed for the new virtualenv - why?
I created one with:
virtualenv ~/virtualenvs/mynewproject --no-site-packages
now i activate with source bin/activate
Now when I start a python interpreter (by just typing python), I thought that it will use the python interpreter in my virtualenv, and my pythonpath would have been set to the site-packages path of my virutalenv's python (/virtualenvs/mynewporject/lib/python2.7/site-packages), and nothing else.
However, when I look at sys.path, all the old, system-wide packages are available, and I can import them fine - which is what I don't want.
What am I missing here?
Check your PYTHONPATH environment variable which probably points to where you have the older version of your package. This variable always comes first in your sys.path so make sure you either clear it or change it to point to the virtualenv which you activate.
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.