Linux: SyntaxError for error-free Python script - python

I am trying to run a python file that works fine on my Windows machine on a remote server:
import collections
import pandas as pd
from pathlib import Path
import shelve
import cloudpickle
import numpy as np
import typing
from typing import List
import ray
from ray.rllib.agents import ppo
from ray.rllib.utils.spaces.space_utils import flatten_to_single_ndarray
from ray.tune import register_env
from ray.rllib.env.base_env import _DUMMY_AGENT_ID
from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
.
.
.
if __name__ == '__main__':
WIN = False
ray.init(dashboard_port=8263)
daterange = pd.date_range('2017-01-01', periods=35040, freq='15T')
norming_factor = 10
actions_module = ActionsModuleContinuous()
batch_n_days = 1
kappa = 1000
seed = 1234
steps_per_episode = batch_n_days * 24 * 4
num_episodes = 5
device_config: List = list()
device_config.append(Device1(WIN=WIN, norming_factor=norming_factor, n_data_points=steps_per_episode))
device_config.append(Device2(WIN=WIN))
device_config.append(Device3(WIN=WIN))
It returns the following SyntaxError, for no apparent reason:
(venv) [<username>#<server> examples]$ python test.py
File "test.py", line 183
device_config: List = list()
^
SyntaxError: invalid syntax
I have tried deleting : List, which just had the effect of moving the same error to a seemingly arbitrary place further down the script. Any help is greatly appreciated.

device_config: List = list()
That syntax is adding type annotations for variable device_config.
If you get SyntaxError: invalid syntax exception, it means that your python interpreter used to run your code is not new enough. Nothing to do with your code, it just means that when you enter python in your shell, interpreter that gets executed is not new enough.

Related

Import issues in Visual Studio Code

I am trying to execute this code in Visual Studio Code, the code works, my problem is related to the import of numpy; the other imports are working.
import codecs
from operator import le
import string
from struct import unpack
import paho.mqtt.client as mqtt
import struct
import numpy as np
def on_connect1(client1, userdata1, flags1, rc1):
client1.subscribe("MYTOPIC")
def on_message1(client1, userdata1, msg1):
#print(msg1.topic+" "+ "TERMORESISTENZA: "+str(msg1.payload))
Byte_Order = '<' # little-endian
Format_Characters = 'f' # float (4 bytes)
data_format = Byte_Order + Format_Characters
r = np.array(list(struct.iter_unpack(data_format, msg1.payload)), dtype=float)
print(r)
When I run the code it returns me this error:
ModuleNotFoundError: No module named 'numpy'
Any suggestions?
I used the Windows Command Prompt to install numpy with the command:
pip install numpy
and solved the problem

Import own python modules in nextflow script block?

I created a python script called utilities.py in bin/ directory:
#!/usr/bin/env python3
import numpy as np
import pandas as pd
from datetime import datetime
import io
def print_info(in_df, fname_base):
buffer = io.StringIO()
df = in_df.copy()
df.info(buf=buffer)
s = buffer.getvalue()
with open(fname_base+"_info.txt", "w", encoding="utf-8") as f:
f.write(s)
def print_desc(in_df, fname_base):
df = in_df.copy()
desc = df.describe()
desc.to_csv(fname_base+"_desc.tsv", sep = '\t')
def print_data(in_df, fname_base):
df = in_df.copy()
print_info(df, fname_base)
print_desc(df, fname_base)
df.to_csv(fname_base+".tsv", sep = '\t')
and made it executable with chmod +x. I would like to use these functions in a several script blocks in various processes in my workflow. Currently when I try importing a function from my utilities module:
#!/bin/bash nextflow
process transform_data {
input:
path(data)
output:
path("out.tsv"), emit: out_data
script:
"""
#!/usr/bin/env python3
import pandas as pd
import io
from utilities import print_info
"""
}
I get the following error:
Traceback (most recent call last):
File ".command.sh", line 4, in <module>
from utilities import print_info
ModuleNotFoundError: No module named 'utilities'
Is it possible to import own modules in this way?
Which version of Nextflow are you using?
I tested with v22.04.5 and the following works:
My setup is little bit different, instead of specifying #!/usr/bin/env python3, I directly invoked a python script (test.py) which has from utilities import print_info inside it, and it works fine.
script:
"""
test.py
"""
Note that the following won't work: from .utilities import print_info. Therefore, you can import custom Python module with Nextflow.

How to import "gleam" package in Python 3?

I'm tyring to import the "gleam" package in Python 3. I have installed the "gleam" package successfully, but still it showing error.
from wtforms import fields
from ggplot import *
from gleam import Page, panels
class ScatterInput(panels.Inputs):
title = fields.StringField(label="Title of plot:")
yvar = fields.SelectField(label="Y axis",
choices=[("beef", "Beef"),
("pork", "Pork")])
smoother = fields.BooleanField(label="Smoothing Curve")
class ScatterPlot(panels.Plot):
name = "Scatter"
def plot(self, inputs):
p = ggplot(meat, aes(x='date', y=inputs.yvar))
if inputs.smoother:
p = p + stat_smooth(color="blue")
p = p + geom_point() + ggtitle(inputs.title)
return p
class ScatterPage(Page):
input = ScatterInput()
output = ScatterPlot()
ScatterPage.run()
Error:
ModuleNotFoundError - Traceback (most> recent call last) in ()
----> 1 import gleam
C:\pythonNJ\lib\site-packages\gleam__init__.py in ()
5 import os
6 import json
----> 7 import urlparse
8 from collections import namedtuple
9
ModuleNotFoundError: No module named 'urlparse'
I looked for the solution and I found that urlparse has been moved to a new module in python 3, which can be imported as
from urllib.parse import urlparse
And I even imported it, but still when I trying to import "gleam" package it shows error of module "urlparse". Can you suggest me how to bypass it (bypassing import urlparse statement and importing gleam package in Python 3).
I know how to import the urlparse but I don't know how to import the gleam package.
You have two possiblities:
Modify source code yourself as you stated inside gleam package, but it could work incorrectly.
Fall back to version of python it works on - so 2.7 it seems, since the modification you mentioned was done with python 3.0 release. It's stated in docs here.
Just do this to get over it:
from:
import urlparser
to:
import urllib.parse

How to resolve "ImportError : no module named ... " in python?

Chaps I am going through what is apparently a #1 problem for python novice. I have been through some tutorials but I really can t get it works. Here is the code :
import time
from settings import *
from actif_class import *
from get_settings import *
from dataython import *
from spreadython import *
from tankython import *
if __name__ == "__main__":
t0 = time.clock()
settings = get_settings()
tickers = get_data_mp(settings)
list_spreads = get_list_spread(tickers,settings)
list_spreads_tank = tanking(list_spreads,settings)
spread_traitable = obtention_spreads_traitables(list_spreads_tank,settings)
print 'done. Timing',time.clock()-t0,'seconds'
and here is the stack :
ImportError: No module named datayhton
Even though the module DOES exist and is in the same folder as every other modules. It is able to see the get_settings one but not dataython. I ve tried on another machine but still got the same trouble.
I tried to go through import sys, sys.path.append but I might have done something wrong because it still doesnt work.
Any help would be much appreciated.
EDIT : still doesnt work when I write this on top of my code :
import time
import sys
sys.path.append("/path/to/dataython")
Ok. I got it now. Not a no brainer so here is the amended code :
import time
import sys
sys.path.append("path/to/your/file")
import your_file
the mistake I was doing was keep writing :
from your_file import *

Python import using dot notation doesn't work as expected

I recently installed a library in Python 3.3.2. I tried to import a module from it like this: import cx_Freeze.freezer. However, cx_Freeze.freezer is not defined as I would have expected, as shown in IDLE:
>>> ================================ RESTART ================================
>>> import cx_Freeze.freezer
>>> cx_Freeze.freezer
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
cx_Freeze.freezer
AttributeError: 'module' object has no attribute 'freezer'
>>>
The same thing happens in the command line. I think I am misunderstanding what happens when you use import with dot notation; what name does the module get assigned to?
In order to fix this seeming problem, I tried import cx_Freeze.freezer as f after restarting the shell, but that gave the same error as before. Can someone please explain why these import statements aren't giving me access to the module?
cx_Freeze/__init__.py has the following contents:
version = "5.0"
import sys
from cx_Freeze.dist import *
if sys.platform == "win32":
from cx_Freeze.windist import *
elif sys.platform == "darwin":
from cx_Freeze.macdist import *
from cx_Freeze.finder import *
from cx_Freeze.freezer import *
from cx_Freeze.main import *
del dist
del finder
del freezer
The parts important to this question are from cx_Freeze.freezer import * and del freezer. The first of those lines imports everything listed in cx_Freeze.freezer.__all__ directly into the cx_Freeze package, and the second line makes cx_Freeze.freezer not available directly. Thus, you should probably just use cx_Freeze; it contains all the parts of cx_Freeze.freezer designed for external use. If you need cx_Freeze.freezer, perhaps to use some of the private functionality, you can find it in sys.modules:
import sys
freezer = sys.modules['cx_Freeze.freezer']

Categories