Permissions prevent creating new virtualenv with virtualenvwrapper on Mountain Lion - macos

I get an error when I try to create a new virtualenv using virtualenvwrapper. Here's the command I'm trying:
mkvirtualenv -a . -i Flask ~/.virtualenvs/dcc-admin/
Here's the output:
New python executable in /Users/raddevon/.virtualenvs/dcc-admin/bin/python
Installing setuptools......
Complete output from command /Users/raddevon/.vir...dcc-admin/bin/python -c "#!python
\"\"\"Bootstra...sys.argv[1:])
" /Library/Python/2.7/...ols-0.6c11-py2.7.egg:
Processing setuptools-0.6c11-py2.7.egg
Removing /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg
error: /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg: Permission denied
----------------------------------------
...Installing setuptools...done.
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 8, in <module>
load_entry_point('virtualenv==1.9.1', 'console_scripts', 'virtualenv')()
File "/Library/Python/2.7/site-packages/virtualenv.py", line 979, in main
no_pip=options.no_pip)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 1091, in create_environment
search_dirs=search_dirs, never_download=never_download)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 611, in install_setuptools
search_dirs=search_dirs, never_download=never_download)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 583, in _install_req
cwd=cwd)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 1057, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /Users/raddevon/.vir...dcc-admin/bin/python -c "#!python
\"\"\"Bootstra...sys.argv[1:])
" /Library/Python/2.7/...ols-0.6c11-py2.7.egg failed with error code 1
I thought I could get around this with sudo, but that gives me sudo: mkvirtualenv: command not found.
I've checked my permissions on the file causing the error, and my user appears to have read and write permissions.
I'm not sure where to go from here. mkvirtualenv should be able to delete that file since I have the delete permission for it. Alternately, sudo should be able to run mkvirtualenv. Someone please tell me where I'm going wrong.
Update: I figured out part of this problem. virtualenvwrapper was trying to run the Python commands through a different install of the Python interpreter than the one I wanted. I used the -p switch to specify the correct interpreter, and that worked.
I read up on this, and, apparently, it should be using the same interpreter that would be the default in my shell, but this is not the case. The interpreter used as the default by the shell is /usr/local/bin/python but virtualenvwrapper is trying to run through /Library/Python/... I'm still unsure why this is the case. /usr/local/bin is the last python path defined in my .zshrc.

I currently found a workaround
virtualenv env -p /usr/local/bin/python

Related

Eror while updating packages from terminal in linux mint

i need help. since 5 days before, i tried to update packages in Mint using this command
sudo apt update
but, at the end of the process, the terminal gives me traceback
Traceback (most recent call last):
File "/usr/lib/cnf-update-db", line 8, in
from CommandNotFound.db.creator import DbCreator
File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code
i tried to find the solution to fix this, but i can't. does anyone have a solution?
If the apt-get also doesn't work, can you try this? Most probably, apt_pkg is not in the correct path.
cd /usr/lib/python3/dist-packages
ls -la /usr/lib/python3/dist-packages
Look for any file of the format apt_pkg.cpython- and so on. Copy that file's name. And replace apt_pkg.cpython-36m-x86_64-linux-gnu.so with the copied file's name in the command below, and hit enter.
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
I also had the issue with "cp: cannot stat 'apt_pkg.cpython-36m-x86_64-linux-gnu.so': No such file or directory while I was as super user. I did the same command withouth the sudo command and it worked fine. I have in my system python 3.5, 3.6 and 3.7, so I do not think the version of python is a problem.
cp apt_pkg.cpython-36m-x86-64-linux-gnu.so.so apt_pkg.so
I hope this help someone now at 2022 :D
Regards.

How can I run pydoc when it doesn't appear to be on the PATH?

I am using MACOSX 10.12.3 and Python 3.52
I am running into issues when running pydoc in a bash script in python IDLE.
For example:
myFile = ‘/home/user/afile.py’
import subprocess
subprocess.run([‘pydoc’, ‘-w’, myFile])
Inevitably does not create the html file as would be expected.
On the other hand, when I run pydoc in the terminal
pydoc3 -w /home/user/afile.py
it always creates the wanted html file. But pydoc -w /home/user/afile.py generates an error
This is due to the fact that:
the python /home/user/afile.py contains a few print statements in python3 style. That is: print('blabla') instead of: print 'blabla' as would be the case on python2.
the bash script in IDLE does not load the correct pydoc. I should also mention that running subprocess.run([‘pydoc3’, ‘-w’, myFile]) generates an error, for example:
subprocess.run(['pydoc3', '-w', myFile]) File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 693, in run
with Popen(*popenargs, **kwargs) as process: File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 947, in init
restore_signals, start_new_session) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'pydoc3'
Any one has a way to circumvent this please?
Question 1:
Regarding running the python 3 version of pydoc against python 2 code- That will not work. Pydoc will execute (import) the target module. You would need to use python 2 version of pydoc by modifying your path or using your favorite virtual environment tool.
Question 2:
If you can get a reference to your desired installation of python (by setting the path or activating a virtual environment), then you can handle missing or broken entry points with this syntax:
python -m pydoc -w myfile
You will also need to make sure myfile is in the current directory or on the PYTHONPATH or otherwise importable

