Need help in Powershell for Python programming - python

So, I bought this book called "Learn Python the Hard Way" and in the learning progress as it is my first programming language.
The problem I'm facing now is I couldn't create a directory in Powershell for Windows 7.
I followed exactly like what the book said "mkdir mystuff" and I got the following error.
All I want is to make the directory becomes like this
C:\Documents and Settings\User\mystuff
How do I solve this ? Your help is greatly appreciated.
Everything works fine in WindowsXP but not Windows 7.
PS C:\Windows\System32\WindowsPowerShell\v1.0> mkdir mystuff
New-Item : Access to the path 'mystuff' is denied.
At line:38 char:24
+ $scriptCmd = {& <<<< $wrappedCmd -Type Directory #PSBoundParameters
}
+ CategoryInfo : PermissionDenied: (C:\Windows\Syst...ll\v1.0\mys
tuff:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft
.PowerShell.Commands.NewItemCommand

Windows is preventing you from adding a directory to the System32 folder. You don't have permission to do so by default, because really you shouldn't do that.
Instead try changing directory to somewhere more reasonable and making the directory there. %UserProfile% is an alias for your home directory.
cd %UserProfile%
mkdir mystuff
The reason this works in XP, by the way, is that XP isn't very strict about security or permissions. Which is wrong. : )

Based on the prompt you're trying to create a folder in the PowerShell directory path and you don't have the appropriate permissions to do so. Try this instead:
mkdir $env:USERPROFILE\mystuff

If you wanted to create the directory C:\Documents and Settings\User\mystuff, try to be explicit about it:
cd "C:\Documents and Settings\User\"
mkdir mystuff
Have fun learning Python. :-)

Related

Updated Macbook to Ventura - now getting 'command not found: Flutter'

Oddly enough I'm able to run my flutter applications, but I'd like to upgrade. However since I've updated to MacOS Ventura Flutter can't be found anymore...
output of echo $PATH:
/Users/me/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/flutter/bin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
What the top of my .zshrc file looks like:
# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
Any help and info is greatly appreciated
P.S dart and python also cannot be found. But they're not in my path so guess that makes sense
If you normally start flutter by typing:
Flutter
you can search in /usr and /opt for a file (rather than a directory) that is executable (i.e. a program) and called Flutter like this:
find /usr /opt -name "Flutter" -type f -perm +111
Then, if/when you find it, add the name of the directory that contains Flutter to your PATH. So if it finds:
/opt/homebrew/bin/Flutter
you would do:
export PATH=$PATH:/opt/homebrew/bin
If you don't find it in /usr or /opt, you will need to perform a more time-consuming search across the whole filesystem and also suppress error messages:
find / -name "Flutter" -type f -perm +111 2> /dev/null

how to fix "Operation not permitted" when i use launchctl in macos catalina

I'm setting up a launchctl server to run a python file regularly. So I write a.plist file , auto.sh file and it works well.
However, after I installed Macos Catalina, it failed.
I write "ls -l" in auto.sh to check file permission,
log shows that:
ls: .: Operation not permitted
python3: can't open file 'auto.py': [Errno 1] Operation not permitted
How can I do to fix it? Thank you so much.
here is my code:
auto.sh:
#!/bin/bash
. ~/.bash_profile
conda activate base
cd /Users/gassy/Documents/
ls -l
python3 auto.py
I put such .plist file in /Users/gassy/Library/LaunchAgents/com.gassy.fangzhou.plist
...
<key>Program</key>
<string>/Users/gassy/auto/launch.sh</string>
...
Finally figure it out...
It's a problem related to Catalina new permission system, the /bin/bash need to have the [full disk access].
I think the problem you have is not with Python, but with the file permissions on auto.py or the path leading up to it. What user account is used to run the script? Does that user have the necessary permissions on both those executables and the parent directory? Reason I suspect directory permissions is that ls is failing along with auto.py.
You might have some luck if you move everything out of /Users/gassy/Documents and to another location, perhaps under /opt or /var or similar, and then make sure that the permissions are sane. I know that macos treats some of those directores under /Users/<user> special, sometimes in a less-than-helpful way...
In the spirit of the principle of least privilege:
If you don't want to give bash Full Disk Access you can create a binary wrapper.
Create a C source code file launch.c.
#include <stdlib.h>
int main(void) {
int status = system("/path/to/launch.sh");
int ret = WEXITSTATUS(status);
return ret;
}
Then compile it to launch.
gcc -Wall -o launch launch.c
Grant it Full Disk Access add it the launchd Property List File.
Change the permissions of the file by using the chmod command in your bash script, before running python3 auto.py.
chmox +x auto.py should do, however I would recommend you to read more about it and be specific on your use case.
A modification of #mingxin's solution worked for me: Basically the same steps but instead of giving full disk access to bash in Security & Privacy, give full access to python3 (which on my Big Sur macOS system is in /usr/bin).

