Python package cannot be loaded into PBS queue file - python

The skopt package (https://scikit-optimize.github.io/stable/install.html) was installed on a cluster I use.
When I run the code in python directly in the terminal (i.e., cluster terminal), no problem occurs and the code works as expected.
However when I simply place the command to execute the code in a PBS queue system file (e.g., python3 ./code.py), I cannot load the installed package and I get the following message:
Traceback (most recent call last):
File "./test.py", line 22, in <module>
from skopt import gp_minimize
ModuleNotFoundError: No module named 'skopt'
Could someone give me an insight on how to solve the problem?
If I need to provide any more information, let me know.
ps1: the packages is installed on /usr/local/lib/python3.6/site-packages/skopt directory.
ps2: I tried to load export PYTHONPATH=$PYTHONPATH:[$HOME]/.usr/local/lib/python3.6/site-packages/skopt into the PBS file and it didn't work.

This has happened to me before but its a pretty simple fix
pip3 converts to python3
pip converts to python
Use those commands instead of random. Check my comment for further details.

The solution was easier than I thought, as the skopt package was in the path "/usr/local/lib/python3.6/site-packages/" just use the command "export PYTHONPATH=$PYTHONPATH:/usr/local/ lib/python3.6/site-packages", before running the code in python
I was using the "export" command wrongly.

Related

Best way to organize and develope a CLI Python project

I am working on a cli application in someways similar to aws-cli.
I started my project with all my code in one big python file. it seems a bit disorganize so I broke up the code into a folder somethingcli similar to awscli.
I also made a directory bin/ with similar code that you find in aws-cli.
But now I am unable to run it.
I have the similar error when I try to run aws-cli.
So I would do: python3 ./bin/aws
Then get:
Traceback (most recent call last):
File "/home/user/Projects/Other/aws-cli/./bin/aws", line 19, in <module>
import awscli.clidriver
ModuleNotFoundError: No module named 'awscli'
I understand the error... But how is one supposed to run the code when you are developing? Are you supposed to run ./setup.py each time? Confusing.
So I run the install and then go to wherever it installed everything and then work on the code there and copy it back in the directory to then push it to my repo?
Also when I run the unit-tests for aws-cli I get a bunch of errors.
I am still new to Python so this might be a very stupid question. I am just trying to do things the right way.
Python version: Python 3.9.13

Why does python say, "no module named os"?

As usual, I opened Notepad++ and wrote my python code. Then when I opened my CMD to execute it, I got this error
`Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\Lib\site.py", line 73, in <module>
import os
ModuleNotFoundError: No module named 'os'
I uninstalled and reinstalled python hoping that it'll run, but it didn't. Can anybody please help!
EDIT: Python doesn't work on my pc. Whenever I type "python" in the CMD, it would throw this error, IDLE is also not opening. And yes, my python is added to PATH so please don't say to add it to the path.
Try to run this snippet
import os
output = os.environ['HOME']
print(output)
If this is not working then there is issue with your python installation. You need to re-install it.
To get comfortable with python modules you can learn from here

Lots of trouble running a python script from the command line

Context for how you answer: I am new to the command line environment, except when it comes to basic git commands. I've always just used an IDE like PyCharm or NetBeans to run stuff for school projects. Please frame your answer accordingly.
I have a very small python script that pulls down a URL:
import sys
sys.path.append(r'C:\Users\WNeill\PycharmProjects\bloomskyGrantGrove\venv\Lib\site-packages\bloomsky_api')
import bloomsky_api as bs
client = bs.BloomSkyAPIClient(api_key='pr-XXXXXXXXXX')
data = client.get_data()[0] # Dictionary formatted like JSON, if you want data besides the latest image
with open("image_URL.txt", 'w') as file:
print(data.get('outdoor').get('image_url'), file=file)
I did the sys.path.append() because I read in a different question that it would solve my problems of "module not found" when running my scripts from the command line.
Well, it did, sort of... now, it finds my imports, but apparently my imports have imports...
$ py -m bloomtest.py
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "C:\Program Files\Python38\lib\runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "C:\Users\WNeill\PycharmProjects\bloomskyGrantGrove\bloomtest.py", line 4, in <module>
import bloomsky_api as bs
File "C:\Users\WNeill\PycharmProjects\bloomskyGrantGrove\venv\Lib\site-packages\bloomsky_api\bloomsky_api.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
So what do I do to make this work when my dependencies have dependencies?
Don't use sys.path.append. Imagine sending your code to someone else, their packages won't be in the same path and they won't be able to run your program. You might not plan on distributing your code but it's just bad practice.
Instead you should use pip to install your packages, as i assume you've been using the PyCharm package manager. I think it automatically installs with current versions of python (not sure though, I'm on Linux) and it's used like so:
pip install BloomSky-API
it'll automatically get all the dependencies and put them in the right places.
I've never used the py command before (am I missing out?), try using python bloomtest.py to run it instead just to be sure. You might get an error telling you that python is an unrecognized command or file, if that's the case it means your PATH is not set up correctly. I've found the easiest way to resolve this is to simply reinstall python, making sure to check the checkbox that says to add python to your PATH.
I'd usually post suggestions like this in a comment if I'm not sure if it solves the problem you're having, but the answer is too long to fit in a comment. Hope this helps!
I want to post the final solution to my problem, which I stumbled on thanks to DutChen18's answer.
He said I should use pip install to install all of my packages, which is one thing that I already do. I don't know much about the command line, except basic git and that. Trying to do it again gave me requirement already satisfied errors.
However, I was using an embedded terminal in PyCharm: C:\Program Files\Git\bin\bash.exe, which comes when you download Git. This works great in PyCharm because it automatically starts in the working directory of your project. Very convenient for me, a new command line user.
I decided to open up Git Bash separately from PyCharm and run pip install again. The first thing I found is that it didn't work without python -m pip install, unlike in the PyCharm embedded terminal.
Once I figured that out, I tried to python -m pip install BloomSky-API, but this time it didn't tell me that the libraries were already installed. All of a sudden, I could run my python script from the command line.
I have ZERO clue as to why this happened or why it now works, and I would love to hear a more technical explanation now that I have things working.

Matplotlib not importing correctly

I'm on Windows using Python version 3.4.2. My Matplotlib is not importing in my file when I run it in the cmd, but it seems to work when I import from the python shell. I'm not sure if my path is messed up somehow. Whenever I run it in the cmd, it produces the following error:
F:\pypractice> py montecarlo.py
Traceback (most recent call last>:
File "montecarlo.py", line 2, in <module>
import matplotlib
ImportError: No module named matplotlib
Again, it works in the python shell, so it probably is a PATH problem. The path I have in my environment variables is F:\Python34\;F:\Python34\Scripts\; I was told to install the following modules: numpy, pyzt, dateutill, setuptools, and six. This fixed the importing error in the python shell.
All of my modules are placed in F:\Python34\Lib\site-packages.
Any help with fixing this problem would be greatly appreciated. Thank you.
I was messing with the wrong variable. Simply placing #!/usr/bin/env python at the top of my file fixed the problem.

Issue with my Python environment

Brand new to Python, coming from Ruby.
I have a script that works perfectly if I run it form ipython or ipython qtconsole. I then tried to turn it into an executable script -- threw #!/usr/bin/env python at the top.
Running the script throws an error:
$ ./script/myscript.py
Traceback (most recent call last):
File "./script/myscript.py", line 6, in <module>
import yaml
ImportError: No module named yaml
Obviously there's something wrong with how python is loading modules (as it works perfectly fine from the REPL) but I have no idea how to fix it.
Thanks!
What is likely happening is you have more than one version of Python installed on your system, and the yaml module is only installed in one of them. When you run ipython it's using one version, but your script's shebang line is finding another version. Run
head `which ipython`
and see if it matches up to the result of which python (I'm betting it won't). Once you know the path to the python binary being used by ipython, you can specifically define it in your script's shebang line.
As a long-term fix, edit your $PATH variable and put the directory containing your desired version of Python ahead of the directory shown by which python, so that you can continue to use #!/usr/bin/env python as a shebang.
ipython must be pointing at a different version of python than what is in PYTHONPATH.
You can find out by looking at cat /usr/local/bin/ipython.
Look at
ipython reads wrong python version

Categories