I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip, and fabric. Should be straightforward enough, but it appears that I'm not able to activate and run commands in the virtualenv through the shell script.
os.system('virtualenv %s --no-site-packages' % project_name)
os.system('source %s/bin/activate' % project_name)
os.system('easy_install pip')
When running, this errors out:
$ startproject+ -s false sample
New python executable in sample/bin/python
Installing setuptools............done.
/testing
Searching for pip
Best match: pip 0.4
Processing pip-0.4-py2.6.egg
pip 0.4 is already the active version in easy-install.pth
Installing pip script to /usr/local/bin
error: /usr/local/bin/pip: Permission denied
Obviously the source line isn't being run, but why? Is it a concurrency/threading issue, or something deeper with virtualenv?
Thanks!
Each call to os.system runs the command in a new subshell, which has the same properties as the original python process.
Try putting the commands into one string separated by semicolons.
Just don't use "source activate" at all. It does nothing but alter your shell PATH to put the virtualenv's bin directory first. I presume your script knows the directory of the virtualenv it has just created; all you have to do is call _virtualenv_dir_/bin/easy_install by full path. Or _virtualenv_dir_/bin/python for running any other python script within the virtualenv.
Each os.system call creates a new process. You'll need to ensure that the activate and the easy_install are run in the same os.system or subprocess call.
You could also install virtualenvwrapper, and use the postmkvirtualenv hook. I use it to automatically bring in fresh copies of pip and IPython into virtualenvs I create (as I don't want it using my system IPython). I also use it to copy pythonw into the virtualenv, otherwise wx-based stuff won't work. Looks like this:
easy_install pip
pip install -I ipython
cd ~/bin
python install_pythonw.py ${VIRTUAL_ENV}
Related
I am running Gitbash for windows and have installed pipenv using pip. Yet when I invoke pipenv nothing happens:
Since there isn't a command not found error, I believe the script is recognized it just isn't running properly.
Pipenv is installed globally:
I also can confirm that the Scripts folder is in the file path:
I don't think it is a problem with Gitbash because I can run other pip packages in the same scripts folder:
I'm also able to run python pipenv.exe but not pipenv.exe when in the Scripts folder:
I've struggled with this myself untill just now.
I've got a few suggestions.
SOLUTION 1:
I was calling Poetry to try and make sense of it as well and then I tried calling both applications from Powershell. Both work as inteded.
SOLUTION 2:
Calling python -m pipenv, as sugested in this other Stack Overflow thread, also works as intended. You could alias the command to pipenv in git bash and call it a day too.
Quoting the docs on the -m flag:
When called with -m module-name, the given module is located on the Python module path and executed as a script.
Quoting appdividend.com:
The -m flag in Python searches the sys.path for the named module and executes its contents as the __main__ module.
It goes through whatever path is in which pipenv and executes the module as a script.
SOLUTION 3:
This is how I ended up fixing my Pipenv blunder.
Reinstall Python and all packages to AppData.
MINGW64 was having trouble seeing Pipenv in C:\Program Files. I also noticed that I had some packages in AppData\Roaming as well, so I figured I'd reinstall Python, unticking the Install for all users option (to trigger AppData installation) to see if I could wrangle all packages together.
After that I tried installing Pipenv and succeeded in calling it as expected.
This is all highly anecdotal. I have no idea why MINGW64 failed to call JUST Pipenv and not Poetry, but this is how I've fixed it on my end.
I installed locustio on my ubuntu using pip
"pip install locustio"
the problem is that everytime I run any locust command on my console (for example)
locust --version
the console returns this
zsh: command not found: locust
any solutions ?
here's a trick:
write this command pip uninstall locustio in the console
this is what will be returned in the console
Uninstalling locustio-0.12.2:
Would remove:
/home/elta/.local/bin/locust
/home/elta/.local/lib/python2.7/site-packages/locust/*
/home/elta/.local/lib/python2.7/site-packages/locustio-0.12.2.dist-info/*
Proceed (y/n)?
write n , and copy the first directory which would be in my case
/home/elta/.local/bin/locust
now this is the directory you gonna use every time to call locust command, for example
/home/elta/.local/bin/locust --version
.... not the best solution, but at works perfectly and did the trick
Make sure the scripts are installed in a directory that is available in your PATH environment variable.
Use pip show --file locustio to figure out where the scripts are installed (in your case: ~/.local/bin). Modify your PATH to contain the directory where the scripts are installed. In your case, you probably should add something like the following to your ~/.zshrc file:
if [ -d "~/.local/bin" ] ; then
export PATH="~/.local/bin:$PATH"
fi
Then the locust command should be available in all new shell sessions.
I am installing Python 3.7.2 for the first time, and I'm using the VS Code python extension.
When I run python -V I get Python 2.7.10 which is not correct!
When I select the usr/local/bin/python3 interpreter in VS Code I get this error when running a script:
bash: /Users/erik/Work/Python/usr/local/bin/python3: No such file or directory
But when I look in usr/local/bin I can see that Python3 is there. I'm not sure why VS Code pastes the work directory in front of usr/local/bin ?
My first thought was that Python3 should be in the PATH variable so I ran the included Update Shell Profile command, which gives this feedback:
This script will update your shell profile when
the 'bin' directory of python is not early enough
of the PATH of your shell.
All right, you're a python lover already
Now, after rebooting VS Code I get a new option for selecting an interpreter:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Is that different from the Python in usr/local/bin ? When I select it, I get this error:
The script isort is installed in '/Users/erik/Library/Python/3.7/bin' which is not on PATH.
I also get this sideways related error:
You are using pip version 18.1. You should consider upgrading via the 'pip install --upgrade pip' command.
But, when following these instructions I get yet another error:
bash: pip: command not found
All in all, this process and the official documentation seem less than user-friendly? Apparently I'm required to dig deep through my mac's system files in the terminal before even writing one line of code.
Am I missing an essential step here?
I suggest that you use virtual environment for your project
first
pip install virtualenv
open cmd in your project directory that you open in VS-Code (it's important that vs-code sees this virtualenv folder that we will create)
mkvirtualenv my_env
and it will activate it automatically. if not run
my_env/bin/Scripts/activate or my_env/Scripts/activate
Then go open vs-code then select my_env for python interpreter
Well, if you want to change your default Python version for the whole system, it might break some applications that are depending on Python 2.
You can alias the commands by adding this line to your ~/.bash_profile:
$ alias python='python3'
The python command will now refer to python3.
If you want to execute the original Python (which refers to python2), you can escape the alias (so \python will launch python2 without touching the alias).
Btw.
$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.7 /usr/local/bin/python
could also be a workaround for you.
I added the pip installation folder in my python site-packages directory to my PATH, but I can still only run it via python -m pip in my git bash. Just pip gives me command not found.
I looked around the pip directory and I don't see any binaries, so this makes sense. Yet clearly pip is a thing that is normally used on the command line without python -m. So what component am I missing here?
Try adding C:/path/to/python/Scripts/ to PATH
There should be a pip.exe there!
in your Anaconda directory there is a pip file in the direcctory Scripts.
add this path : C:\Users#yourname\Anaconda3\Scripts
i did the same and it was successful !!
(when i wrote pip in git bash it worked)
I'm trying to launch a command in different Python virtualenvs using subprocess.check_call().
To activate the virtualenv (in a Python 2/3 agnostic manner), I simply append the path to my virtualenv bin (or Scripts under Windows) to the PATH and then call subprocess.check_call() with this modified environment. Like so:
environment = os.environ.copy()
environment['PATH'] = os.pathsep.join([bin_path, environment['PATH']])
subprocess.check_call("pip install -r dev_requirements.txt", env=environment)
However, what I notice is that pip installs everything in the system Python site-packages. If I change the check_call() for:
subprocess.check_call("pip install -r dev_requirements.txt", env=environment, shell=True)
Then suddenly pip operates in the virtualenv as expected.
What bothers me is that in both cases running where pip gives me the path to the virtualenv's pip first.
What could explain this odd behavior ?
PATH is not the first place where CreateProcess() used by Popen() on Windows looks for the executable. It may use pip.exe from the same directory as the parent python.exe process. The shell (cmd.exe) uses different rules. See Popen with conflicting executable/path.
To avoid the dependency; use the explicit full path to pip. You don't need to change the environment in this case:
import os
from subprocess import check_call
check_call([os.path.join(bin_path, 'pip.exe')] +
'install -r dev_requirements.txt'.split())