Powershell not recognizing conda as cmdlet, function, or operable program

I've been having this issue on my new laptop for a couple of hours and cannot figure out what's causing it. I'm trying to install scikit-learn with conda and get the following error
conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ conda install -c anaconda scikit-learn
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (conda:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Has anybody else had a similar issue on Windows 10?
Have you already activated the environment for this use case?
There is a long running thread about this on the GitHub conda discussion regarding conda failures various Windows 7 and higher, here:
https://github.com/conda/conda/issues/626
One suggestion is:
The down and dirty:
Check to see if activate works in cmd.exe.
If doesn't work or not acceptable--as #TurboTim shows:
Powershell needs the path to each env (anaconda3\envs\someenv\py33.exe. Laborious! :p
If you don't mind polluting your powershell a little, you can create a profile script which is run every time you open powershell.
The below will add the functions Invoke-CmdScript, Conda-Activate, Conda-Deactivate to your powershell. See Tim's link above for why.
PS C:> New-Item -Path $profile -ItemType File -Force
This creates a script at:
PS C:\> echo $profile
...something like C:\Users\yourUser\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Edit that script.
PS C:\> explorer $profile
Add this code, save, and reopen powershell (or . $profile ) :
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
Select-String '^([^=]*)=(.*)$' | ForEach-Object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
Set-Item Env:$varName $varValue
}
}
$condaRoot = "$Env:USERPROFILE\Anaconda3"
function Conda-Activate([string]$condaEnv) {Invoke-CmdScript $condaRoot\Scripts\activate.bat $condaEnv}
function Conda-Deactivate {Invoke-CmdScript $condaRoot\Scripts\deactivate.bat}
Usage:
C:\> Conda-Activate TFTheano
C:\> Conda-Activate root
C:\> conda info --envs
Disclaimers: Deactivate, as written, didn't do the job for me, thus I just use Conda-Activate to move around. Also,
I don't suspect there are security cautions with adding Invoke-Cmd to profile, so please chime in if 2 cents
I found this online: Unblock-File -Path .\Get-RemoteProgram.ps1
I'm going to try that command in power shell as soon as I can.
I previously had Python installed before conda and since having both together is said to be bring complications I uninstalled the original Python interpreter. This is what worked for me when the same error came up while trying to run a python script using conda.
TLDR; Simply add the path of conda.exe to the environment variables.

How to create a working directory using mkdir command in Mac OS X?

I started learning python recently and I am very new to programming.
My book tells the following :
On Unix-based systems (including Mac OS X and Linux), your working directory might be in /usr/home and be created by a mkdir command in a shell window or file explorer GUI specific to your platform, but the same concepts apply. The Cyg- win Unix-like system for Windows is similar too, though your directory names may vary (/home and /cygdrive/c are candidates).
I am running python on a Mac OS X and I am finding hard to create a directory.
What code should I type in the Terminal ?
I tried :
mkdir
It says :usage: mkdir [-pv] [-m mode] directory ...
cd c: mkdir
It outputs : -bash: cd: c:: No such file or directory
Please help me
Thanks
mkdir mydir creates a directory called mydir; then you could change directory like cd mydir. To get further information you could check out the man page for mkdir using man mkdir from your terminal. I'm not really sure what you mean with working directory. The pwd command prints out the current working directory, i. e. the directory you are currently standing in. – Cyclone
For making directory in mac use below command:
mkdir nameOfDir
If you want to check directory then use below command:
cd nameOfDir
The problem is with the name you want to use for your directory. If the name contains any characters as '#', '$' etc., then it might be considered as another command in the bash. Try to name your directory using only lowercase alphabets and underscores because of its convention(at least as far as I know).
If you want to still name it using some unconventional characters then use '' to tell the machine that it should not be considered as a command. It might look something like this.
$ mkdir \#659div2
Here character '#' is the unconventional character. Or like this
$ mkdir images\ from\ tour
Here character ' '(space) is an unconventional character.

