ERROR virtualenvwrapper in GitBash - python

I trying to setup virtualenvwrapper in GitBash (Windows 7). I write the next lines:
1 $ export WORKON_HOME=$HOME/.virtualenvs
2 $ export MSYS_HOME=/c/msys/1.0
3 $ source /usr/local/bin/virtualenvwrapper.sh
And the last line give me an error:
source /usr/local/bin/virtualenvwrapper.sh
sh.exe: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
I find, where on my drive is virtualenvwrapper.sh and change directory name. On my computer it's /c/Python27/Scripts/virtualenvwrapper.sh. When I again run command
$source /c/Python27/Scripts/virtualenvwrapper.sh
I get the next ERROR message:
sh.exe":mktemp:command not found ERROR: virtualenvwrapper could not create a temporary file name
I check my enviroment variable: C:\python27\;C:\python27\scripts\;C:\python27\scripts\virtualenvwrapper.sh\;C:\msys;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\Git\bin\
I don't know where i made a mistake

The error is saying that sh.exe (the shell) can't find a command matching mktemp, which means it's not present in GitBash, at least not in your environment.
One option is you could download a Windows version of mktemp, such as http://gnuwin32.sourceforge.net/packages/mktemp.htm and then place it in the C:\Program Files (x86)\Git\bin directory. The shell should then be able to match the mktemp command and be able to proceed.

I've found a fix for this problem on a Windows 8 machine using GitBash.
TL;DR:
Get mktemp for windows, put it somewhere that can be used by GitBash, then edit virtualenvwrapper.sh and on line 202, add a touch command with the file created. It should look like this:
file="$(virtualenvwrapper_mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX)"
touch $file # this is the new line
if [ $? -ne 0 ] || [ -z "$file" ] || [ ! -f "$file" ]
FULL ANSWER:
As khampson mentioned, you do have to download mktemp and place it where your Git\bin (C:\Program Files (x86)\Git\bin usually) directory is. After that, running the virtualenvwrapper.sh file would cause an error saying:
path = C:/Users/User/AppData/Local/Temp/virtualenvwrapper-initialize-hook-XXXXXX XXXX
lpPathBuffer = C:\Users\User\AppData\Local\Temp\
szTempName = C:\Users\User\AppData\Local\Temp\tmp23A9.tmp
path = C:\Users\User\AppData\Local\Temp\tmp23A9.tmp
fd = 3
ERROR: virtualenvwrapper could not create a temporary file name.
On line 202(source), you see a function call to virtualenvwrapper_mktemp (which is just a wrapper function to call mktemp) and this is supposed to create the new temp file, but apparently it doesn't on windows.
Going through the manual for mktemp, on the examples section, you see that they are always sending something to that new file handle which forces the file to be created.
So instead of sending an empty string using echo like the manual, just add a touch command to the virtualenvwrapper.sh:
file="$(virtualenvwrapper_mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX)"
touch $file # new command here
This should force windows to create the temp file. I can't post the rest of the links due to low rep but I hope this still helps someone.
EDIT
I created a pull request on the virtualenvwrapper repo and it got approved. You can see the touch command I suggested added here.

Related

Alternative Windows/bash command 'workon [insert virtualenv name]' for Mac Terminal [duplicate]

I have installed virtualenv and the virtualwrapper via apt-get, I got to a point where I created a virtual enviroment but however later on during that same day when I used the workon command it was not found. I further on went and inspected my home directory and .virtualenvs dir and the virtualenv I created earlier were still there.
Solving this problem took two steps:
Add this to your .bashrc / .bash_profile / .zshrc:
# load virtualenvwrapper for python (after custom PATHs)
venvwrap="virtualenvwrapper.sh"
/usr/bin/which -s $venvwrap
if [ $? -eq 0 ]; then
venvwrap=`/usr/bin/which $venvwrap`
source $venvwrap
fi
Then use:
source .bash_profile
# or .bashrc / .zshrc
to reflect the changes.
Additionally, if the terminal still sometimes cant find workon, use source .bash_profile to reset and find it again.
type source .profile in home directory from terminal.
Read the readme in the top of which virtualenvwrapper.sh
You need to source it inside bashrc
open ~/.profile
cd ~
nano .profile
add at the end
#virtualenvwrapper setup
export WORKON_HOME=$HOME/envs
export PROJECT_HOME=$HOME/dev
source /usr/local/bin/virtualenvwrapper.sh
to load your .profile file you just edited:
$ . .profile
I ran in to this problem too and I simply needed to logout and log back in.
This read in the changes which the debian package manager made to my system at /etc/bash_completion.d/virtualenvwrapper

Raspberry Pi file not found when present in location

