How to run a python script in virtualenv - python

I have a hello.py file located in "/home/user1/public_html/cgi-bin" that displays the python version:
#!/usr/bin/python
import platform
print "Content-type: text/html\n\n"
print(platform.python_version())
When I go to the url for the script, it displays "2.6.6" in the browser.
I am wanting to use python 3.5 so I had the webhost install a virtualenv in "/home/user1/virtualenv/testproject/"
When I try to change the first line of the code to:
#!/home/user1/virtualenv/testproject/3.5/bin/python
import platform
print "Content-type: text/html\n\n"
print(platform.python_version())
the script will display "It works! Python 3.5.5" which is not what I have coded into the script.
Am I supposed to put the hello.py file into the virtualenv directory? Or is it that I have the path to the python 3.5 incorrect? Or am I just totally misunderstanding how all of this works? Any help would be appreciated.

This will definitely resolve your problem
Your Code is correct nothing to worry because it will gives correct output in interpreter.
This problem is happening because you installed two versions of python on your machine and by default the path of python 2 is set in the priority in your Environment Variable.
The Script gives correct output in interpreter because priority of path is not required for interpreter but when you run it from browser or command-prompt or console then path is required and it search the path of python from system environment variable.In your case, First it got the path of python 2 ,so that its show the version of python 2
Their will be two solution for your problem, You can do anyone.
Uninstall python 2 from your system. This will resolve your problem instantly.
If you want to keep both version of python then for resolving this problem you should have to set the path of python 3 in the priority from environment variable. Like This
See in the Screenshot, Path of Python 2 is Upper in the Environment Variable should be below the Path of Python 3
Like This
This is the correct and static way to set the path in priority
Thank you, Expecting This will resolve your problem

phd's comment was the correct answer. print is a function in Python 3, so you must call it: print("Content-type: text/html\n\n"). print-as-operator is a SyntaxError in Python 3.

Related

not able to uninstall python and get python 3

i just started using python and i use VS Code.
When i do
python --verion
i get python version 2
py --version
i get python version 3
i wanted to install networkx which turned out to only work with python version 3. I did multiple things to change python version to 3 but could not.
So i decided to uninstall python and went to control panel and uninstalled
python
python launcher
and restarted the computer.
But when i run
python --version
it still shows up python 2.7.18
and this is my where python output
can anyone please help me to understand whats going on and how can i get python 3 to my system.
My solution:
I had already tried putting the python 3.1 path (default added by python when you click add path option while installing) above all path but had no intended outcome.
What i did:
installed python again and this time added the path by myself and moved it to top.
Now when i run
where.exe python it shows me three path, i don't know much but looks like there are two/three python in the system but it started showing python 3 for now.
my where.exe python output:
uninstalled
In windows If you do not have correct environment variable set then this is likely to cause the problem.
On your Powershell if you type $env:path or on command prompt echo %PATH%
you shall see your environment variable path which you have set,
where-in, either you may not have path to directory of Python 3 or you have it appended after Python2.
i.e.:
if your result show up as (... could be other paths in your variable) then only you will get python.exe to be pointing to your Python3 path:
...
C:\Python38\Scripts;C:\Python38;
...
C:\Python27\Scripts;C:\Python27\;
...
Update:
Based on OP comment it looks like there is misunderstanding with how environment variable works.
If you are still looking for why question then follow below steps:
List down all paths which are under environment variable PATH
Now go over each path and check contents of the directory. One of the directory would be having the python which is throwing you the version which you find mystery.

Run python program from terminal

I have downloaded a python program from git.
This program is python 3.
On my laptop i have both python 2.7 and python 3.4. Python 2.7 is default version.
when i want run this program in terminal it gives some module errors because of it used the wrong version.
how can i force an name.py file to open in an (non) default version of python.
I have tried so search on google but this without any result because of lack of search tags.
also just trying things like ./name.py python3 but with same result(error)
When you type "python", your path is searched to run this version. But, if you specify the absolute path of the other python, you run it the way you want it.
Here, in my laptop, I have /home/user/python3_4 and /home/user/python2_7. If I type python, the 3.4 version is executed, because this directory is set in my path variable. When I want to test some scripts from the 2.7 version, I type in the command line: /home/user/python2_7/bin/python script.py. (Both directory were chosen by me. It's not the default for python, of course).
I hope it can help you.
Use the shebang #!<path_to_python_version_you_want>
As in:
#!/usr/bin/env python
at the very top of your .py file
Also checkout: Should I put #! (shebang) in Python scripts, and what form should it take?
The Method of #Tom Dalton and #n1c9 work for me!
python3 name.py

Geektool not working with python3

When I try to run a python script with python3 it does not work but it works when I just use python. Why is this?
I have a simple hello.py file:
__author__ = 'A'
print("hellow")
When I use python ~/path/hello.py with geektool it works, but not with python3 ~/path/hello.py, the same works from terminal.
Also, where can I see geektool's log file?
From the comments, it looks like you have Python 3 installed at /usr/local/bin/python3. It could be that that's not part of the default PATH, but you've configured your login shell to add it to the PATH. Since your other program either executes the program directly or does it through a non-login shell, it won't read that configuration, and the PATH will remain at its default, excluding that directory. If that's the case, you might have to instead change your command to have an absolute path to Python:
/usr/local/bin/python3 /path/to/hello.py
This should work from the Terminal and any other environments.

Multilpe python versions and interpreters

I am trying to setup python 2.7.6 on my windows 7 machine (python 3.3.3 was installed first). When selecting the project interpreter after adding it in settings-project interpreter, I get this error
File "C:\Python33\Lib\site.py", line 173 file=sys.stderr) ^ SyntaxError: invalid syntax
When initially adding the python 2.7 interpreter in the settings page (as compared to adding the virtual environment above), it gives this error message
Cannot setup python SDK at C: .... . The SDK seems invalid.
I also get a similar error when adding any virtual env based on that interpreter.
I've added the python27 paths in the Paths box as in the picture, but it seems to be looking at the path set in windows. If I have to change that, doesnt that defeat the point of being able to select multiple interpreters?
Any help would be greatly appreciated.
I made a really silly mistake: I was messing with PYTHONPATH instead of PATH. I added both PYTHON33 and PYTHON27 to my PATH and it worked.
Try adding these commands before you run your UI
set PYTHONHOME=C:\Python33
set PYTHONPATH=C:\Python33\lib

How do I find where Python is located on Unix?

I'm working on a new server for a new workplace, and I'm trying to reuse a CGI script I wrote in Python earlier this year. My CGI script starts off with
#!/local/usr/bin/python
But when I run this on the new server, it complains that there's no such folder. Obviously Python's kept in a different place on this box, but I've got no idea where.
I haven't done much unix before, just enough to get around, so if there's some neat trick I should know here I'd appreciate it :)
Thanks!
Try:
which python
in a terminal.
For this very reason it is recommend that you change your shebang line to be more path agnostic:
#!/usr/bin/env python
See this mailing list message for more information:
Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, #!/usr/local/bin/python will fail.
For those cases, we get to call the env executable with argument which will determine the arguments path by searching in the $PATH and use it correctly.
(env is almost always located in /usr/bin/ so one need not worry that env is not present at /usr/bin.)
# which python
/usr/local/bin/python
Update:
I misread. Replace your header with
#!/usr/bin/env python
This will pull in the python location from the user that runs the script's environmental settings
Try: which python or whereis python
It is a good idea to use backticks for header Python script:
`which python`
The proper way to solve this problem is with
#!/usr/bin/env python
which allows for the use of a binary in the PATH in a shebang.

Categories

Resources