Am pretty new to python although have programmed a lot before. I'm using mac osx snow leopard and python 2.6.1.
I've followed this post about setting your PYTHONPATH to a custom scripts directory but can't get python to recognise it.
How can I run my python script from the terminal in Mac OS X without having to type the full path?
So i've got a simple helloworld script in /Users/richn/Documents/scripts/ called hello.py
Inside is this
#!/usr/bin/env python
print "Hello World!"
I've created a .profile file in my home directory with this in it
export PYTHONPATH=/Users/richn/Documents/scripts
I've also changed permissions of the file to make it executable with chmod a+x hello.py
Running ./hello.py in the terminal from that scripts folder works fine however whenever i run it outside of that folder i get this error
-bash: ./hello.py: No such file or directory
How can i get my scripts to run outside of that folder? Anyone got any ideas?
Thanks very much
What you'll want to do is to edit your PATH variable which is a list of directories your command shell checks when you run a command that does not begin with / or ./ rather than PYTHONPATH:
export PATH="$PATH:/Users/richn/Documents/scripts"
After you have exported your PATH variable you should be able to confirm that it has exported correctly:
echo $PATH
and afterwards you should be able to run "hello.py" successfully.
Related
This question has been asked before, but the answers are all several years old and I could not get any to work for me, so I would appreciate some help.
The question is simple, really: I have a python script, and a virtual environment I want it to run in when I double-click it, or call it from another program. How can I achieve this?
you must add packages directory address to your sys.path
sys.path a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module.
add line below to top of your script, now you can double click on script file and every thing must work fine
import sys; sys.path.append("./<environment path>/Lib/site-packages")
this is simple solution however, as sinoroc said it's not good solution because in this situation you are using system interpreter not virtual environment interpreter so i wouldn't be surprised if things don't work as expected.
also you can download all packages that you need and extract them into single folder and add that folder to sys.path
first download packages you need with command below
pip download <package names> --dest <directory name>
for example:
pip download requests --dest packages
at the end add folder contain you packages to your path
import sys; sys.path.append("./<packages directory>")
Recommended Solution
the best solution is that write little batch script for windows or shell script for linux that automatically active virtual environment and then run your script
first create file with .bat extention for windows or .sh extension for linux and then add line below to it
for .bat file
<environment path>\Scripts\activate && python <script name>.py
for .sh file
source <environment path>/bin/activate && python <script name>.py
now you can click on this batch script *.bat file or shell script *.sh file and everything work's fine
Some ideas
Shebang
Place a shebang pointing to the Python interpreter inside the virtual environement with the full path at the top of the script.
#!/path/to/my/venv/bin/python
import sys
print(sys.executable)
Shell/batch script
Write a shell script wrapping the call to the actual Python script:
#!/usr/bin/env sh
/path/to/my/venv/bin/python /path/to/my/script.py
Python wrapper
#!/usr/bin/env python3
import subprocess
command = [
'/path/to/my/venv/bin/python',
'/path/to/my/script.py',
]
subprocesss.check_call(command)
I've tried to run code on the cmd but it isn't working. I'm using windows7 and I changed the path from the default but I wrote the directory on the cmd. Can anyone help me?
python3 yourfilename.py
The above command can run your code, makesure you are at the project directory.
make sure python in your system path;
set python root path to system environment variable;then you can run 'python filename.py' ex
On Windows Vista, I need a script that starts the activate (to activate the virtualenv) script in:
C:\Users\Admin\Desktop\venv\Scripts\
And later, in the virtual environment, starts to the manage.py runserver in the folder:
C:\Users\Admin\Desktop\helloworld\
How should I do? What modules should I use?
You can activate your virtualenv and then start server using a bat file.
Copy this script in to a file and save it with .bat extension (eg. runserver.bat)
#echo off
cmd /k "cd /d C:\Users\Admin\Desktop\venv\Scripts & activate & cd /d C:\Users\Admin\Desktop\helloworld & python manage.py runserver"
Then you can just run this bat file (just double click) to start the server
runserver.bat:
CALL [your path]\Scripts\activate.bat
python manage.py runserver
If you want call virtualenv'ed Python directly you can do something like this:
C:\Users\Admin\Desktop\venv\Scripts\bin\python.exe manage.py runserver
Double check python.exe location on your virtualenv folder - don't remember how it is out of my head. This Python associates itself with the virtualenv and uses its site-packages by default.
I am using Anaconda 3 and python 3.7.6 on Windows. Had to do this in my .bat file:
CALL path\to\base\virtual\environment\Scripts\activate.bat path\to\your\virtual\environment
[path\to\your\virtual\environment]python.exe path\to\your\script\yoursript.py
Without activate.bat nothing works. I was getting an error about mkl-server. This error is described here https://github.com/numpy/numpy/issues/15523. People complained there about conda being broken, i.e. just calling python.exe yoursript.py does not work.
For me the above didn't work and therefore I will provide a more general answer.
But first specifically, this worked for me:
Open a notepad
paste this:
#echo off
CALL c:\1\env\Scripts\activate.bat
python c:\1\app.py runserver
save as whatever.bat
double-click this file to run
And generally: it is important to locate "activate.bat" under your python project. My project in this case was in c:\1 and the activate.bat under the relative directory env\Scripts which apparently may be situation dependent or have changed over time. This makes the general script:
#echo off
CALL [Your python project path]\[the relative path of your activate.bat]\activate.bat
python [Your python project path]\[your python filename].py runserver
In my case the project path was: c:\1
The relative path: env\Scripts
And the python filename: app
When I make a virtual environment the env files are placed relative to my python file. Just in case your situation is like in the question the call line in the script would change to
CALL [your activate.bat location]\activate.bat
i.e. in this situation the following should work:
#echo off
CALL C:\Users\Admin\Desktop\venv\Scripts\activate.bat
python C:\Users\Admin\Desktop\helloworld\manage.py runserver
Tip: I just found that python took my desktop as the working directory. It may therefore be a good idea to change your working directory to your python path. In my case adding cd\1 under #echo off does that trick.
Rather than using strings you can use a caret (^) as described in this question: Long commands split over multiple lines in Windows Vista batch (.bat) file
E.g.
cmd /k cd path/to/activate ^
activate.bat
pip uninstall --yes package ^
pip install git+https://git.server.com/user/project#remote/branch ^
deactivate
will open a venv and uninstall and reinstall a branch of a Git repository. This is a useful pattern for automating deployment of code into a venv.
For me, working with this code: (script_file.bat)
#echo off
CALL C:\Users\apo1979\Anaconda3\Scripts\activate.bat PyPWBI
C:/Users/apo1979/Anaconda3/envs/PyPWBI/python.exe "d:/.APO_OneDrive/script_SpeedTest.py" runserver
pause
Hello I am attempting to learn Python using django in powershell on windows to create a simple CRUD app but have some questions regarding the setup and usage.
I have setup the path environment variable to include paths to a folder that includes test .py scripts that I thought allowed me to run directly from a c:\ in powershell but when I try to run the samples using:
c:\> python test1.py
c:\> python .\test1.py
It does not work and I get:
c:\Python27\python.exe: can't open file '.\test1.py':[Errno 2] No such file or directory
but if I CD to path first and run it works fine and I get the output in the powershell window itself:
c:\> cd c:\Python27\Scripts
c:\Python27\Scripts> python test1.py
In addition if I just type the name of the script from the root c:\ prompt such as:
c:\> c:\Python27\Scripts\test1.py
c:\> test1.py
it runs the script by opening in a dos box that that closes immediately since it ran the script and finished.
What is the difference between these methods and is there an issue running scripts one way or the other?
The issue here is that in the first case:
c:\> python test1.py
c:\> python .\test1.py
You're running python itself, and then the file is an argument to the interpreter. So python will be affected by PATH, but the file is relative. Both of these file paths mean to look for it in the same directory, which is the root of C:. Since the file isn't located there, the error results.
The second case:
c:\> c:\Python27\Scripts\test1.py
c:\> test1.py
It's the file itself being run as an "executable", factoring in PATH. On Unix, this would use the shebang and invoke the shell in pretty much the same way, however that's not the case on Windows. Instead, it's basically running whatever is associated with the .py extension at the OS level, which is likely to invoke a new Python interpreter in a new Window, hence the behavior where it disappears after it's done.
On Windows, since there's no shebang, I generally prefer to run the python interpreter directly and pass the script as an argument. I would generally switch to the directory the script is located in first. This way it's more shell-centric, and, I think, straightforward.
You can run pushd . to save the cwd, and then after popd to go back, optionally. That would work in .cmd files and similar, too.
Are you sure test1.py is in C: and not in c:\Python27\Scripts
I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run code directly in the interpreters.
I have a basic script on the filesystem called Standalone.py:
#!/usr/bin/env python3.2
import sys
print("this is a standalone script.")
When I'm at my bash prompt I type $ python3.2 Standalone.py. I get a response saying this is a standalone script. But when I type $ Standalone.py then it tells me that the command is not found.
How do I run such scripts?
Thanks for any help.
update
I changed the permissions of Standalone.py to 755. Then I ran the command:
$ ./Standalone.py
and received the message:
: No such file or directory
I then switched the permissions of Standalone.py back to 644. Then when I ran
$ ./Standalone.py
I received the message
-bash: ./Standalone.py: Permission denied
Is there something I'm missing?
You need to make the script executable using
chmod +x Standalone.py
Usually, the current directory is not searched for executable files, so you need to use
./Standalone.py
to tell the shell that the script is in the current directory.
Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.
Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2
Make sure you're in the folder where your script is located you can check with ls
chmod +x Standalone.py
./Standalone.py
At first, to excecute a script it need to be executable. So you either have to do a chmod +x $file or a chmod 0740 $file. When you set the file permission to 644 you are putting the execute right away, so if gives you an error. If you are unsure of execution right and octal notation, you can use this : http://permissions-calculator.org/decode/0644/.
To really answer your question then, if you want to call the script with $file.py it needs to be in your PATH variable. You can display it with echo $PATH. Those are the directories that are searched for script to execute. So you simply need to give your script the executable right and put it in one of the directory given by your PATH.
Can you check if /usr/bin/python or /usr/bin/python3.2 exists
Execute below comamnd:
which python3.2
and then use the resulting path on top of you script.