I am following this tutorial to Installing OpenCv on my Raspberry Pi.
However, when I get to the stage to install the virtual environment the following commands execute without issue:
cd $cwd
python3 -m venv OpenCV-"$cvVersion"-py3
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "alias workoncv-$cvVersion=\"source $cwd/OpenCV-$cvVersion-py3/bin/activate\"" >> ~/.bashrc
However, the last command on that list source "$cwd"/OpenCV-"$cvVersion"-py3/bin/activate throws an error bash: /OpenCV-master-py3/bin/activate: No such file or directory
I've tried using sudo before the command along with repeating the entire process with sudo-s. When I check the file path the full file is located there with instructions at the top
#This file must be used with "source bin/activate" from bash
#you cannot run it directly
I've tried skipping that step and moving onto the next one but then I get another error: sed: can't read /etc/dphy: No such file or directory - but that's a whole different question.
How can I get the terminal to find the file that I can?

Set Path variable in Ubuntu 15.04

I have just moved to Linux (Ubuntu 15.04) and I am trying to add my python directory (:~/Documents/Python/Programs) to the path variable, but I am struggling..
I have tried export PATH = $PATH:~/Documents/Python/Programs, and then turning off and on again, but nothing happens
I have also looked at my ~/.profile, but don't under stand it, it comes up with (I have removed a tonnes of comments from the top):
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
UPDATE:
What I am trying to do is add my python directory to PATH so that I will be able to import self made modules from within this directory
I was under the impression I had to add this to PATH by adding
PATH="$HOME/Documents/Python/Programs/:$PATH"
To the bottom of my ~/.profile document, was this wrong, and what should I actually be doing to solve this?
You have to set your PATH e.g. inside ~/.profile or ~/.bashrc depending on where you want to use Python. Add something like this to the end of either of them:
PATH="$HOME/Documents/Python/Programs/:$PATH"
As stated by the comments, this change will only be take into account, after either restarting a login shell or new start of X session (e.g. newboot). If you need the changes directly, either source the file or export it manually... so
either
source ~/.profile
source ~/.bashrc
(you could also use the . operator, but this is only working in Bash)
or export the variable
export PATH="$HOME/Documents/Python/Programs/:$PATH"
It's important to add your custom path before original PATH as the shell will call the 1st file found inside $PATH.

Where do I find the bashrc file on Mac?