Run a Python Script from a Mac Terminal

My file name is 'corr.py' located in the Downloads folder and I'm running python 2.7 in terminal in the correct working directory. When I run the command 'python corr.py' I get the error message below. What should I do?
>>> os.getcwd()
'/Users/Daniel/Downloads'
>>> python corr.py
File "<stdin>", line 1
python corr.py
^
SyntaxError: invalid syntax
>>> python3 corr.py
File "<stdin>", line 1
python3 corr.py
^
SyntaxError: invalid syntax
You're in a Python interpreter already. First type exit() to get back to the terminal. (Or just open a new terminal tab/window.) Then use python corr.py.

Sublime does not see env variables

When running sublime from desctop it does not see my PERL5LIB environment variable.
When running it from terminal - everithing is OK.
I set my variables in .bashrc and .bash_profile.
import os
>>> os.environ['SHELL']
'/bin/bash'
>>> os.environ['PERL5LIB']
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "./os.py", line 676, in __getitem__
KeyError: 'PERL5LIB'
this makes trouble with linting and build system - I can not link and build becase of perl does not see libraries.
How to fix env variable?
Bad news:
The Ubuntu desktop session is no longer affected by .profile.
Additionally bash doesn't parse .profile if either .bash_profile or
.bash_login exists.
You can work around:
Launching desktop application with an environment variable
You can add an environment variable to an application by editing its
.desktop file. For example, to run "digiKam" with the environment
variable APPMENU_DISPLAY_BOTH=1, find the corresponding
digikam.desktop file and add the setting of the variable, via the env
command, to the entry "Exec":
Exec=env APPMENU_DISPLAY_BOTH=1 digikam -caption "%c" %i
or do small script:
#!/bin/bash
source ~/.bashrc
/opt/sublime_text/sublime_text

How can I check I'm not in a virtualenv before sourcing virtualenvwrapper commands in bash?

I am sourcing virtualenvwrapper so that I can use virtualenvwrapper functions in a bash script, as in the following simplified example:
Running do_some ve_name branch_name calls:
#! /bin/bash
# some script that takes parameters
source /etc/bash_completion.d/virtualenvwrapper # the magic of sourcing
workon $1 # pick a venv
cd /some/project/path
git checkout $2 # pick a branch
python do_something.py
This works (and I don't mind dropping out of the virtual environment once it ends, in fact I prefer it). However, if I am already in a virtual environment I get the following:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/home/username/.virtualenvs/ve_name/bin/python and that PATH is set properly.
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named virtualenvwrapper.hook_loader
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named virtualenvwrapper.hook_loader
So let's just assume that I will occassionally forget to deactivate my current virtualenv. I tried resolving this as follows:
#! /bin/bash
# some advanced script that takes parameters
deactivate
source /etc/bash_completion.d/virtualenvwrapper # the magic of sourcing
workon $1 # pick a venv
...
But whether or not I am currently in a virtualenv, I get the following error (and it doesn't deactivate if I am accidentally in a virtualenv, which is the problem I want to solve):
/path/to/scripts/do_some: line 4: deactivate: command not found
So, how can I protect against already being in a virtualenv before sourcing virtualenvwrapper commands?
You might also try using the VIRTUAL_ENV environment variable as well. I found this to be a clearer solution after viewing this, so I thought I'd leave a note in case this is reaching others searching for similar solutions.
E.g.,
if [ ${VIRTUAL_ENV} ]
then
# do some stuff
fi
Here's the relevant portion of the deactivate function in the activate script:
# (inside deactivate function)
unset VIRTUAL_ENV
if [ ! "$1" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
Here's how that gets set when you originally source the file:
# (runs when you source the activate file)
VIRTUAL_ENV="/path/to/venv/dir"
export VIRTUAL_ENV
This may not solve the original question (didn't test), but it's helpful for a large subset of cases where you just need to know if you're in a virtualenv or not.
If I understand it correct, the function workon is defined by the virtualenvwrapper. You could check if the function is defined before attempting to source the wrapper.
Replace your source command with the following:
[[ $(type -t workon) == "function" ]] || source /etc/bash_completion.d/virtualenvwrapper

Resources