Run Python module in another code but with an older version - python

I am using Python 3.10 but I want to run a Python module with Python 3.6 and use the result inside my code(that written with Python 3.10)
Please help me to solve it
thanks

Create a .py script that should be run with python3.6
Make the script prints your expected result
# script.py
import sys
print(sys.version[:6])
Use subprocess to get it
import subprocess
python3_6_result = subprocess.check_output(["python3.6", "script.py"])
print(python3_6_result) # 3.6.15

Related

pybind11 module cannot be found in Jupyter, but can be found in command line python and Spyder

Folks, I am having this puzzle with a pybind11 library mytest.cp37-win_amd64.pyd put in C:\Temp.
Then I have this:
import sys
sys.path.insert(0, r"C:\Temp")
from mytest import *
Now here comes the problem, if I launch the python in command line:
>python
>>>import sys
>>>sys.path.insert(0, r"C:\Temp")
>>>from mytest import *
It works fine. Or I just put above code in test.py, then run:
>python test.py
It also works. If I put this piece of code in Spyder, it works as well. But if I put this piece of code in Jupyter, it will not work by saying:
ModuleNotFoundError: No module named 'mytest'
I am sure all my tests are conducted in the same python environment by printing it out:
import os
print(os.environ['CONDA_DEFAULT_ENV'])
Am I missing anything here?
With Wayne's help and a previous post, I finally found out the root cause, that is the version of the python running in the Jupyter kernel is different from that of the environment from which the Jupyter is launch and also the pybind11 library is build with.
After I reinstall the Jupyter in the environment again, the library is picked up successfully.

bash: Python import - Command not found for pandas

I am a beginner in Python using MacBook
I want to import pandas in my Python script and I'm typing the following command below:
import pandas as pd
which results in:
Error: -bash: import: command not found
Questions:
How can I enable import command. I used #!/usr/bin/python and #!/usr/bin/env python3 as well but nothing happens after hitting enter.
I am importing pandas in a folder under Documents. I hope that's OK. I can't put the path where my Python is installed, since it is in Applications folder.
Sounds like you need to open the Python prompt first.
Try this:
$ python
>>> import pandas as pd
Where $ is the prompt in your bash shell, and the >>> is the prompt in your Python prompt. Don't type these in.
I think you are running python code on shell script. I tried to get the same result by running shell script with python "import command" in it.
I created a file test.sh
#!/usr/local/bin/python
import pandas as pd
and the output was: test.sh: line 3: import: command not found
You need to run it as "/usr/local/bin/python test.py" or just "python test.py"

How to handle 'ImportError: No module named profanity_check'

I am trying to make profanity_check run on a EC2 Linux instance.
The issue is that I get an ImportError: No module named profanity_check Error when I try to run profanity.py with python34 profanity.py.
Also, when try to run python profanity.py I get an ValueError: unsupported pickle protocol: 3 Error.
I assume this is because profanity_check only works with Python 3.
Furthermore I got an DEPRECATION: Python 2.7 will reach the end of its life warning when I installed profanity-check. However, I already have Python 3.4.9 installed on the system.
According to this issue here I already checked for the __init__.py file. It exists and looks stable.
The path to the __init__.py file is following:
/home/ec2-user/.local/lib/python2.7/site-packages/profanity_check/init.py
What could be the problem here?
profanity.py
#!/usr/bin/python34
from profanity_check import predict, predict_prob
predict(['predict() takes an array and returns a 1 for each string if it is offensive, else 0.'])
# [0]
__init__.py
from .profanity_check import predict, predict_prob
__version__="1.0.2"
You've installed profanity_check for Python 2.7
/home/ec2-user/.local/lib/python2.7/site-packages/profanity_check/__init__.py
But your script is run using Python 3.4
#!/usr/bin/python34 <-- this here
from profanity_check import predict, predict_prob
Change your script to use python2.7 or install profanity_check for python3.7

python3 - ImportError: No module named twython

I am using python3.
I installed twython on my MAC using pip3 command and I confirmed it was successfully installed.
When I run my python file, it comes up with:
ImportError : No module named twython
My code is as follows:
import sys
import string
import json as simplejson
from twython import Twython
I can't comment the response from #ajxs, but as additional information to his repsonse:
You can change the default python interpreter like this in your MAC terminal:
nano ~/.bash_profile
Add this to the file:
alias python=python3
exit the the bashrc file and run the following command:
source ~/.bash_profile
Now you can check the defaul python version with:
python --version
Maybe this helps you.
First thing that springs to mind is to check that you're running the script with the correct version of Python. Use python --version on the command line to check which version of Python you're executing by default. I've definitely had problems like this before when I've forgotten that my system's default version was 2.7 and I needed to use python3 to run Python 3 on the command line.

How can I fix this Python script?

I'm trying to convert a CVS repository to Git using cvs2svn and am following the directions on this page. I got to step 7 but am getting an error running git-move-refs.py:
Traceback (most recent call last):
File "../../cvs2svn-trunk/contrib/git-move-refs.py", line 23, in ?
from subprocess import Popen, PIPE, call
ImportError: No module named subprocess
For reference, this is what the script shows:
usage = 'USAGE: %prog [options]'
import sys
import optparse
from subprocess import Popen, PIPE, call
I'm not a Python expert but from browsing the web it looks like subprocess is a standard module, right? I'm using a Python installation built from source for version 2.6.3. What am I missing for this script to work?
I'm guessing that you have an old version (pre-2.4) of Python at /usr/bin/python, from your distribution, and the Python 2.6 you compiled is somewhere else (like /usr/local/bin/python). You have the Python 2.6 executable on your path before /usr/bin, so when you execute python from the command-line you get Python 2.6.
However, looking at the cvs2svn source code, git-move-refs.py's interpreter line is hard-coded to
#!/usr/bin/python
instead of #!/usr/bin/env python, which means when you run the script it uses the old Python.
As a workaround, run the script by passing it to your Python 2.6 interpreter:
user#host$ python /path/to/cvs2svn/contrib/git-move-refs.py

Categories

Resources