Hello I am following this page.. I'm installing Python onto my mac so that I can set up a Django / Eclipse development environment. However I am not too sure how to go about executing this step:
The script will explain what changes it will make and prompt you
before the installation begins.
Once you’ve installed Homebrew,
insert the Homebrew directory at the top of your PATH environment variable.
You can do this by adding the following line at the bottom of your
~/.bashrc file
export PATH=/usr/local/bin:$PATH
Where do I find the bashrc file on my mac and where do I find the homebrew directory?
I am running a macbook pro with OS 10.8.5.
The .bashrc file is in your home directory.
So from command line do:
cd
ls -a
This will show all the hidden files in your home directory. "cd" will get you home and ls -a will "list all".
In general when you see ~/ the tilda slash refers to your home directory. So ~/.bashrc is your home directory with the .bashrc file.
And the standard path to homebrew is in /usr/local/ so if you:
cd /usr/local
ls | grep -i homebrew
you should see the homebrew directory (/usr/local/homebrew). Source
Yes sometimes you may have to create this file and the typical format of a .bashrc file is:
# .bashrc
# User specific aliases and functions
. .alias
alias ducks='du -cks * | sort -rn | head -15'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
PATH=$PATH:/home/username/bin:/usr/local/homebrew
export PATH
If you create your own .bashrc file make sure that the following line is in your ~/.bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
I would think you should add it to ~/.bash_profile instead of .bashrc, (creating .bash_profile if it doesn't exist.) Then you don't have to add the extra step of checking for ~/.bashrc in your .bash_profile
Are you comfortable working and editing in a terminal? Just in case, ~/ means your home directory, so if you open a new terminal window that is where you will be "located". And the dot at the front makes the file invisible to normal ls command, unless you put -a or specify the file name.
Check this answer for more detail.
On your Terminal:
Type cd ~/ to go to your home folder.
Type touch .bash_profile to create your new file.
Edit .bash_profile with your code editor (or you can just type
open -e .bash_profile to open it in TextEdit).
Type . .bash_profile to reload .bash_profile and update any
functions you add.
In my macOS Monterey version, zsh is the default terminal shell. zsh executes ~/.zshrc every time the terminal is opened.
vi ~/.zshrc
#Add your path export to .zshrc
PATH=/usr/local/bin:$PATH
Now, when you open the terminal, the path will be set correctly.
On some system, instead of the .bashrc file, you can edit your profils' specific by editing:
sudo nano /etc/profile
~/.bashrc is already a path to .bashrc.
If you do echo ~ you'll see that it's a path to your home directory.
Homebrew directory is /usr/local/bin. Homebrew is installed inside it and everything installed by homebrew will be installed there.
For example, if you do brew install python Homebrew will put Python binary in /usr/local/bin.
Finally, to add Homebrew directory to your path you can run echo "export PATH=/usr/local/lib:$PATH" >> ~/.bashrc. It will create .bashrc file if it doesn't exist and then append the needed line to the end.
You can check the result by running tail ~/.bashrc.
The .bash_profile for macOS is found in the $HOME directory. You can create the file if it does not exit. Sublime Text 3 can help.
If you follow the instruction from OS X Command Line - Sublime Text to launch ST3 with subl then you can just do this
$ subl ~/.bash_profile
An easier method is to use open
$ open ~/.bash_profile -a "Sublime Text"
Use Command + Shift + . in Finder to view hidden files in your home directory.
Open Terminal and execute commands given below.
cd /etc
subl bashrc
subl denotes Sublime editor. You can replace subl with vi to open bashrc file in default editor. This will workout only if you have bashrc file, created earlier.

How do I install a script to run anywhere from the command line?

If I have a basic Python script, with it's hashbang and what-not in place, so that from the terminal on Linux I can run
/path/to/file/MyScript [args]
without executing through the interpreter or any file extensions, and it will execute the program.
So would I install this script so that I can type simply
MyScript [args]
anywhere in the system and it will run? Can this be implemented for all users on the system, or must it be redone for each one? Do I simply place the script in a specific directory, or are other things necessary?
The best place to put things like this is /usr/local/bin.
This is the normal place to put custom installed binaries, and should be early in your PATH.
Simply copy the script there (probably using sudo), and it should work for any user.
Walkthrough of making a python script available anywhere:
Make a python script:
cd /home/el/bin
touch stuff.py
chmod +x stuff.py
Find out where your python is:
which python
/usr/bin/python
Put this code in there:
#!/usr/bin/python
print "hi"
Run in it the same directory:
python stuff.py
Go up a directory and it's not available:
cd ..
stuff.py
-bash: stuff.py: command not found
Not found! It's as we expect, add the file path of the python file to the $PATH
vi ~/.bashrc
Add the file:
export PATH=$PATH:/home/el/bin
Save it out, re apply the .bashrc, and retry
source ~/.bashrc
Try again:
cd /home/el
stuff.py
Prints:
hi
The trick is that the bash shell knows the language of the file via the shebang.
you can also use setuptools (https://pypi.org/project/setuptools/)
your script will be:
def hi():
print("hi")
(suppose the file name is hello.py)
also add __init__.py file next to your script (with nothing in it).
add setup.py script, with the content:
#!/usr/bin/env python3
import setuptools
install_requires = [
'WHATEVER PACKAGES YOU NEED GOES HERE'
]
setuptools.setup(
name="some_utils",
version="1.1",
packages=setuptools.find_packages(),
install_requires=install_requires,
entry_points={
'console_scripts': [
'cool_script = hello:hi',
],
},
include_package_data=True,
)
you can now run python setup.py develop in this folder
then from anywhere, run cool_script and your script will run.
Just create ~/bin and put export PATH=$PATH:$HOME/bin in your bashrc/profile. Don't mess with the system, it will bite you back, trust me.
Few more things (relevant to the question but not part of the answer):
The other way export PATH=$HOME/bin:$PATH is NOT safe, for bash will will look into your ~/bin folder for executables, and if their name matches with other executables in your original $PATH you will be surprised by unexpected/non working command execution.
Don't forget to chmod+x when you save your script in ~/bin.
Be aware of what you are putting in your ~/bin folder, if you are just testing something or working on unfinished script, its always better to use ./$SCRIPT_NAME from your CWD to execute the script than putting it under ~/bin.
The quick answer is to symlink your script to any directory included in your system $PATH.
The long answer is described below with a walk through example, (this is what I normally do):
a) Create the script e.g. $HOME/Desktop/myscript.py:
#!/usr/bin/python
print("Hello Pythonista!")
b) Change the permission of the script file to make it executable:
$ chmod +x myscript.py
c) Add a customized directory to the $PATH (see why in the notes below) to use it for the user's scripts:
$ export PATH="$PATH:$HOME/bin"
d) Create a symbolic link to the script as follows:
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
Notice that hello (can be anything) is the name of the command that you will use to invoke your script.
Note:
i) The reason to use $HOME/bin instead of the /usr/local/bin is to separate the local scripts from those of other users (if you wish to) and other installed stuff.
ii) To create a symlink you should use the complete correct path, i.e.
$HOME/bin GOOD ~/bin NO GOOD!
Here is a complete example:
$ pwd
~/Desktop
$ cat > myscript.py << EOF
> #!/usr/bin/python
> print("Hello Pythonista!")
> EOF
$ export PATH="$PATH:$HOME/bin"
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
$ chmod +x myscript.py
$ hello
Hello Pythonista!
Just create symbolic link to your script in /usr/local/bin/:
sudo ln -s /path/to/your/script.py /usr/local/bin/script
Putting the script somewhere in the PATH (like /usr/local/bin) is a good solution, but this forces all the users of your system to use/see your script.
Adding an alias in /etc/profile could be a way to do what you want allowing the users of your system to undo this using the unalias command. The line to be added would be:
alias MyScript=/path/to/file/MyScript
i find a simple alias in my ~/.bash_profile or ~/.zshrc is the easiest:
alias myscript="python path/to/my/script.py"
Type echo $PATH in a shell. Those are the directories searched when you type command, so put it in one of those.
Edit: Apparently don't use /usr/bin, use /usr/local/bin
Acording to FHS, the /usr/local/bin/ is the good place for custom scripts.
I prefer to make them 755 root:root, after copying them there.

Categories

Resources