Shell : workon not found [duplicate]

So, once again, I make a nice python program which makes my life ever the more easier and saves a lot of time. Ofcourse, this involves a virtualenv, made with the mkvirtualenv function of virtualenvwrapper. The project has a requirements.txt file with a few required libraries (requests too :D) and the program won't run without these libraries.
I am trying to add a bin/run-app executable shell script which would be in my path (symlink actually). Now, inside this script, I need to switch to the virtualenv before I can run this program. So I put this in
#!/bin/bash
# cd into the project directory
workon "$(cat .venv)"
python main.py
A file .venv contains the virtualenv name. But when I run this script, I get workon: command not found error.
Of course, I have the virtualenvwrapper.sh sourced in my bashrc but it doesn't seem to be available in this shell script.
So, how can I access those virtualenvwrapper functions here? Or am I doing this the wrong way? How do you launch your python tools, each of which has its own virtualenv!?
Just source the virtualenvwrapper.sh script in your script to import the virtualenvwrapper's functions. You should then be able to use the workon function in your script.
And maybe better, you could create a shell script (you could name it venv-run.sh for example) to run any Python script into a given virtualenv, and place it in /usr/bin, /usr/local/bin, or any directory which is in your PATH.
Such a script could look like this:
#!/bin/sh
# if virtualenvwrapper.sh is in your PATH (i.e. installed with pip)
source `which virtualenvwrapper.sh`
#source /path/to/virtualenvwrapper.sh # if it's not in your PATH
workon $1
python $2
deactivate
And could be used simply like venv-run.sh my_virtualenv /path/to/script.py
I can't find the way to trigger the commands of virtualenvwrapper in shell. But this trick can help: assume your env. name is myenv, then put following lines at the beginning of scripts:
ENV=myenv
source $WORKON_HOME/$ENV/bin/activate
This is a super old thread and I had a similar issue. I started digging for a simpler solution out of curiousity.
gnome-terminal --working-directory='/home/exact/path/here' --tab --title="API" -- bash -ci "workon aaapi && python manage.py runserver 8001; exec bash;"
The --workingdirectory forces the tab to open there by default under the hood and the -ci forces it to work like an interactive interface, which gets around the issues with the venvwrapper not functioning as expected.
You can run as many of these in sequence. It will open tabs, give them an alias, and run the script you want.
Personally I dropped an alias into my bashrc to just do this when I type startdev in my terminal.
I like this because its easy, simple to replicate, flexible, and doesn't require any fiddling with variables and whatnot.
It's a known issue. As a workaround, you can make the content of the script a function and place it in either ~/.bashrc or ~/.profile
function run-app() {
workon "$(cat .venv)"
python main.py
}
If your Python script requires a particular virtualenv then put/install it in virtualenv's bin directory. If you need access to that script outside of the environment then you could make a symlink.
main.py from virtualenv's bin:
#!/path/to/virtualenv/bin/python
import yourmodule
if __name__=="__main__":
yourmodule.main()
Symlink in your PATH:
pymain -> /path/to/virtualenv/bin/main.py
In bin/run-app:
#!/bin/sh
# cd into the project directory
pymain arg1 arg2 ...
Apparently, I was doing this the wrong way. Instead of saving the virtualenv's name in the .venv file, I should be putting the virtualenv's directory path.
(cdvirtualenv && pwd) > .venv
and in the bin/run-app, I put
source "$(cat .venv)/bin/activate"
python main.py
And yay!
add these lines to your .bashrc or .bash_profile
export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh
and reopen your terminal and try
You can also call the virtualenv's python executable directly. First find the path to the executable:
$ workon myenv
$ which python
/path/to/virtualenv/myenv/bin/python
Then call from your shell script:
#!/bin/bash
/path/to/virtualenv/myenv/bin/python myscript.py

Categories

Resources